如何从同一个类中更新全局变量?蟒蛇

时间:2021-11-12 23:05:57

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

问题陈述:如何在Java中像'this'这样的同一个类中更新全局变量?

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.

如何通过在同一个类中调用该函数(talk)来更新全局变量(响应)。我理解使用self,我需要传递一个对象但是我不能在同一个类中创建一个类的实例吗?救命。

1 个解决方案

#1


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:

你可能会尝试做不同的事情,你可能需要更多地研究python本身,但让我们看看这是否有帮助:

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__):

另一方面,如果您希望每个实例都有自己的响应(请参阅有关__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!"

#1


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:

你可能会尝试做不同的事情,你可能需要更多地研究python本身,但让我们看看这是否有帮助:

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__):

另一方面,如果您希望每个实例都有自己的响应(请参阅有关__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!"