Python中super 方法的使用

2021-09-03 01:39:15 字數 672 閱讀 8585

先看一段**:

class stmatrix(object):#定義乙個類

"""docstring for stmatrix"""

def __init__(self, data, timestamps, t=24, checkcomplete=true):#建構函式

super(stmatrix, self).__init__()#super表繼承,這裡繼承自己

assert len(data) == len(timestamps)#如果不滿足程式就會中斷

self.data = data

self.timestamps = timestamps

self.t = t

self.pd_timestamps = string2timestamp(timestamps, t=self.t)

if checkcomplete:

self.check_complete()

# index

self.make_index()

關於super的用法:

比如人是乙個類,我們定義這個類的屬性有手,頭,當我們繼承人這個類(把人作為父類),可以發展自己的其他屬性,比如頭髮,眼睛。

定義乙個類後,如果呼叫這個類,首先會執行其建構函式,其他函式是否會執行還得呼叫,呼叫才會執行。

Python中super 方法的使用

如果在子類中也定義了構造器,既 init 函式,那麼基類的構造器該如何呼叫呢?方法 一 明確指定 使用乙個子類的例項去呼叫基類的構造器,在子類的構造器中明確的指明呼叫基類的構造器。class c p def init self p.init self print calling cs constru...

python中super 方法的理解

python中物件方法的定義很怪異,第乙個引數一般都命名為self 相當於其它語言的this 用於傳遞物件本身,有時候還會有乙個引數cls 相當於類名,當直接呼叫類方法的時候使用 python2中super 的用法 super class,self init python3中super 的用法 su...

python中super方法的注意點

之前,碰到乙個很有意思的問題,呼叫super之後,並沒有按照預期執行。大概是如下的 usr bin env python coding utf 8 class a object def init self print type self mro deftest self print a.test p...