3 ms·
I don't see how nonlocal could fix this. Could you explain? More specifically, given a better 'default arguments thing', how would you interpret: x = [2]
by dalke 12y ago
I don't see how nonlocal could fix this. Could you explain?
More specifically, given a better 'default arguments thing', how would you interpret:
x = [2]
def f(x=x*5):
x.append(4)
With the earlier conversion it's:
x = [2]
def f(x=DefaultArg):
if x is DefaultArg:
x = x*5
x.append(4)
This isn't going to work because the x inside of f() is different than the outside x, and you'll get the error message I mentioned.
If you add a nonlocal, as in:
x = [2]
def f(x=DefaultArg):
nonlocal x
if x is DefaultArg:
x = x*5
x.append(4)
then you'll get "SyntaxError: name 'x' is parameter and nonlocal".
What other solution are you thinking of?
- syllogism 12y agoAh I see. Yeah nonlocal can't fix that.