167 points · by ArdelleF · 4y ago · 190 comments
(import os)
(import [os.path :as op])
(with [f (open (op.join dir_name file_name) "w")]
(.write f "..."))
Some things you just have to learn, like using `(setv var value)` to do assignment, or the fact that `(for)` is an imperative loop while `(lfor)` is a list comprehension, but a little experimentation goes a long way and the docs are quite good.`pip3 install --user hy`
as mentioned in the first page of the docs:
a link: https://fable.io/blog/2022/2022-06-06-Snake_Island_alpha.htm...
In order for this to succeed, python libraries would have to be rewritten without the C API in the dialect/subset of python supported.
I wonder if erg could strive to use a subset of python mostly and innovate narrowly in just a couple of places to make python more functional.
- Common stuff (opening files, dealing with databases, string processing, math, regexps) is very easy. Sane defaults.
- Works well with the existing C ecosystem. It's not a "rewrite everything in X" language.
- It's everywhere, most machines run Python just fine.
- It doesn't treat Windows like a second-class citizen.
- It doesn't force upon you a programming model that you might or might not care about. It's not a puzzle language where everything is a monad, or everything is a class, or everything is some weird widget. Write the damn code.
- I'm personally not a fan of the language itself, but it's mostly a bog-standard algol if you don't poke it too much. It's relatively standard, people just get it even if Python isn't their main language.
- Very personal point, but it's the only language I can edit with a plain text editor. Think nano, vi, Windows Notepad, that kind of thing. I don't know what to tell you, maybe it's my tiny monkey brain, but I can't do the same in Java or C++. Not that it's my preferred way to edit software, but I like having that option in a pinch.
At least for what I do (developer, but with a data engineering/data science flavor) the overall architecture is usually the important thing, so admittedly some downsides might hit me less.
I'd still rather write medium-to-large pieces of software (say 10,000+ LOC) in something else (Java, C#, C++, something boring like that), but it's pretty much unbeatable for small programs as far as I'm concerned.
Python's internals are also relatively accessible and easy to work with, so it's smooth sailing once you have that need. The language starts out easy, and grows with you. Of the languages I've tried (and there are many), only Smalltalk and the Lisp family were comparable in expressiveness.
The language mostly gets out of my way and lets me do what I want. I don't feel like I have to fight the compiler (at least until I tried Mypy) or write a lot of tediously verbose boilerplate just to get out a "Hello, World!" like I did in Java.
Unlike, say, JavaScript, Python is pretty strongly typed and fails fast. The stack traces almost always point you to the exact location of the problem (unlike the JS tendency to propagate `undefined` everywhere). There aren't a lot of surprises or gotchas. I can pretty much run it in my head just by reading the code and be right most of the time. I cannot say the same for C++ or JavaScript, which naturally tend to become inscrutable without discipline.
It's the community. Network effect looms large.
While typing is dynamic, I rarely see this being used, perhaps with the exception of identifying empty variables with None. Most identifiers are born and die the same type. It would be interesting to see how much effect dynamic typing has on the rate of programming errors.
Also, for me, Jupyter is huge.
Objectively, none of these languages are particularly good at what they do and there are other languages that tend to be preferred by people who code for a living. But these are harder to learn and have higher barrier for entry. You typically need some theoretical background and experience.
I've used all of the above BTW. I learned programming using the manual that came with my Commodore 64 in the eighties (Basic, obviously). I kind of like Python for some things. Javascript is fine for simple things. None of these are my first choice of language but I can work with these languages. They are very approachable languages. Just open an editor and start adapting code.
There's a lot of language snobbery out there where objectively, the advantages aren't that huge or dramatic as people like to believe.
More modern statically typed languages can be quite easy to use (I am thinking of Scala) but none provides an ecosystem as rich as python's.
Plus, recent static languages (Scala, Rust, Haskell or even Elm) are more oriented toward "Software Engineers" or CS academics. Python appeals to a lot of "programmers" who's main job is actually data science, mechanical engineering, biologist...
I agree that working with Python code from an undisciplined/sloppy/inexperienced developer can be a pain (although in those situations I've found code from those same developers in others languages to be a problem too, but maybe worse in Python? I dunno), but in the hands of a solid dev, Python code is so readable and easy to follow, that to me code in other languages often feels much noisier. YMMV of course.
Also, in Python integers do not overflow, unlike in Rust, C and other buggy languages.
If you name a language, with high probability Python will be better in something unless it is about performance.
What I don't like: no type hints in many libraries, some libraries are not type-hint friendly, lot of unnecessary dynmamic features (like being able to patch modules in runtime) and absolutely no caring about performance and being able to compile into machine code.
> trying to figure out what some dynamically typed code is doing
There should be type hints.
My background was originally in C# then Java (plus JavaScript on the side, since most of us have to do that), and when I first came to Python I found it to be a lovely language and a breath of fresh air.
I guess I would say to me it feels neat and tidy, thoughful about interface design, and gives me a sense of quality. I get a similar feeling from Clojure (perhaps even more so).
I don't find static typing as valuable as you do, and additionally I find the cost in verbosity and in time wasted solving type-checking puzzles quite painful. Perhaps if I'd spent more time with one of the more sophisticated type systems I'd feel differently (Java and C# are quite limited here), but not convinced enough to try.
I think this is a problem in both static and dynamic languages and is pretty much solved in the same way: you test your code.
- Django is a robust, batteries-included web framework that doesn't have an analog in, for example, Rust.
now you can have succint code and fast repl (ala sml) but you don't get the libs
These aren't just some third-party tools bolted on. The type annotation syntax is built into the language[5] and standard library[6][7].
I personally find static typing to be more trouble than it's worth most of the time. Industry typing metalanguages are not expressive enough to deal with even fairly basic real-world programs and force you to write bad code to work around the type checker's stupidity. And, of course, you still have to write tests. Maybe someday they'll catch up to Idris. Python's static typing is no better, but at least it allows you to turn it off when it's not worth it.
[1]: https://github.com/microsoft/pyright
[2]: https://github.com/python/mypy
[3]: https://github.com/google/pytype
[4]: https://github.com/facebook/pyre-check
[5]: https://peps.python.org/pep-3107/
[6]: https://peps.python.org/pep-0484/
[7]: https://docs.python.org/3/library/typing.html#module-typing
* Download and just run doesn't work. (e.g. Node, dotnet, cargo do this well). Hell, I've generally had more success with C++ in this regard.
* The syntax can be used in an extremely terse way, and that terseness seems to be idiomatic.
So you'll end up with everything you import being `any` which is not really any better than just using Python.
Though Nim definitely can be described as a statically typed Python-compatible language! I haven't used them but https://github.com/Pebaz/nimporter https://github.com/yglukhov/nimpy both seem great. Nimporter in particular looks fantastic for writing fast python libraries.
Actually come to think of it that might be the easiest way to write fast KiCad 6 plugins.. I really want to try making a native KiCad autorouter. But I don't want to figure out the C++ plugin setup and since KiCad 6 the Python APIs seem better documented and supported anyways. Problem is that Python would likely be too slow. Nimporter could be perfect. It looks really simple to setup.
So I find this to be super interesting, particularly when it comes to parts with lots of pads that one has to route.
Python would let you iterate the algorithm quickly. It's far easier to optimize the big O notation of your autorouter than in C++, say. Once you're happy with the algorithm, then you can try to optimize the slow bits in C/C++. But even things like dicts in python are still going to be super fast.
As far as speed up goes, there's PyPy, Nuitka, and Cython, which are all interesting. There's also a restricted version of python called RPython which PyPy uses to compile everything to C.
If it's me in 2022, I'd probably go with Cython. A lot of packages uses it. It's well known, and it's (mostly) python -- mostly because it would be easier to go back and forth in python rather than switching mental contexts between Nim and C and Python.
See here for the examples.
https://cython.readthedocs.io/en/latest/src/tutorial/cython_...
Good luck with the project!
> Python would let you iterate the algorithm quickly. It's far easier to optimize the big O notation of your autorouter than in C++, say.
The hard part wouldn't be the routing algorithm itself. It's getting the algorithm and code to handle all of the corner cases. That's really what has kept me from it.. Getting all the correct data in and out of KiCad seems like a pain.
> mostly because it would be easier to go back and forth in python rather than switching mental contexts between Nim and C and Python.
If I were to tackle it, I'd do it in Nim since my overall productivity is as fast or faster than with Python. The Python layer would just be necessary to act as the glue layer to KiCad and `nimpy` looks great. It looks like `nimporter` builds binary wheel's too! That's awesome.
Cython seems handy but involves more mental switching between Python-to-C types than Nim would (IMHO). Taking an example from the Cython docs and writing it in Nim with nimpy would be:
proc randomNoise(number: int = 1): seq[float] {.exportpy.} =
result = newSeq[float](number)
for i in 0 .. number:
result[i] = rand(1.0)
Versus the original Cython: def random_noise(int number=1):
cdef int i
# allocate number * sizeof(double) bytes of memory
cdef double *my_array = <double *> malloc(
number * sizeof(double))
if not my_array:
raise MemoryError()
try:
ran = random.normalvariate
for i in range(number):
my_array[i] = ran(0, 1)
return [x for x in my_array[:number]]
finally:
free(my_array)> 2.times! do!:
May be what you mean is a static typing solution for Python whose syntax is a superset of Python syntax (which I don't think `erg` is), but isn't identical to Python's syntax( ie, it is a strict superset, which statically typed Python's syntax isn't). Typescript is exactly that for JS.
In that case, I agree with you that such an attempt would indeed be very interesting because it would allow for more aggressive exploration of ergonomic typing constructs while maintaining that any valid Python would be valid in that new language as well (just like TS).
However what I’m missing is having more advanced type constructs. For example mapped types, lookup types, etc [1].
That said, it seems like python devs keep adding features over time. For example a couple of years ago it wasn’t possible to specify which fields of a dictionary are required and not required (it required putting required fields in one TypedDict and then subclassing it into another one that had the non required ones with total=False), nor it was possible to “spread” a TypedDict to declare the kwarg types in a function.
[1]: https://www.typescriptlang.org/docs/handbook/release-notes/t...
The only thing you need to do is add appropriate type hints at function definitions and then run mypy in your build and CI and you got something that checks basically as strict as Java.
For those saying it’s optional so can’t be trusted, consider that compiling C++ with -Wall -Werror is also optional… adding it is simply part of standard project hygiene.
There's another comment making this claim elsewhere and I wonder if perhaps those assertions are coming from someone who doesn't write Java, or if my experience with mypy is just especially bad
I'll also point out that easily 90% of my heartburn with trying to typehint python in legacy projects is that python seems to encourage so much introspection and namespace trickery. It feels to me like the mypy audience is saying "well, there's a staticly typed language hiding in python so just don't use unsafe parts of the language and you'll be fine"
Does anyone know if Erg code can be (ergonomically) called from python? I think that'd be the real killer feature, being able to write erg modules and pull it into the greater python ecosystem as easily as native python.
* looks like the project is nary a few weeks old? Can't really dig into the git history on mobile, it freezes my browser
I think that limitation of statically typed Python alone is enough to give this new language a chance.
It's also looks like the type inference of erg will be better than the current Python checkers.
I think it's pretty damn amazing how well Python has been able to evolve into the realm of static typing. And no unofficial superset syntax required!
Pylance (the VSCode built-in linter, based on Pyright I believe) tends to do a better job than Mypy, but not as easy to integrate into CI. It's a real shame that the Python ecosystem doesn't have anything nearly as robust as TypeScript.
[0] https://github.com/python/mypy/issues?q=is%3Aopen+is%3Aissue...
https://peps.python.org/pep-0646/#grammar-changes
The other case that I remember off the top of my head is Python 3.6 adding annotations for variables that aren't created via function definitions:
Also thanks about pointing out the syntax change for variadic generics! I still think my overall point about pace of "syntactic innovation" in static typing remaining slow stands, though. Just comparing equivalent typescript and python constructs:
This in TS:
{ member?: string, other_member: string }
is currently in python: from typing import TypedDict
class SmallerObj(TypedDict, total=False):
member: str
class Obj(SmallerObj):
other_member: str
Python 3.11 will shorten the above to: from typing import TypedDict, NotRequired
class Obj(TypedDict):
member: NotRequired[str]
other_member: str
But still ...I don't know if this is what I'm looking for, but I think there's a ton of room for innovation and improvement in the python world. I welcome this language and I think it's quite interesting!
Are you using an IDE?
If you want to use static typing with c ES6 like syntax, Haxe is amazing. Haxe also compiles down to a half a dozen more compiler targets - including C++, C#, Flahs (via OpenFL), lua etc.
This panics for me after install. Any suggestions to make it work ? {I have Python in my path, so writing python would not return "not found" }
Starting the REPL server... thread 'main' panicked at 'cannot execute python: Os { code: 3, kind: NotFound, message: "The system cannot find the path specified." }',
Dont do cargo install erg , but rather build from source and this works.
Using a 69 as your language's logo? I actually don't think it has been done in a serious-seeming project before.