This doesn't work: (global name 'a' is not defined)
if True:
a = 3
else:
a = 4
print a
This works:
class A:
def f(self):
if True:
a = 3
else:
a = 4
print a
A().f()
This doesn't work: (global name 'a' is not defined)
class A:
def __init__(self):
a = 3
def f(self):
print a
A().f()
Why is it working in one case and not in another ? I thought that if it works in case (2) then it is because a
gets in the class context, but this doesn't make it work in (3)
I saw other discussions but I don't understand everything that is said and cases vary a bit.
EDIT:
example 1 does NOT work, at least in a python 2.7 interpreter. This is why it examples 1 and 2 are contradictory