很喜歡Python這門(mén)語(yǔ)言。在看過(guò)語(yǔ)法后學(xué)習(xí)了Django 這個(gè) Web 開(kāi)發(fā)框架。算是對(duì) Python 有些熟悉了。不過(guò)對(duì)里面很多東西還是不知道,因?yàn)橛玫纳佟=裉鞂W(xué)習(xí)了兩個(gè)魔術(shù)方法:__new__ 和 __init__。
開(kāi)攻:
如果對(duì) Python 有所簡(jiǎn)單了解的話應(yīng)該知道它包含類(lèi)這個(gè)概念的。語(yǔ)法如下:
class ClassName:
???
??????? .
??????? .??
??????? .
???
問(wèn)題來(lái)了。像我們學(xué)習(xí)的 C# 或是 Java 這些語(yǔ)言中,聲明類(lèi)時(shí),都是有構(gòu)造函數(shù)的。類(lèi)似下面這樣子:
public class ClassName{
??? public ClassName(){
??? }
}
當(dāng)然訪問(wèn)修飾符不一定非得 public ,這不是重點(diǎn)就不??嗦了。那 Python 中的構(gòu)造函數(shù)是怎樣的呢?我自己的理解是它是沒(méi)有構(gòu)造函數(shù)的。只不過(guò)在初始化時(shí)會(huì)調(diào)用一些內(nèi)部的可被改變的方法。比如:__new__ 和 __init__ 。從字面意思理解 __new__ 應(yīng)該會(huì)在 __init__ 之前執(zhí)行,實(shí)際查了資料后確實(shí)是如此的。官方文檔中關(guān)于 __init__ 方法有這樣一句話:
Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named __init__()
意思就是說(shuō)在創(chuàng)建類(lèi)時(shí),如果想指定的它的初始狀態(tài),那么可以通過(guò)定義一個(gè)指定名稱(chēng)為 __init__ 的方法去實(shí)現(xiàn)這樣的功能。這樣說(shuō)來(lái) __new__ 并不是官方推薦的初始化類(lèi)時(shí)要使用的方法。但是 __new__ 卻是在 __init__ 之前執(zhí)行的。官方文檔中對(duì) __init__ 介紹的第一句便是:當(dāng)創(chuàng)建實(shí)例時(shí)調(diào)用 __init__ 方法(Called when the instance is created.),后面又介紹說(shuō),如果想調(diào)用基類(lèi)的 __init__方法必須顯式的調(diào)用,只繼承基類(lèi)在初始化子類(lèi)時(shí)并不會(huì)自動(dòng)調(diào)用基類(lèi)的 __init__ 方法。到此應(yīng)該算是對(duì) __init__ 方法了解了。
下面我們看一下 __new__ 方法是怎么回事兒。先看一下官方文檔:
Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of __new__() should be the new object instance (usually an instance of cls).
Typical implementations create a new instance of the class by invoking the superclass's __new__() method using super(currentclass, cls).__new__(cls[, ...]) with appropriate arguments and then modifying the newly-created instance as necessary before returning it.
If __new__() returns an instance of cls, then the new instance's __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to __new__().
If __new__() does not return an instance of cls, then the new instance's __init__() method will not be invoked.
__new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.
從這里可以看出來(lái),這個(gè)方法才是產(chǎn)生類(lèi)的實(shí)例的地方。當(dāng)實(shí)例創(chuàng)建完成就調(diào)用 __init__ 方法,初始化類(lèi)的內(nèi)部狀態(tài)值。文檔中還提到 __new__ 方法其實(shí)是一個(gè)靜態(tài)方法,不用每次定義類(lèi)的時(shí)候都聲明這個(gè)方法,因?yàn)樵诎姹?2.4 之后 object 是所有對(duì)象的基類(lèi),而 __new__ 是定義在 object 對(duì)象內(nèi)部的靜態(tài)方法。
到這兒其實(shí)就差不多了,就按字面意思理解就可以。 __new__ 用于創(chuàng)建對(duì)象,而 __init__ 是在創(chuàng)建完成之后初始化對(duì)象狀態(tài)。前兩天看到一個(gè)有趣的方法,通過(guò)使用 __new__ 應(yīng)用了單例模式在對(duì)象身上。
注意點(diǎn):在類(lèi)繼承中,當(dāng)子類(lèi)和父類(lèi)都定義了自己的 __new__ 方法時(shí),那么會(huì)先調(diào)用子類(lèi)的 __new__ 方法再調(diào)用父類(lèi)的。這一點(diǎn)倒是和 C# 中的繼承是一樣的。其實(shí)仔細(xì)想想 Python 中只不過(guò)是把初始化和創(chuàng)建對(duì)象這兩個(gè)概念分開(kāi)了,而 C# 中即沒(méi)有這么干,只給了構(gòu)造函數(shù),開(kāi)發(fā)者可以自己看著辦。從這一點(diǎn)兒上說(shuō)我覺(jué)得 Python 的做法我更喜歡。
結(jié)束:
今天算是又學(xué)習(xí)到了新知識(shí),自己挺開(kāi)心的。再實(shí)踐實(shí)踐。。。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
