from objprint import op
class Person:
def __init__(self, name):
self.name = name
class Male(Person):
def __init__(self, name):
super().__init__(name)
self.gender = "male"
m = Male("Peter")
op(m)
输出结果
super本质
super是一个class,类型为type
,是一个内置类的名字
super()不是调用了一个函数,而是建立了一个super的对象
super的完整版
class super(object):
"""
super() -> same as super(__class__, )
super(type) -> unbound super object
super(type, obj) -> bound super object; requires isinstance(obj, type)
super(type, type2) -> bound super object; requires issubclass(type2, type)
Typical use to call a cooperative superclass method:
class C(B):
def meth(self, arg):
super().meth(arg)
This works for class methods too:
class C(B):
@classmethod
def cmeth(cls, arg):
super().cmeth(arg)
"""
super的第一个参数是一个type,第二个参数是一个type或object,第二个参数决定了这个super()的点后面的函数bind到哪一个object或class上。同时,第二个参数决定了使用哪一个**MRO**,第一个参数决定在该MRO链上从哪个class往后找(不包括这个class)。
举例
from objprint import op
class Animal:
def __init__(self, age):
self.age = age
class Person(Animal):
def __init__(self, age, name):
super().__init__(age)
self.name = name
class Male(Person):
def __init__(self, age, name):
super().__init__(age, name)
self.gender = "male"
m = Male(34, "Peter")
op(m)
输出
from objprint import op
class Animal:
def __init__(self, age):
self.age = age
class Person(Animal):
def __init__(self, age, name):
super().__init__(age)
self.name = name
class Male(Person):
def __init__(self, age, name):
super(Person, self).__init__(age, name)
self.gender = "male"
m = Male(34, "Peter")
op(m)
输出
TypeError: Animal.__init__() takes 2 positional arguments but 3 were given
当前的MRO链为:Male->Person->Animal->Object
所以会调用Person类的父类——Animal类的__init__
函数
from objprint import op
class Animal:
def __init__(self, age):
self.age = age
class Person(Animal):
def __init__(self, age, name):
super().__init__(age)
self.name = name
class Male(Person):
def __init__(self, age, name):
super(Person, self).__init__(age)
self.gender = "male"
m = Male(34, "Peter")
super(Male, m).__init__(12,"pepper")
op(m)
结果
只传一个参数给super
只传给super一个object的时候,它会变成一个unbound的对象,需要把它bind到别的object上才可以使用
助于深入理解的例子class A:
def say(self):
print("A")
class B(A):
def say(self):
super().say()
class C(A):
def say(self):
print("C")
class M(B, C):
def say(self):
B.say(self)
m = M()
m.say()
打印结果是:C
为什么?在调用say()方法的时候,用的self是M的self,即当前的MRO链是M的MRO链:M->B->C->A,寻找方法的顺序是由self的MRO决定的,所以最先在C里找到say()方法
参考视频https://www.bilibili.com/video/BV1FL4y1E7xK
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)