欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中的__new__與__init__魔術(shù)方法理解筆記

 更新時(shí)間:2014年11月08日 13:13:15   投稿:junjie  
這篇文章主要介紹了Python中的__new__與__init__魔術(shù)方法理解筆記,需要的朋友可以參考下

很喜歡Python這門語(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)該知道它包含類這個(gè)概念的。語(yǔ)法如下:

復(fù)制代碼 代碼如下:

class ClassName:
    <statement - 1>:
        .
        .  
        .
    <statement - N>

問(wèn)題來(lái)了。像我們學(xué)習(xí)的 C# 或是 Java 這些語(yǔ)言中,聲明類時(shí),都是有構(gòu)造函數(shù)的。類似下面這樣子:

復(fù)制代碼 代碼如下:

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__ 方法有這樣一句話:

復(fù)制代碼 代碼如下:

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)建類時(shí),如果想指定的它的初始狀態(tài),那么可以通過(guò)定義一個(gè)指定名稱為 __init__ 的方法去實(shí)現(xiàn)這樣的功能。這樣說(shuō)來(lái) __new__ 并不是官方推薦的初始化類時(shí)要使用的方法。但是 __new__ 卻是在 __init__ 之前執(zhí)行的。官方文檔中對(duì) __init__ 介紹的第一句便是:當(dāng)創(chuàng)建實(shí)例時(shí)調(diào)用 __init__ 方法(Called when the instance is created.),后面又介紹說(shuō),如果想調(diào)用基類的 __init__方法必須顯式的調(diào)用,只繼承基類在初始化子類時(shí)并不會(huì)自動(dòng)調(diào)用基類的 __init__ 方法。到此應(yīng)該算是對(duì) __init__ 方法了解了。

下面我們看一下 __new__ 方法是怎么回事兒。先看一下官方文檔:

復(fù)制代碼 代碼如下:

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)生類的實(shí)例的地方。當(dāng)實(shí)例創(chuàng)建完成就調(diào)用 __init__ 方法,初始化類的內(nèi)部狀態(tài)值。文檔中還提到 __new__ 方法其實(shí)是一個(gè)靜態(tài)方法,不用每次定義類的時(shí)候都聲明這個(gè)方法,因?yàn)樵诎姹?2.4 之后 object 是所有對(duì)象的基類,而 __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):在類繼承中,當(dāng)子類和父類都定義了自己的 __new__ 方法時(shí),那么會(huì)先調(diào)用子類的 __new__ 方法再調(diào)用父類的。這一點(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í)踐。。。

相關(guān)文章

最新評(píng)論