3 ms·
You can do the same in Python, with named functions or lambdas: >>> def foo(x): ... def add(y): ... return x + y ... return add ... >>> bar
by andrewaylett 15y ago
You can do the same in Python, with named functions or lambdas:
>>> def foo(x):
... def add(y):
... return x + y
... return add
...
>>> bar = foo(10)
>>> bar(5)
15
- masklinn 15y agoAnd it takes twice the token count and lowered readability for no gain whatsoever.
- burgerbrain 15y agoNo gain at all? How do you figure?
- olavk 15y agoTwice the token count? How did you get that?
- cyrus_ 15y agofoo = lambda x: lambda y: x + y bar = foo(10) This is perfectly correct Python code too if you prefer. The only real thing with Python's lambdas is that, because Python makes a distinction between statements and expressions, you can't easily do things like use temporary variables inside a lambda, but cases like that are rare and are typically more readable with a separate standalone function definition anyway.