Re:WTFPython – Understanding Python through surprising snippets
by Kwantuum on Monday December 23, 2019 @05:20AM
(#21862683)
You can achieve the same thing in python by using two lambdas (which is actually what you're doing in JS with map):
Or you can use default parameter values to use a single lambda (though this means it can be overridden, it's not semantically equivalent to the js implementation)
your python snippet is equivalent to the following JS:
And Python's comprehensions are actually a shorthand for writing the above generator with this syntax: yieldExpression for i in iterable. The semantics are consistent with the regular for..in
is equivalent to
This is all a consequence of python's late binding.
[ Parent
| on HN ]
Re:WTFPython – Understanding Python through surprising snippets
by Izkata on Monday December 23, 2019 @08:04AM
(#21863340)
Amusingly, the "default parameter" version relies on a feature that also gives rise to another WTPython entry: https://github.com/satwikkansal/wtfpython/blob/master/README...
Re:WTFPython – Understanding Python through surprising snippets
by sjagoe on Monday December 23, 2019 @08:36AM
(#21863483)
That's a different issue around default arguments that are mutable, rather than default arguments generally.
The lambda case here is more like a default argument of a constant integer (immutable) than a default that is a list, which can be altered directly by the function it's a default of.
Re:WTFPython – Understanding Python through surprising snippets
by Izkata on Monday December 23, 2019 @08:47AM
(#21863532)
The underlying feature that both rely on is that default arguments are set on function declaration instead of function call. Mutable/immutable only affects whether or not you hit the bug.
Re:WTFPython – Understanding Python through surprising snippets
by mplanchard on Monday December 23, 2019 @09:14AM
(#21863684)
Parenthetical comprehensions (generator expressions) are equivalent to your expanded function, but list comprehensions are not. `(x for x in range(10))` creates a generator, but using square brackets converts it to a list, including allocating the necessary memory. If you’re trying to do yield and streaming, the parens will serve you better.