很喜歡Python這門語言。在看過語法后學習了Django 這個 Web 開發框架。算是對 Python 有些熟悉了。不過對里面很多東西還是不知道,因為用的少。今天學習了兩個魔術方法:__new__ 和 __init__。
開攻:
如果對 Python 有所簡單了解的話應該知道它包含類這個概念的。語法如下:
class ClassName:
???
??????? .
??????? .??
??????? .
???
問題來了。像我們學習的 C# 或是 Java 這些語言中,聲明類時,都是有構造函數的。類似下面這樣子:
public class ClassName{
??? public ClassName(){
??? }
}
當然訪問修飾符不一定非得 public ,這不是重點就不??嗦了。那 Python 中的構造函數是怎樣的呢?我自己的理解是它是沒有構造函數的。只不過在初始化時會調用一些內部的可被改變的方法。比如:__new__ 和 __init__ 。從字面意思理解 __new__ 應該會在 __init__ 之前執行,實際查了資料后確實是如此的。官方文檔中關于 __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__()
意思就是說在創建類時,如果想指定的它的初始狀態,那么可以通過定義一個指定名稱為 __init__ 的方法去實現這樣的功能。這樣說來 __new__ 并不是官方推薦的初始化類時要使用的方法。但是 __new__ 卻是在 __init__ 之前執行的。官方文檔中對 __init__ 介紹的第一句便是:當創建實例時調用 __init__ 方法(Called when the instance is created.),后面又介紹說,如果想調用基類的 __init__方法必須顯式的調用,只繼承基類在初始化子類時并不會自動調用基類的 __init__ 方法。到此應該算是對 __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.
從這里可以看出來,這個方法才是產生類的實例的地方。當實例創建完成就調用 __init__ 方法,初始化類的內部狀態值。文檔中還提到 __new__ 方法其實是一個靜態方法,不用每次定義類的時候都聲明這個方法,因為在版本 2.4 之后 object 是所有對象的基類,而 __new__ 是定義在 object 對象內部的靜態方法。
到這兒其實就差不多了,就按字面意思理解就可以。 __new__ 用于創建對象,而 __init__ 是在創建完成之后初始化對象狀態。前兩天看到一個有趣的方法,通過使用 __new__ 應用了單例模式在對象身上。
注意點:在類繼承中,當子類和父類都定義了自己的 __new__ 方法時,那么會先調用子類的 __new__ 方法再調用父類的。這一點倒是和 C# 中的繼承是一樣的。其實仔細想想 Python 中只不過是把初始化和創建對象這兩個概念分開了,而 C# 中即沒有這么干,只給了構造函數,開發者可以自己看著辦。從這一點兒上說我覺得 Python 的做法我更喜歡。
結束:
今天算是又學習到了新知識,自己挺開心的。再實踐實踐。。。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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