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?

2
  • 1
    Does this answer your question? How to share variables between methods in a class? Commented Dec 19, 2021 at 19:39
  • @outis it's close, but I decided this is the better version of the question to use as a canonical. Commented Oct 23, 2022 at 3:45

5 Answers 5

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)
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for most correct answer illustrating use of the __init__() method.
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)

Comments

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)

1 Comment

Probably want to get a number from the string at some point if you're switching to raw_input.
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)

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.