3 ms·
The 'print_and_assign_a' case comes up more often with a in an enclosing local scope, so you couldn't really get it to work prior to the introduction of 'nonloc
by anonymoushn 15y ago
The 'print_and_assign_a' case comes up more often with a in an enclosing local scope, so you couldn't really get it to work prior to the introduction of 'nonlocal' in Python 3. The best you could do was something silly like
a = [5]
def print_and_assign_a():
print(a[0])
a[0]=2
It's cool that the second case works in other implementations. I hadn't thought to test that.
- kbd 15y agoYes, 'nonlocal' definitely closed a hole in the language.