I am trying to understand Python 3 variable scoping and nonlocal.
Consider the following function (it is just an example):
def build_property(something):
def deco(func):
def getter(self):
return getattr(self, something)
def setter(self, value):
setattr(self, something, value)
return property(getter, setter)
return deco
This works fine without nonlocal. But if now I want to conditionally create getters and setters depending on something I need nonlocal.
def build_property(something):
def deco(func):
nonlocal something # This is needed
if something.startswith('A'):
getter = None
else:
def getter(self):
return getattr(self, something)
if something.startswith('B'):
setter = None
else:
def setter(self, value):
setattr(self, something, value)
return property(getter, setter)
return deco
Why is nonlocal needed in one case, but not in the other? In other word, why something if correctly found in the first case (without nonlocal), but in the second I get: "UnboundLocalError: local variable 'something' referenced before assignment" if nonlocal is not present?
First:
nonlocalis not necessary in the code you've written. You're not changing the object thatsomethingpoints to.Second: There are cases where you would need to use
nonlocal. Below is some code wherenonlocalis necessary. Note that all of the assertions are correct (That is, they do not raise an AssertionError).Third: The code you've presented does not produce the error that you claim it does. If I remove the
nonlocalline, and call the following functions, I get no errors.