Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何更改父类的属性_Python_Oop - Fatal编程技术网

Python 如何更改父类的属性

Python 如何更改父类的属性,python,oop,Python,Oop,我有两节课。在面向对象方法中,我可以从低级类更改父属性。在python中,如何从其他类更改父类的变量?我所拥有的 class Concurrent( threading.Thread): def __init__(self): self.rec = Rec() self.rec.start() self.parentvar = None # I have change this variable self.secon

我有两节课。在面向对象方法中,我可以从低级类更改父属性。在python中,如何从其他类更改父类的变量?我所拥有的

class Concurrent( threading.Thread):
    def __init__(self):
         self.rec = Rec()
         self.rec.start()
         self.parentvar = None # I have change this variable
         self.secondParentVar = [] # or use this

class Rec(Concurrent):
    def run(self):
        # from here, change variable of the parent Conccurent class variable

我认为这样做是可行的:

class Concurrent( threading.Thread):
    def __init__(self):
        self.rec = Rec()
        self.rec.start()
        self.parentvar = None 

class Rec(Concurrent):
    def __init__(self):
        Parent.__init__(self)
        self.parentvar = #new variable
        self.secondParentVar = [list1, list2]      

您可能想要的是:

class Rec(Concurrent):
    def run(self):
        self.parentvar = "new value"

您试图更改的父变量是什么?@user2396467通常,SO上的策略是让OP包含SSCCE。在Python中,您可以在任何地方更改任何内容。您尝试了什么?@lennartreegebro启动rec实例后,我尝试更改并发实例变量。我已经将print语句放在并发实例中,但print只显示第一个初始化的值,例如none。然后,您做了一些错误的事情,在您实际包含您尝试过的示例之前,我们不知道是什么。