4

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. When I run calculate(z) for some number z, it gives me errors saying the global variables x and y aren't defined. Obviously this means that the calculate method isn't getting access to x and y from getinput(). What am I doing wrong?

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
monolith
  • 51
  • 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

5 Answers5

16

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
turtlebender
  • 1,887
  • 14
  • 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
Li-aung Yip
  • 11,952
  • 5
  • 31
  • 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 = raw_input("input value for x: ")
            self.y = raw_input("input value for y: ")
    def calculate (self, z):
            print int(self.x)+int(self.y)+z
Karoly Horvath
  • 91,854
  • 11
  • 113
  • 173
  • 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
Brendan Long
  • 51,248
  • 19
  • 139
  • 179
0
class myClass(object):

    def __init__(self):

    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
       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 is a method written to log into some device/server and check the hostname but it needs ip, username and password for that and that is provided by calling the helper method.

Gajendra D Ambi
  • 3,280
  • 24
  • 25