5 ms·
Completely insane that python decided to hijack "def" to be used to create an object that very much is not a function. At the least they could have gone was cr
by throwawaymaths 2y ago
Completely insane that python decided to hijack "def" to be used to create an object that very much is not a function. At the least they could have gone was create a different keyword
- maxbond 2y agoIt's a function, it's just been transformed into a function with a different return type (that of a generator or coroutine, you can see the type signatures here [1]). You could do something similar in vanilla Python rather than language-level sugar, eg using decorators. [1] https://docs.python.org/3/library/typing.html#typing.Generator https://docs.python.org/3/library/typing.html#typing.Generat... As a caveat, I point out the type signature for academic interest, I prefer to use the simpler Iterable and Awaitable types in type signatures.
- xmcqdpt2 2y agoI think the type signature and example makes the parent's point, frankly. It has three types, on of which is contravariant, two different usage of the yield keyword, and two exit points. Now granted most uses of generators are simpler than that, but still that's a lot of special syntax. And if you are writing Python without type-checking, that's a lot of bookkeeping you'd have to do when reading code.
- maxbond 2y agoThat type signature is gnarly, I agree. I've seen people get tripped up because they think the function is the generator, and are confused about what behavior to expect from concurrent readers of different instances of the generator (because they don't realize that they are different instances in the first place). So my position is more about how to work with Python effectively/understand it accurately than in defense of it's syntax.
- throwaway81523 2y agoThey somewhat fixed that with "async def" but the whole thing is a huge tragedy anyway.
- cuu508 2y agoDo you mean they fixed the hijacking by adding the "async" keyword? By the whole thing do you mean the async functionality in Python in general? What is the tragedy?
- jononomo 2y agoI'm also curious to know what the tragedy is.
- darthrupert 2y agoDunno if they meant this, but I feel that adding async to a language/ecosystem where it was earlier not used makes everything awkward for a long time. Especially if it's done liberally like python did, with both sync and async styles still supported. It feels like the worst of both worlds at times. I think Rust suffers from this too, whereas Node.js has managed to make async the default mode of operation for enough things that it doesn't feel so bad. Especially with Typescript.
- throwaway81523 2y agoObligatory video, NSFW (lots of swearing): https://www.youtube.com/watch?v=bzkRVzciAZg https://www.youtube.com/watch?v=bzkRVzciAZg "Node.js is bad ass rock star tech".
- jononomo 2y agoI actually learned a lot from watching that video.
- andrewstuart 2y ago
- globular-toast 2y agoWhat's the actual problem with it? Or is this just a language "purity" thing?
- zo1 2y agoAre you talking about decorators? Very confused trying to parse what you're saying in the context of python.
- cqqxo4zV46cp 2y agoWhat? There is no tragedy here. It sounds like it just put DX semantics ahead of…what? some sort of language purity? I can’t say that I’ve ever thought “gosh! that async function is…an async function, and that’s tripped me up!” before. Not even once.
- hot_gril 2y agoThe bigger problem with Python to begin with was that you have to use def for functions, meaning you can't inline them like with JS arrow funcs or C++ lambdas. And this isn't a language purity thing, it's just inconvenient.