5

I have a class and two methods. One method gets input from the user and stores it in two variables, x and y. I want another method that accepts an input so adds that input to x and y. Like so:

class simpleclass(object):
    def getinput(self):
        x = input("input value for x: ")
        y = input("input value for y: ")
    def calculate(self, z):
        print(x + y + z)

When I run calculate(z) for some number z, it gives me errors saying the global variables x and y aren't defined.

How can calculate get access to the x and y values that were assigned in getinput?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
monolith
  • 61
  • 1
  • 1
  • 2
  • 1
    Does this answer your question? [How to share variables between methods in a class?](https://stackoverflow.com/questions/7670415/how-to-share-variables-between-methods-in-a-class) – outis Dec 19 '21 at 19:39
  • @outis it's close, but I decided this is the better version of the question to use as a canonical. – Karl Knechtel Oct 23 '22 at 03:45

5 Answers5

17

These need to be instance variables:

class simpleclass(object):
    def __init__(self):
        self.x = None
        self.y = None

    def getinput(self):
        self.x = input("input value for x: ")
        self.y = input("input value for y: ")
   
    def calculate(self, z):
        print(self.x + self.y + z)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
turtlebender
  • 1,907
  • 15
  • 16
5

You want to use self.x and self.y. Like so:

class simpleclass(object):
    def getinput(self):
        self.x = input("input value for x: ")
        self.y = input("input value for y: ")
    def calculate(self, z):
        print(self.x + self.y + z)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Li-aung Yip
  • 12,320
  • 5
  • 34
  • 49
3

x and y are local variables. They get destroyed when you move out from the scope of that function.

class simpleclass(object):
    def getinput(self):
        self.x = input("input value for x: ")
        self.y = input("input value for y: ")
    def calculate(self, z):
        print(int(self.x) + int(self.y) + z)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • Probably want to get a number from the string at some point if you're switching to raw_input. – DSM Mar 01 '12 at 16:36
2

Inside classes, there's a variable called self you can use:

class Example(object):
    def getinput(self):
        self.x = input("input value for x: ")
    def calculate(self, z):
        print(self.x + z)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Brendan Long
  • 53,280
  • 21
  • 146
  • 188
0
class myClass(object):
    def __init__(self):
        pass
    
    def helper(self, jsonInputFile):
        values = jsonInputFile['values']
        ip = values['ip']
        username = values['user']
        password = values['password']
        return values, ip, username, password
    
    def checkHostname(self, jsonInputFile):
       values, ip, username, password = self.helper(jsonInputFile)
       print(values)
       print('---------')
       print(ip)
       print(username)
       print(password)

The __init__ method initializes the class. The helper function just holds some variables/data/attributes and releases them to other methods when you call it. Here jsonInputFile is some JSON. The checkHostname method logs in to some device/server to check the hostname; but it needs ip, username and password for that, which it gets by calling the helper method.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Gajendra D Ambi
  • 3,832
  • 26
  • 30