3 ms·
When 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: I
by kibwen 15y ago
When 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