第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

關(guān)于python源對(duì)象object的__new__(),為什么是靜態(tài)方法?

關(guān)于python源對(duì)象object的__new__(),為什么是靜態(tài)方法?

DIEA 2019-05-09 22:19:56
classobject:"""Themostbasetype"""...@staticmethod#knowncaseof__new__def__new__(cls,*more):#knownspecialcaseofobject.__new__"""Createandreturnanewobject.Seehelp(type)foraccuratesignature."""pass...這里的__new__方法很奇怪,下面不是第一個(gè)參數(shù)還綁定了類(lèi)(也就是源對(duì)象object自身)嗎?我的問(wèn)題有幾個(gè):為什么不是類(lèi)方法?這是個(gè)靜態(tài)方法裝飾器的特例嗎?除了object里面,還有別的地方這么用嗎?期待大家解答
查看完整描述

2 回答

?
侃侃無(wú)極

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊

想通了,現(xiàn)列出個(gè)人的看法
作為python的源對(duì)象,object是所有類(lèi)對(duì)象的祖先(base)包括另一個(gè)源對(duì)象type。其實(shí)__new__方法的第一個(gè)參數(shù)只是恰好設(shè)置成了cls,并非要綁定某個(gè)實(shí)例對(duì)象,所以可以這么回答
是靜態(tài)方法,因?yàn)開(kāi)_new__()在這里只是一個(gè)綁定源對(duì)象object的普通方法,常規(guī)意義下__new__()的確是類(lèi)方法
不是,所有的靜態(tài)方法都只是綁定類(lèi)對(duì)象或者實(shí)例對(duì)象的普通方法
沒(méi)有,實(shí)際上是把cls這個(gè)普通的positionargument和類(lèi)方法當(dāng)中的代表類(lèi)對(duì)象的clspositionargument混淆了
需要補(bǔ)充的是,源對(duì)象類(lèi)型的實(shí)例也是類(lèi),是可以進(jìn)一步實(shí)例化的,常常作為某些框架的基礎(chǔ)數(shù)據(jù)類(lèi)型描述符,可以進(jìn)行強(qiáng)制類(lèi)型檢查,比如django的ORM的ModelBase,作為metaclass被所有model繼承,涉及元編程的一些東西
附上官方文檔的解釋?zhuān)约皊tackoverflow一些對(duì)這個(gè)東西的討論,很清晰,看來(lái)還是多看官方文檔更有用
1.documentation
object.__new__(cls[,...])
Calledtocreateanewinstanceofclasscls.__new__()isastaticmethod(special-casedsoyouneednotdeclareitassuch)thattakestheclassofwhichaninstancewasrequestedasitsfirstargument.Theremainingargumentsarethosepassedtotheobjectconstructorexpression(thecalltotheclass).Thereturnvalueof__new__()shouldbethenewobjectinstance(usuallyaninstanceofcls).Typicalimplementationscreateanewinstanceoftheclassbyinvokingthesuperclass’s__new__()methodusingsuper().__new__(cls[,...])withappropriateargumentsandthenmodifyingthenewly-createdinstanceasnecessarybeforereturningit.
If__new__()returnsaninstanceofcls,thenthenewinstance’s__init__()methodwillbeinvokedlike__init__(self[,...]),whereselfisthenewinstanceandtheremainingargumentsarethesameaswerepassedto__new__().
If__new__()doesnotreturnaninstanceofcls,thenthenewinstance’s__init__()methodwillnotbeinvoked.
__new__()isintendedmainlytoallowsubclassesofimmutabletypes(likeint,str,ortuple)tocustomizeinstancecreation.Itisalsocommonlyoverriddenincustommetaclassesinordertocustomizeclasscreation.
2.StackOverflow的討論
                            
查看完整回答
反對(duì) 回復(fù) 2019-05-09
  • 2 回答
  • 0 關(guān)注
  • 990 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)