#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    60
    Rep Power
    10

    Calling a variable inside a method


    Hi all.

    I was wondering how i could use a method to get a value inside a method withint the same class.

    for example

    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?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    60
    Rep Power
    10
    heck. ill just post the 2 methods in case it helps


    Code:
        def __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"
            
    
        def __get_mood(self):
            unhappiness = self.hunger + self.boredom
            if unhappiness < 5:
                mood = "happy  :)"
            elif 5 <= unhappiness <= 10:
                mood = "okay..  :|"
            elif 11 <= unhappiness <= 15:
                mood = "frustrated! >:#"
            else:
                mood = "mad!! >:@"
            return mood
    Now as you can see, "unhappiness" is defined in __get_mood(self):, but i wanna use it in __pass_time(self):
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by play_
    heck. ill just post the 2 methods in case it helps


    Code:
        def __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"
            
    
        def __get_mood(self):
            unhappiness = self.hunger + self.boredom
            if unhappiness < 5:
                mood = "happy  :)"
            elif 5 <= unhappiness <= 10:
                mood = "okay..  :|"
            elif 11 <= unhappiness <= 15:
                mood = "frustrated! >:#"
            else:
                mood = "mad!! >:@"
            return mood
    Now as you can see, "unhappiness" is defined in __get_mood(self):, but i wanna use it in __pass_time(self):
    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.

    If you use self.unhappiness instead of unhappiness then you should be able to do it .

    Hope this helps,

    Mark.
    Last edited by netytan; March 5th, 2006 at 06:05 PM.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    60
    Rep Power
    10
    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." ?
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I mean that you need to use an instance variable and that instance variables begin with self.

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    618
    Rep Power
    65
    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 05:45 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    60
    Rep Power
    10
    got it.

    thanks guys.

IMN logo majestic logo threadwatch logo seochat tools logo