4 ms·
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
by orangecat 14y ago
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.
- whalesalad 14y agoI'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.
- tikhonj 14y agoJavaScript also has much better scoping--Python's using = for both defining and setting variables makes life much more difficult.
- masklinn 14y ago> JavaScript also has much better scoping Though I generally agree, that's as long as you mandate linting. JS's implicit globals are even worse than Python's implicit scoping.
- tikhonj 14y agoThat'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.
- hiddenbayes 14y agoAdding a "var" keyword, similar to that of Javascript, was discussed on the Python mailing list a while ago. See here: http://mail.python.org/pipermail/python-3000/2006-October/003968.html http://mail.python.org/pipermail/python-3000/2006-October/00...
- btilly 14y agoWhat 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. def demo (): counter = [0] def increment (): counter[0] += 1 def peek (): return counter[0] return (increment, peek) (increment1, peek1) = demo() (increment2, peek2) = demo() increment1() increment1() increment2() print peek1() print peek2() So there. Function literals. They work. 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.shtml 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.)
- orangecat 14y agoWhat do you mean by "usable function literals"? 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.
- btilly 14y agoI 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
- sitkack 14y ago