March 5th, 2006, 02:09 PM
Calling a variable inside a method
I was wondering how i could use a method to get a value inside a method withint the same class.
inside the method example1(self), a variable gets assigned the value of "jon"...let's call this variable 'var'
now, inside another method, i wanna 'print var'.
how would i do that?
March 5th, 2006, 02:12 PM
heck. ill just post the 2 methods in case it helps
Now as you can see, "unhappiness" is defined in __get_mood(self):, but i wanna use it in __pass_time(self):
self.hunger += 1
self.boredom += 1
if unhappiness > 15:
print "You suck as a citter owner I'm running away, bye!"
print self.name, "has commited suicide"
unhappiness = self.hunger + self.boredom
if unhappiness < 5:
mood = "happy :)"
elif 5 <= unhappiness <= 10:
mood = "okay.. :|"
elif 11 <= unhappiness <= 15:
mood = "frustrated! >:#"
mood = "mad!! >:@"
March 5th, 2006, 03:42 PM
Simply, you can't . Those variables are defined as being local to the method and can't be accessed. You can however access instance methods, those that are defined in the instances namespace – begin with self.
Originally Posted by play_
If you use self.unhappiness instead of unhappiness then you should be able to do it .
Hope this helps,
Last edited by netytan; March 5th, 2006 at 05:05 PM.
March 5th, 2006, 04:18 PM
Ok ill have to fiddle with this a bit. Thanks though.
what do you mean by " those that are defined in the instances namespace – begin with self." ?
March 5th, 2006, 04:39 PM
I mean that you need to use an instance variable and that instance variables begin with self.
March 5th, 2006, 04:42 PM
Look at it this way, the prefix 'self.' makes the variable available to all methods in a class. Notice that self is the first argument of any method within the class. You already have self.boredom and self.hunger, why not use self.unhappiness?
Last edited by Dietrich; March 5th, 2006 at 04:45 PM.
March 6th, 2006, 01:45 PM