开发者

access calling classes atrributes

I have 2 classes defined as such

class class1():
    self.stuff = 1
    def blah(self):
        foo = class2()
        foo.start()

class class2(threading.Threa开发者_Go百科d):
    def run(self):
       #access class1.stuff

How would I access class1.stuff from class2


It could look like this:

class class1(object):
    stuff = 1
    def blah(self):
        foo = class2()
        foo.start()

class class2(threading.Thread):
    def run(self):
       print(class1.stuff)

There is no special syntax to "access calling classes atrributes". If you want access to a object you must give it a visible name, for example by passing it to __init__ or by using the class object like this.


You would have to pass it into the function.

class class1():
    self.stuff = 1
    def blah(self):
        foo = class2()
        foo.start(self)

class class2(threading.Thread):
     def run(self, obj):
        obj.stuff;

There is no way to access another object's properties without having a reference to the object. The easiest way to obtain a reference to an object of class1 is to ask for it as an argument.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜