3 ms·
Javascript has true closures, Python doesn't (or didn't?)
by koide 14y ago
Javascript has true closures, Python doesn't (or didn't?)
- __alexs 14y agoPython has closures, it just doesn't have a very convenient anonymous function syntax. I can't say I miss function () { .. } very often though.
- koide 14y agoAFAIU Python's closure have been incomplete for a long time. I think they've become true, complete closures after the addition of the nonlocal keyword on 3.0 Please correct me if I'm mistaken. This all comes from a cursory research on the subject.
- klibertp 14y agoIn short, in your opinion, Python has no "complete" closures because you cannot bind a new value to the name from enclosing lexical scope from within a nested scope. I disagree.
- effn 14y agoThis is not possible in Haskell either. You never hear people claiming Haskell doesn't support closures.
- klibertp 14y agoWhich is why I disagree with claims that Python doesn't support them either. It does, of course; we could argue if Python supports "complete" closures, but we won't, because we know better than to use meaningless terms in discussion, right?
- koide 14y agoIn any case it's not been the case in a while, so I stand corrected. Although being able to write on the closed over variable is a neat feature to have, I would argue that feature does make a difference. Whether you call that complete or read-write or however you want to.
- btilly 14y agoTo my eyes they always were true and complete, but you had to play the minor trick of using mutable data to get them. Here is an example of what I mean that has worked forever. def outer (): counter = [0] def inner (): counter[0] += 1 return counter[0] return inner
- pekk 14y agoI like Python's scoping rules but declaring functions inline is a lot nicer and nicer-looking than defining functions starting with underscores.
- __alexs 14y agoThat is a false dichotomy. There are plenty of nice ways of working with closures with the given syntax.