Python has been my favorite language for many years, while the last few months at work I've been writing a lot of JavaScript. I've found that JS has exactly one advantage over Python, which is usable function literals. And that turns out to have a surprisingly large impact. It's convenient and natural to create "anonymous classes" that use closures to store their state, while in Python you'd have to manually create classes and manually copy the variables in __init__. I really wish Python could gain something like that while retaining its many advantages.
What do you mean by "usable function literals"? Because Python has them, even if you have to do a bit of work to get them.
def demo (n):
def multiplier (m):
return n * m
return multiplier
x5 = demo(5);
print x5(4)
You can do anything in the inner closure that you might want. The only surprise is scoping - if you want to modify state you need to use a mutable data type because otherwise you'll be instantiating a private variable. But that hoop is easy to handle.
But, you say, you really want anonymous classes? Well look up metaprogramming then. Python really has quite good support for it. There is no trouble creating anonymous classes on the fly with their own attributes, functions, and so on. They can even inherit from each other. http://www.voidspace.org.uk/python/articles/metaclasses.shtm... might be a good starting place on this for you.
(Note, I do not recommend metaprogramming unless the solution you make will be reused a lot.)
lambdas. Your example isn't a literal, it's just a declared function named "multiplier". Sure, it has equivalent functionality, in the same sense that Python could remove collection literals like [1,2,3] and you could still create lists and maps by hand. But that would be silly.
even if you have to do a bit of work to get them
And that work means that useful techniques become too unwieldy to use in practice.
I am also annoyed by the omission of a truly useful lambda. But the rest of the language is compact enough that I've never found techniques relying on closures to be unwieldy in Python. shrug
My similar solution is here https://gist.github.com/3678958 it has the added bonus of using an object with the methods attached, but the outcome is the same.
I've been heavily involved with python and javascript for most of my programming life and find them to be very similar. It's a joy to write in either language.
That's fair. My editor marks those for me automatically, so they're much less of an issue for me, but they are still really stupid.
On the other hand, you can work around that particularly stupidity just by being careful. Working around the Python limitations requires either something hacky like using a one-element list instead of a variable or (only in 3, I think) a rather ugly "nonlocal" statement at the top of your scope.