How can I set a class variable from inside a function inside another function?
var.py
class A:
def __init__(self):
self.a = 1
self.b = 2
self.c = 3
def seta(self):
def afunction():
self.a = 4
afunction()
def geta(self):
return self.a
run.py
cA = A()
print cA.a
cA.seta()
print cA.a
print cA.geta()
python run.py
1
1
1
why does a not equal 4 and how can I make it equal 4?
Edit:
Thanks everyone – sorry, I just saw now. I accidentally was off by a _ in one of my names…. so my scope is actually all ok.
The problem is that there are multiple
selfvariables. The argument passed into your inner function overwrites the scope of the outer.You can overcome this by removing the
selfparameter from the inner function, and making sure you call that function in some way.