Passing variables between methods in Python?









up vote
2
down vote

favorite












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









share|improve this question

















  • 1




    use raw_input
    – Karoly Horvath
    Mar 1 '12 at 16:33










  • @yi_H : could you please explain why raw_input() would be preferred over input()?
    – Li-aung Yip
    Mar 2 '12 at 6:06














up vote
2
down vote

favorite












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









share|improve this question

















  • 1




    use raw_input
    – Karoly Horvath
    Mar 1 '12 at 16:33










  • @yi_H : could you please explain why raw_input() would be preferred over input()?
    – Li-aung Yip
    Mar 2 '12 at 6:06












up vote
2
down vote

favorite









up vote
2
down vote

favorite











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









share|improve this question













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






python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 1 '12 at 16:31









monolith

1612




1612







  • 1




    use raw_input
    – Karoly Horvath
    Mar 1 '12 at 16:33










  • @yi_H : could you please explain why raw_input() would be preferred over input()?
    – Li-aung Yip
    Mar 2 '12 at 6:06












  • 1




    use raw_input
    – Karoly Horvath
    Mar 1 '12 at 16:33










  • @yi_H : could you please explain why raw_input() would be preferred over input()?
    – Li-aung Yip
    Mar 2 '12 at 6:06







1




1




use raw_input
– Karoly Horvath
Mar 1 '12 at 16:33




use raw_input
– Karoly Horvath
Mar 1 '12 at 16:33












@yi_H : could you please explain why raw_input() would be preferred over input()?
– Li-aung Yip
Mar 2 '12 at 6:06




@yi_H : could you please explain why raw_input() would be preferred over input()?
– Li-aung Yip
Mar 2 '12 at 6:06












5 Answers
5






active

oldest

votes

















up vote
12
down vote













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





share|improve this answer
















  • 1




    +1 for most correct answer illustrating use of the __init__() method.
    – Li-aung Yip
    Mar 1 '12 at 16:38

















up vote
5
down vote













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





share|improve this answer




















  • @DSM: D'oh! Good catch. Edited to fix.
    – Li-aung Yip
    Mar 1 '12 at 16:37

















up vote
2
down vote













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





share|improve this answer



























    up vote
    2
    down vote













    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





    share|improve this answer






















    • Probably want to get a number from the string at some point if you're switching to raw_input.
      – DSM
      Mar 1 '12 at 16:36










    • @DSM: right. thx
      – Karoly Horvath
      Mar 1 '12 at 16:40

















    up vote
    0
    down vote













    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.






    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9520075%2fpassing-variables-between-methods-in-python%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      12
      down vote













      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





      share|improve this answer
















      • 1




        +1 for most correct answer illustrating use of the __init__() method.
        – Li-aung Yip
        Mar 1 '12 at 16:38














      up vote
      12
      down vote













      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





      share|improve this answer
















      • 1




        +1 for most correct answer illustrating use of the __init__() method.
        – Li-aung Yip
        Mar 1 '12 at 16:38












      up vote
      12
      down vote










      up vote
      12
      down vote









      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





      share|improve this answer












      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






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 1 '12 at 16:33









      turtlebender

      1,7271316




      1,7271316







      • 1




        +1 for most correct answer illustrating use of the __init__() method.
        – Li-aung Yip
        Mar 1 '12 at 16:38












      • 1




        +1 for most correct answer illustrating use of the __init__() method.
        – Li-aung Yip
        Mar 1 '12 at 16:38







      1




      1




      +1 for most correct answer illustrating use of the __init__() method.
      – Li-aung Yip
      Mar 1 '12 at 16:38




      +1 for most correct answer illustrating use of the __init__() method.
      – Li-aung Yip
      Mar 1 '12 at 16:38












      up vote
      5
      down vote













      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





      share|improve this answer




















      • @DSM: D'oh! Good catch. Edited to fix.
        – Li-aung Yip
        Mar 1 '12 at 16:37














      up vote
      5
      down vote













      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





      share|improve this answer




















      • @DSM: D'oh! Good catch. Edited to fix.
        – Li-aung Yip
        Mar 1 '12 at 16:37












      up vote
      5
      down vote










      up vote
      5
      down vote









      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





      share|improve this answer












      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






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 1 '12 at 16:32









      Li-aung Yip

      9,38142241




      9,38142241











      • @DSM: D'oh! Good catch. Edited to fix.
        – Li-aung Yip
        Mar 1 '12 at 16:37
















      • @DSM: D'oh! Good catch. Edited to fix.
        – Li-aung Yip
        Mar 1 '12 at 16:37















      @DSM: D'oh! Good catch. Edited to fix.
      – Li-aung Yip
      Mar 1 '12 at 16:37




      @DSM: D'oh! Good catch. Edited to fix.
      – Li-aung Yip
      Mar 1 '12 at 16:37










      up vote
      2
      down vote













      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





      share|improve this answer
























        up vote
        2
        down vote













        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





        share|improve this answer






















          up vote
          2
          down vote










          up vote
          2
          down vote









          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





          share|improve this answer












          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 1 '12 at 16:33









          Brendan Long

          39.2k10113152




          39.2k10113152




















              up vote
              2
              down vote













              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





              share|improve this answer






















              • Probably want to get a number from the string at some point if you're switching to raw_input.
                – DSM
                Mar 1 '12 at 16:36










              • @DSM: right. thx
                – Karoly Horvath
                Mar 1 '12 at 16:40














              up vote
              2
              down vote













              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





              share|improve this answer






















              • Probably want to get a number from the string at some point if you're switching to raw_input.
                – DSM
                Mar 1 '12 at 16:36










              • @DSM: right. thx
                – Karoly Horvath
                Mar 1 '12 at 16:40












              up vote
              2
              down vote










              up vote
              2
              down vote









              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





              share|improve this answer














              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






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 1 '12 at 16:39

























              answered Mar 1 '12 at 16:34









              Karoly Horvath

              77.6k1090155




              77.6k1090155











              • Probably want to get a number from the string at some point if you're switching to raw_input.
                – DSM
                Mar 1 '12 at 16:36










              • @DSM: right. thx
                – Karoly Horvath
                Mar 1 '12 at 16:40
















              • Probably want to get a number from the string at some point if you're switching to raw_input.
                – DSM
                Mar 1 '12 at 16:36










              • @DSM: right. thx
                – Karoly Horvath
                Mar 1 '12 at 16:40















              Probably want to get a number from the string at some point if you're switching to raw_input.
              – DSM
              Mar 1 '12 at 16:36




              Probably want to get a number from the string at some point if you're switching to raw_input.
              – DSM
              Mar 1 '12 at 16:36












              @DSM: right. thx
              – Karoly Horvath
              Mar 1 '12 at 16:40




              @DSM: right. thx
              – Karoly Horvath
              Mar 1 '12 at 16:40










              up vote
              0
              down vote













              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.






              share|improve this answer
























                up vote
                0
                down vote













                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.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  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.






                  share|improve this answer












                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 28 '17 at 14:07









                  Gajendra D Ambi

                  8311013




                  8311013



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9520075%2fpassing-variables-between-methods-in-python%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Syphilis

                      Darth Vader #20