The one thing that I miss in Python is true anonymous functions. Perl has them. JavaScript has them. Ruby has them. Python just has lambda functions which can only evaluate an expression.
Meh. In Python you can put a def inside of any scope you want and do anything that you want with it. The fact that it winds up with a name in that scope doesn't stop it from being anonymous everywhere else.
I think the parent poster was referring to the fact that the critical thing is that a) you can define a function within the scope of another function, and b) you can return the inner function. To do this requires something called a closure, which in general involves some of the local variables being transferred to the heap.
You're right that almost all dynamic languages have this feature, but to give you two notable languages that don't: C doesn't allow defining inner functions. Pascal allows to define inner functions, but doesn't allow you to return them. C++ does allow you to define inner functions (actually functor classes) and allows you to return them, but you need to be explicit about the capturing process.
Right. Anything that you could want to do with an anonymous function, you can.
It is less frustrating for me than the fact that the scoping rules for a var in JavaScript mean that you're likely to scope to the nearest function call rather than loop. Despite having anonymous functions, doing what I would want to do with them in JavaScript is often much harder than doing it in Python.
>You're declaring the function within that scope. You can do this in other languages too.
>This isn't some Python-specific thing.
I think btilly meant that by the ability to do in Python what he described (define a nested function and then return it), you get (almost) all the same features as anonymous functions (and specifically, more than Python's lambdas give you, because they can only contain an expression, not statements) - in response to what you said:
>Python just has lambda functions which can only evaluate an expression.
In fact, IIRC, Guido (van Rossum, Python creator) has been known to say, to people who complain about the limitations of lambda in Python, to just use nested functions. And BTW they are quite powerful so you can do a lot with them.
He (btilly) was not implying that other languages cannot have nested function declarations (if I understood his comment right).
> And BTW they are quite powerful so you can do a lot with them.
I'm not saying that they aren't powerful. I'm saying that personally, I find declaring one-time-use functions in-line to be syntactically more pleasant / organized than sticking the function definition in the middle of functional code.
I'm not a newbie to Python, but after working with it for years, it's one of those things that I still wish existed. It's not necessarily a pain point or deal breaker or anything. I'm not even sure how it could work given Python's syntax.
>I find declaring one-time-use functions in-line to be syntactically more pleasant / organized than sticking the function definition in the middle of functional code.
Ok, I get what you mean now, and think you have a point there.