3 ms·
Perl, that miserable little unwanted orphan of a language, has real anonymous, first-class functions complete with closures. Python, sadly, does not Which Pyth
by jmillikin 17y ago
Perl, that miserable little unwanted orphan of a language, has real anonymous, first-class functions complete with closures. Python, sadly, does not
Which Python are you talking about? The language I use has had closures and first-class functions for years.
- abstractbill 17y agoThey don't seem to work very well: >>> def foo(): ... blah = 42 ... def wibble(): ... print "blah is " + blah ... wibble() ... >>> foo() blah is 42 Great! Looks like we have real closures! >>> def foo(): ... blah = 42 ... def wibble(): ... blah += 1 ... wibble() ... print "blah is " + blah ... >>> foo() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 5, in foo File "<stdin>", line 4, in wibble UnboundLocalError: local variable 'blah' referenced before assignment Oh. Maybe not.
- arohner 17y agoThat's a scoping problem, not a closure problem. The issue in this example is that python doesn't have true lexical scope. It has three scopes: function level, class level, file level. If you wrote "global blah", it would look at file level, not the next function level. I agree it's broken, but it's a different flaw than the closure problem.
- Cald 17y agoI think the nonlocal keyword would make your second example work as you expect (but I can't test it as I don't have Python 3.0 installed here). See http://www.python.org/dev/peps/pep-3104/ http://www.python.org/dev/peps/pep-3104/
- grunt 17y agoOnly since Python 3000, with the addition of the "nonlocal" declaration, can python claim to have closures. And yes, Python does also have "first-class functions"--in the same way that C has first-class functions. It's the "anonymous" part that it has difficulty with.
- ubernostrum 17y agoGlad to know I can add you to my list of people who believe that Haskell does not (and by definition cannot) have closures.
- blasdel 17y agoYou can't have closures without variables to close over! :)