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.
vvvvv
18.5k16 gold badges43 silver badges62 bronze badges
asked Jan 16, 2013 at 21:35
2
The problem is that there are multiple self
variables. The argument passed into your inner function overwrites the scope of the outer.
You can overcome this by removing the self
parameter from the inner function, and making sure you call that function in some way.
class A:
def __init__[self]:
self.a = 1
self.b = 2
self.c = 3
def seta[self]:
def afunction[]: # no self here
self.a = 4
afunction[] # have to call the function
def geta[self]:
return self.a
answered Jan 16, 2013 at 21:43
As others have mentioned, afunction
is never called. You could do something like this:
class A:
def __init__[self]:
self.a = 1
def seta[self]:
def afunction[self]:
self.a = 4
afunction[self]
def geta[self]:
return self.a
a = A[]
print a.a
a.seta[]
print a.a
Here we actually call afunction
and explicitly pass it self
, but this is a rather silly way to set the attribute a
-- especially when we can do it explicitly without the need for getters or setters: a.a = 4
Or you could return
the
function:
def seta[self]:
def afunction[]: #Don't need to pass `self`. It gets picked up from the closure
self.a = 4
return afunction
and then in the code:
a = A[]
a.seta[][] #the first call returns the `afunction`, the second actually calls it.
answered Jan 16, 2013 at 21:41
mgilsonmgilson
288k60 gold badges601 silver badges673 bronze badges
Inside seta
, you define a function
def afunction[self]:
self.a = 4
...that would set self.a
to 4 if it would ever be called. But it's not called anywhere, so a
is unchanged.
answered Jan 16, 2013 at 21:38
Anton KovalenkoAnton Kovalenko
20.4k2 gold badges35 silver badges68 bronze badges
As several others have said, you need to actually call functiona at some point. Comments won't let me type this intelligably so here's an answer:
def seta[self]:
def functiona[self]: #defined
self.a = 4
functiona[] #called
answered Jan 16, 2013 at 21:43
Charlie MartinCharlie Martin
108k23 gold badges192 silver badges258 bronze badges
2
How can you make it equate to 4:
class A:
def __init__[self]:
self.a = 1
self.b = 2
self.c = 3
def seta[self]:
##def afunction[self]: [remove this]
self.a = 4
def geta[self]:
return self.a
Tricky part: Why does is not equate to 4...
Currently a is set to 4 only via "afunction". Since afunction is never called it never executes.. The seta has "afunction" nested inside but not called... similar to member variables within a classs.
answered Jan 16, 2013 at 21:47
spookyspooky
1,55412 silver badges15 bronze badges