1 2 3 4 5 6 7 8 | class myThread(threading.Thread): def __init__(self,str1,str2): threading.Thread.__init__(self) self.str1 = str1 self.str2 = str2 def run(self): run1(self.str1,self.str2) |
我知道用_init__初始化一个类,但它在下一行的目的是什么?除此之外还有其他选择吗?
注意,您明确调用了
Python文档
There are two typical use cases for super. In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable. This use closely parallels the use of super in other programming languages.
The second use case is to support cooperative multiple inheritance in a dynamic execution environment.
派生类调用基类init有两个原因。一个原因是如果基类在它的
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Car(object): def __init__(self, color): self.color = color class SportCar(car): def __init__(self, color, maxspeed): super(SportCar, cls).__init__(self, color) self.maxspeed = maxspeed class MiniCar(car): def __init__(self, color, seats): super(MiniCar, cls).__init__(self, color) self.seats = seats |
这只是一个例子,但是您可以看到SportCar和MiniCar对象如何使用
这里发生的是,您从类
因此,
因此,在执行修改后的