$ cat func.py
a = 'global'
def myfunc():
a = 'myfunc'
class myclass:
print('myclass a =', a)
# a = 'myclass'
myfunc()
$ python3.8 func.py
myclass a = myfunc
$ cat func.py
a = 'global'
def myfunc():
a = 'myfunc'
class myclass:
print('myclass a =', a)
a = 'myclass'
myfunc()
$ python3.8 func.py
myclass a = global
Which are the variable visibility rules applicable to the a attribute of myclass and how do they explain these results? References to documentation welcome, but not required.
I have been asked to explain how does Short description of the scoping rules? not answer my question here. The answer is ridiculously simple: that question and its answers do not address the case of a nested class, with one exception, which certainly does not explain the weird "global" output in my example, which directly contradicts the rule that the variable should be resolved to the closest nesting scope.