不一样类中self的传递

 1 # 不一样类中self的传递
 2 
 3 class Foo(object):  4     def __init__(self, config):  # config是Cat中的self
 5         self.config = config  6 
 7     def eat(self):  8         print(self.config.name) # 打印Cat中的self.name
 9 
10     def run(self): 11         pass
12 
13 
14 class Cat(object): 15     def __init__(self): 16         self.name = ['tom', 'jerry', 'wangxiao'] 17 
18     def bark(self): 19         foo = Foo(self)  # 这里将self传到其余类中
20  foo.eat() 21 
22 cat = Cat() 23 cat.bark()
相关文章
相关标签/搜索