Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.

    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.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.)


What 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.


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.


JavaScript also has much better scoping--Python's using = for both defining and setting variables makes life much more difficult.


> 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.


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.


Adding 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/00...




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: