首页 > 编程知识 正文

python类中函数,python类属性和实例属性

时间:2023-05-06 06:40:33 阅读:163203 作者:749

All the while I have been using:

无奈猫Class.__init__(self,*args,**kwargs )

myreasonisthatthisshowsexplicitlywhichsuperclassisused,especiallyinthecaseofmultipleinheritance。

However,other codes I came across use

super(myclass,self ).__init_ ) (args,**kwargs ) ) ) ) ) ) ) ) ) ) 65

国际空间站

thiscouldbecomeambigiouswhenit ' SuSE din :

class MyClass (无奈猫类1,无奈猫类2 ) :

def __init__(self,*args,**kwargs ) :

super(myclass,self ).__init_ ) ) Args,**kwargs ) #which无可奈何的猫Class is being used?

iwouldliketoknowwhythisformofcallingiswidelyadopted? Any advantage at all?

thereasonthatsuperisprefereableformodern (new style ) classesisthatitallowscooperativemultipleinheritance.here ' San example

class俊秀的乌龟(object ) :

. defdisplay(self ) :

.印入俊秀的乌龟'

.

class俊秀的乌龟2 (俊秀的乌龟) :

. defdisplay(self ) :

. print 'In俊秀乌龟2 '

. super (俊秀的乌龟2,self ).display ) ) ) )。

. print 'Back in俊秀的乌龟2 '

.

俊秀乌龟:

. defdisplay(self ) :

.打印' in bar '

. super(bar,self ).display )。

.打印' back in bar '

.

class俊秀的乌龟Bar (俊秀的乌龟2,Bar ) :

. pass

.

英俊的乌龟Bar ().display ) )。

英俊的乌龟2

In Bar

俊秀的乌龟

Back in Bar

拜占俊秀之龟2

class Bar俊秀的乌龟(Bar,俊秀的乌龟2 ) :

. pass

.

Bar俊秀的乌龟().display ) )。

In Bar

英俊的乌龟2

俊秀的乌龟

拜占俊秀之龟2

Back in Bar

notethatididn ' tdoanythingtochangethedisplaymethodonthesuperclassesbutigotdifferentdisplaymethodsonthesubclasssesbychangingthe rclasses.bar俊秀的乌龟and俊秀的乌龟barhavedifferentmethods.thisisbecausetheyhavedifferentmethodresolutionoresolution

Bar俊秀的乌龟. __mro__

(,)

俊秀的乌龟Bar.__mro__

(,)

thismeansthatsuperresolvestoadifferentclassforeachsubclassthatit ' scal ledin.thisallowsforeachoverridingmethodtochangeasmall veryothersuperclasscontributetothemethodcallaslongasthey ' rewillingtoplaynicely。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。