-2

Problem Statement: How to update a global variable from within the same class like 'this' in Java ?

Code Example:

class XYZ(object):

    response = "Hi Pranjal!";

    def talk(response):
       self.response = response;          #Class attribute should be assiged Anand value!

    talk("Hi Anand!");
    print response;

Output Should be: Hi Anand!

Comments: ERROR! 'self' is not defined!

How to update the global variable (response) by calling that function(talk) within the same class. I understand for using self, I need to pass an object but I can't create an instance of the class inside the same class right? Help.

Pranzell
  • 2,275
  • 16
  • 21
  • Is your whitespace exactly as given in the question? – doctorlove Mar 02 '17 at 10:56
  • Yes, the indentation is correct. – Pranzell Mar 02 '17 at 10:57
  • Why do you have code on class level? Seeing that you have unneeded trailing semicolons in each line I can safely assume that you're new to Python. I think you have a wrong approach here. Please explain what you really want to do. BTW, `response` is not global. – Matthias Mar 02 '17 at 11:04
  • I have been coding in Java and yes I'm new to Python but I like the use of semicolons. Well, there is this class attribute (response) which I need to update in the function in the way shown above. Then I want to call that function from within the same class. I want to know how it can be done? – Pranzell Mar 02 '17 at 11:09
  • You're trying to code Python with Java concepts in mind. This won't work. In your `talk` function you can use `XYZ.response = response` but the whole approach seems strange. Again the question: Why do you think you need code on class level? Why can't you call `talk()` outside of the class? – Matthias Mar 02 '17 at 12:00
  • The error you're getting has to do with the fact that you are not passing `self` to `XYZ.talk()`, but even if you did you never assign a value to an instance's `response` and thus `self.response` would also be not defined. At the same time your call to that function should not be made within the definition of the class, but on an instance of that class created outside. See my answer bellow for several alternatives on what you can do. – berna1111 Mar 02 '17 at 13:11
  • Disregard "but even if you did you never assign a value to an instance's response and thus self.response would also be not defined." on previous comment, was thinking about `talk()` as the method that prints the response... You should have a method to assign the value and a method to print it out, create an instance of the class and then call those methods on that instance. – berna1111 Mar 02 '17 at 13:18

1 Answers1

1

There are different things that you could be trying to do, and you probably need to research a little bit more about python itself, but let's see if this helps:

If you want to have a class whose instances share a common response, you can have something like this:

class XYZ(object):
    response = "Hi Pranjal!"; # class atribute

    def change_talk(self, response):
        # class methods always get `self` (the object through which they are
        # called) as their first argument, and it's commons to call it `self`.
        XYZ.response = response  # change class atribute

    def talk(self):
        print XYZ.response

XYZ_inst = XYZ() # instance of the class that will have the class methods
XYZ_inst.talk() # instance "talks"
#"Hi Pranjal!"
XYZ_inst.change_talk("Hi Anand!");
XYZ_inst.talk()
#"Hi Anand!"

XYZ_inst_2 = XYZ()
## New instance has the same response as the previous one:
XYZ_inst_2.talk()
#"Hi Anand!"
## And changing its response also changes the previous:
XYZ_inst_2.change_talk("Hi Bob!")
XYZ_inst_2.talk()
#"Hi Bob!"
XYZ_inst.talk()
#"Hi Bob!"

On the other hand, if you want each instance to have its own response (see more about __init__):

class XYZ2(object):
    # you must initialise each instance of the class and give
    # it its own response:
    def __init__(self, response="Hi Pranjal!"):
        self.response = response

    def change_talk(self, response):
        self.response = response;

    def talk(self):
        print self.response

XYZ_inst = XYZ2() # object that will have the class methods
XYZ_inst.talk()
#"Hi Pranjal!"
XYZ_inst.change_talk("Hi Anand!");
XYZ_inst.talk()
#"Hi Anand!"

XYZ_inst_2 = XYZ2()
## new instance has the default response:
XYZ_inst_2.talk()
#"Hi Pranjal!"
## and changing it won't change the other instance's response:
XYZ_inst_2.change_talk("Hi Bob!")
XYZ_inst_2.talk()
#"Hi Bob!"
XYZ_inst.talk()
#"Hi Anand!"

Finally, if you really want to have a global variable (which is not really advised as for changing whatever is outside the class instance you should pass it as an argument to the method changing it):

# global variable created outside the class:
g_response = "Hi Pranjal!"

class XYZ3(object):

    def change_talk(self, response):
        # just declare you are talking with a global variable
        global g_response
        g_response = response  # change global variable

    def talk(self):
        global g_response
        print  g_response


XYZ_inst = XYZ3() # object that will have the class methods
XYZ_inst.talk()
#"Hi Pranjal!"
XYZ_inst.change_talk("Hi Anand!");
XYZ_inst.talk()
#"Hi Anand!"
print g_response
#"Hi Anand!"
Community
  • 1
  • 1
berna1111
  • 1,811
  • 1
  • 18
  • 23
  • Thanks a lot for your input. But I think my doubt heads in a wrong direction. You have called that methods outside of the class. I wanted to call those methods from within the same class which I later figured out is meaningless. Thanks for the help. – Pranzell Mar 06 '17 at 06:37