Python super 函式用法總結

2022-09-25 16:03:14 字數 4022 閱讀 3743

了解 super() 函式之前,我們首先要知道 super() 的用途是啥?

語法格式:

super([type[, object-or-type]])

函式描述:

返回乙個**物件,它會將方法呼叫委託給 type 的父類或兄弟類。

引數說明:

type —— 類,可選引數。object-or-type —— 物件或類,一般是 self,可選引數。

返回值:

super object —— **物件。

help 幫助資訊:

>>> help(super)

help on class super in module builtins:

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 s程式設計客棧uperclass 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)

... ...

首先我們看乙個最基本的子類呼叫父類方法的示例:

>>> class a:

def funxx(self):

print("執行 a 中的 funxx 方法 ... ...")

>>> class b(a):

def funxx(self):

a.funxx(self) # 通過類名呼叫父類中的同名方法,self 引數代表 b 類的例項物件 b

print("執行 b 中的 funxx 方法 ... ...")

>>> b = b()

>>> b.funxx()

執行 a 中的 funxx 方法 ... ...

執行 b 中的 funxx 方法 ... ...

使用 super() 函式來實現父類方法的呼叫:

>>> class a:

def funxx(self):

print("執行 a 中的 funxx 方法 ... ...")

>>> class b(a):

def funxx(self):

super().funxx()

print("執行 b 中的 funxx 方法 ... ...")

>>> b程式設計客棧 = b()

>>> b.funxx()

執行 a 中的 funxx 方法 ... ...

執行 b 中的 funxx 方法 ... ...

在help()的幫助資訊中,也說明了類中使用super()不帶引數的形式等同於super(__class__, )這種形式。這也是 python 2.x 和 python 3.x 關於super()的區別。

改寫之前的單繼承問題的**:

>>> class a:

def funxx(self):

print("執行 a 中的 funxx 方法 ... ...")

>>> class b(a):

def funxx(self):

super(b, self).funxx()

print("執行 b 中的 funxx 方法 ... ...")

>>> b = b()

>>> b.funxx()

執行 a 中的 funxx 方法 ... ...

執行 b 中的 funxx 方法 ... ...

示例:class a:

pass

class b(a):

pass

class c(a):

def funxx(self):

print("找到 funxx() 位於 c 中...")

class d(a):

pass

class e(b, c):

pass

class f(e, d):

def funff(self):

print("執行 f 中的 funff()...")

super(e, self).funxx()

print(f"f 類的 mro : ")

f = f()

f.funff()

執行結果:

f 類的 mro : (, , , , , , )

執行 f 中的 funff()...

找到 funxx() 位於 c 中...

重複呼叫問題 也稱 鑽石繼承問題 或 菱形圖問題。

先來看看普通呼叫方法在:

>>> class a:

def __init__(self):

print("列印屬性 a")

>>> class b(a):

def __init__(self):

print("列印屬性 b")

a.__init__(self)程式設計客棧

>>> class c(a):

def __init__(self):

print("列印屬性 c")

a.__init__(self)

>>> class d(b, c):

def __init__(self):

print("列印屬性 d")

b.__init__(self)

c.__init__(self)

>>> d = d()

列印屬性 d

列印屬性 b

列印屬性 a

列印屬性 c

列印屬性 a

接下來我們使用 super() 函式來呼叫:

>>> class a:

def __init__(self):

print("列印屬性 a")

>>> class b(a):

def __init__(self):

print("列印屬性 b")

super().__init__() # super() 等同於 super(b, self)

>>> class c(a):

def __init__(self):

print("列印屬性 c")

super().__init__() # super() 等同於 super(c, self)

>>> class d(b, c):

def __init__(self):

print("列印屬性 d")

super(d, self).__init__()

>>> d = d()

列印屬性 d

列印屬性 b

列印屬性 c

列印屬性 a

>>> class a:

def funxx(self):

print("...a...")

>>> class b(a):

def funxx(self):

print("...b...")

>>> sa = super(b)

>>> print(sa)

, null>

>>> print(type(sa))

可以看出 super(type) 返回的是乙個無效的物件,或者是未繫結的 super object。

Python super 函式簡介

python 內建函式 描述 super 函式是用於呼叫父類 超類 的乙個方法。super 是用來解決多重繼承問題的,直接用類名呼叫父類方法在使用單繼承的時候沒問題,但是如果使用多繼承,會涉及到查詢順序 mro 重複呼叫 鑽石繼承 等種種問題。mro 就是類的方法解析順序表,其實也就是繼承父類方法時...

Python super關鍵字用法

使用super關鍵字,會按照繼承順序執行相應類中的方法,在沒有多繼承的情況下,一般是執行父類 coding utf 8 usr bin python class counter object def init self super counter,self setattr counter 0 def...

python super用法及原理詳解

概念 super作為python的內建函式。主要作用如下 例項在單個繼承的場景下,一般使用super來呼叫基類來實現 下面是乙個例子 class mammal object def wugbzinit self,mammalname print mammalname,is a warm bloode...