当前位置: 移动技术网 > IT编程>脚本编程>Python > python之super的使用小结

python之super的使用小结

2018年08月18日  | 移动技术网IT编程  | 我要评论

世界男模,双强鹰王宠妻,徐州论坛

为什么需要super

在python没有引入super之前, 如果需要在子类中引用父类的方法, 一般写法如下:

class father:
 def whoami(self):
  print("i am father")


class child:
 def whoami(self):
  print("i am child")
  father.whoami(self)

这样看好像没什么问题, 就算没有super也能正常调用父类的方法, 但是如果有一天father类需要修改类名为father1, 那么子类child中也必须跟着修改.

想象下如果一个类有很多个子类, 这样一来我们就需要修改每个子类中引用父类的语句

怎么使用super

help on class super in module builtins:

class super(object)
 | super() -> same as super(__class__, <first argument>)
 | 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的帮助文档, 首先super是一个类, 它的调用方法有如下几种:

1.super()
2.super(type)
3.super(type, obj)
4.super(type, type2)

我们推荐用第一种方法来使用super, 因为它并不需要显式传递任何参数.但是要注意一点, super只能在新式类中使用.

class a:
 def __init__(self):
  print("this is a")

class b(a):
 def __init__(self):
  super().__init__()
  print("this is b")

b = b()

"""
this is a
this is b
"""

看起来super就像直接调用了b的父类a的__init__, 其实并不是这样的, 我们看看super在多继承下的使用

class a:
 def __init__(self):
  print("this is a")
  print("leave a")

class b(a):
 def __init__(self):
  print("this is b")
  super().__init__()
  print("leave b")

class c(a):
 def __init__(self):
  print("this is c")
  super().__init__()
  print("leave c")
 

class d(b, c):
 def __init__(self):
  print("this is d")
  super().__init__()
  print("leave d")  
  
d = d()

"""
this is d
this is b
this is c
this is a
leave a
leave c
leave b
leave d
"""

print(d.__mro__)
"""
(<class '__main__.d'>, 
<class '__main__.b'>, 
<class '__main__.c'>, 
<class '__main__.a'>, 
<class 'object'>)
"""

这里可以看到, 如果super只是简单调用父类的方法的话, 那么调用了b的__init__ 方法之后, 应该调用a的__init__ 才对, 但是调用的却是c的__init__ 方法

这是因为super调用的是当前类在mro列表中的后一个类, 而并不是简单地调用当前类的父类

python并没有强制性限制我们使用super调用父类, 我们还是可以使用原始的方法来调用父类中的方法, 但是需要注意的是调用父类的方法要统一, 即全用super或全不用super, 而用super 的话, 调用的方式也最好是统一的

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网