3 ms·
Javascript isn't Haskell-esque, it's scheme-esque. I didn't mean to imply it was a substitute for Haskell, merely that it's a better version of Python. Javascr
by yummyfajitas 15y ago
Javascript isn't Haskell-esque, it's scheme-esque. I didn't mean to imply it was a substitute for Haskell, merely that it's a better version of Python.
Javascript also has proper closures, and it's support for event driven programming (admittedly, not part of the language) is also quite nice.
[edit, since I can't reply. By proper closures, I mean this shouldn't happen:
> x = 0
> def foo():
> x = x + 1
> print x
> foo()
UnboundLocalError: local variable 'x' referenced before assignment
]
- kibwen 15y agoWhen you say "proper closures", do you mean anonymous functions? I can't think of any other reason why Python's closures wouldn't be considered proper. EDIT: Interesting, I've never run into that behavior before. Looks like you'd have to explicitly mark that variable as global if you want to mutate it: x = 0 def foo(): global x x = x + 1 print x foo() However, after reading the rationale[1], I tend to agree with this requirement: "This isn't a bug; it's by design. Because there's an assignment to 'a' in the function 'y', 'a' is considered local to that function. (It doesn't matter where the assignment happens within the function; the presence of an assignment anywhere is enough to make 'a' local for the entirety of 'y'.)" [1] http://bugs.python.org/issue9049#msg108309 http://bugs.python.org/issue9049#msg108309
- yummyfajitas 15y agoYou don't necessarily have to mark it as global to mutate. Some more interesting behavior: class BoxedInt(object): def __init__(self, v): self.val = v x = BoxedInt(0) def foo(): x.val = x.val + 1 print x.val
- cdavid 15y agoThis is an annoying behaviour, but it is consistent with Python's design . It is not exactly caused by broken closure, rather by the object model of python, and how named are bounds to objects. While this does not make it less annoying, it explains why the difference between using x vs x.val is logical from python POV.