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?