不同类中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()

 

转载于:https://www.cnblogs.com/changwoo/p/9734284.html