4 ms·
Is it the Python language or the Python ecosystem you are referring to? The statement in question is from The Zen of Python and is more of a guiding principle
by paddyoloughlin 11y ago
Is it the Python language or the Python ecosystem you are referring to?
The statement in question is from The Zen of Python and is more of a guiding principle for the design of Python than a rule.
The usual observation with Python is that it can have many libraries and frameworks which are quite similar and choosing one can be hard (e.g. web frameworks). This is a function of popularity and not something which can fairly be subject to that principle.
On a different note: missing is the Zen of Python's following line:
Although that way may not be obvious at first unless you're Dutch.
- bad_user 11y agoIt's both the language and the ecosystem. The language itself is a prime example of a language with features that aren't orthogonal. For example count how many features in Python are solved in other languages just with proper support for anonymous functions.
- DannoHung 11y agoPython's support for closures is perfectly fine. Its anonymous function support is crappy, but it doesn't stop you from doing what you want, it just looks as ugly as sin.
- jimmaswell 11y agoPython only lets you have one line in a lambda - the return line. In languages with nicer anonymous function support I can make an anonymous function span multiple lines and branches eg. in C# you could assign to a delegate something like myObj.stringToHash = (s) => {int x = 0; foreach(var c in s) x += ... ; return x;} I find python's restriction in this case fairly strange. If I wanted to do that in python I'd have to just declare a function body for what I want to put in the anonymous function elsewhere, which defeats the purpose.
- sophacles 11y agoFunctions can be defined in a nested way setting up closures. It isn't that different from what you wrote to do: def some_method(...): avar = aval .... (some code) def throwaway(x, y): .... something_else(avar, x+y) some_obj.somefunc = throwaway The major difference is you do the function definition just prior to the assignment. The "namespace pollution" is literally limited to the scope of "some_method", so you have an extra local var in a function somewhere. The semantics of python mean that the function is created on the stack, and a closure is created each time the some_method is called, and the closure is later GCed as any other object would be, so there is nothing fundamentally different about the "named" function, and the "anonymous" function in you example, except a single local variable.
- jimmaswell 11y agoThat's what I meant. Defining the throwaway function directly before the assignment is a good compromise at least.
- Retra 11y agoWell, if you need multiple lines, you probably don't need your function to be anonymous. (Your example is just `sum`...)
- deleted 11y ago[deleted]
- jimmaswell 11y agoI've needed it many times in the past where the function I'm assigning can't be done as a one-liner like that.