【Python】Python 新式類介紹

來源:互聯網
上載者:User

標籤:

本文轉載自:kaka_ace‘s blog

我們使用 Python 開發時, 會遇到 class A 和 class A(object) 的寫法,
這在 Python2 裡是有概念上和功能上的區別, 即經典類(舊式類)與新式類的區別,
英文上分別描述為 old-style(classic-style) 與 new-style.

通過搜尋, 先查閱了三個資料連結:
官方文檔
stackoverflow 解答
Python Types and Objects

根據 stackoverflow 答案引出的語言發明者 Guido 寫的一篇文章:
The Inside Story on New-Style Classes
其總結可以作為官方解釋:

  • low-level constructors named __new__() – 低層級的建構函式.
    Note: Python 的 class __init__ 並不是其他語言意義上的建構函式,
    在 new 建立執行個體後對執行個體屬性初始化的函數.

  • descriptors, a generalized way to customize attribute access – 描述符.
    或者說描述符協議支援.descriptor protocol __get__, __set__ ,__delete__ 等,
    可以閱讀 descriptor 文檔

  • static methods and class methods - 靜態方法和類方法

  • properties (computed attributes) – 屬性訪問 setter getter.

  • decorators (introduced in Python 2.4) – 裝飾器.
    現在裝飾器文法糖遍布各Python架構.

  • slots – 使用者佈建後可以限定執行個體的屬性.
    在 Python2 中替代 __dict__, 可以節省近 2/3 記憶體, Python3 中可以
    不因為最佳化記憶體使用量率而使用 slots, 因為 __dict__ 結構記憶體做了最佳化,
    Note: __dict__ 並不是 Python 意義上的內建的 dict, 其實是一個 proxy 類.

  • a new Method Resolution Order (MRO) – MRO 方法解析次序改變
    (由左遞迴改為C3演算法)

另一個答案增加了一點:

  • super added – super 方法支援

關於C3MRO介紹, 可以點選連結

以下是翻譯官方文檔的資料 (Python2的內容)

Classes and instances come in two flavors: old-style (or classic) and new-style.
類和執行個體有兩種方式: 舊式(或者經典) 和 新式類.

Up to Python 2.1 the concept of class was unrelated to the concept of type,
and old-style classes were the only flavor available. For an old-style class,
the statement x.__class__ provides the class of x, but type(x) is always
<type ‘instance’>. This reflects the fact that all old-style instances, independent
of their class, are implemented with a single built-in type, called instance.

一直到 Python 2.1 class 的概念不與 type 不一樣, 舊式類是唯一的風格. 對於一個
舊式類裡, 陳述句 x.__class__ 提供 x 的 class, 但是 type(x) 一直是 <type ‘instance’>.
它反應了所有舊式類執行個體, 獨立於它們的 class, 它們通過一個簡單的內建 type 實現,
被稱作 執行個體 instance.

New-style classes were introduced in Python 2.2 to unify the concepts of class
and type. A new-style class is simply a user-defined type, no more, no less.
If x is an instance of a new-style class, then type(x) is typically the same as
x.__class__ (although this is not guaranteed – a new-style class instance is
permitted to override the value returned for x.__class__).

新式類在Python 2.2 引入, 用於統一 class 和 type 的概念. 一個新式類簡化了使用者
自訂的 type, 不多不少. 如果 x 是一個新式類的執行個體 instance , type(x) 典型地
與 x.__class__ (儘管這並不能保證 – 一個新式類被允許重寫 x.__class__ 的值)一致.

 

eg:
>>> class A(object): pass
>>> a = A()
>>> a.__class__
>>> <class ‘__main__.A’>
>>>
>>> type(a)
>>> <class ‘__main__.A’>

而 class A 的 type 和 __class__ 是 <type ‘type’>

>>> A.__class__
>>> <type ‘type’>

The major motivation for introducing new-style classes is to provide a unified
object model with a full meta-model. It also has a number of practical benefits,
like the ability to subclass most built-in types, or the introduction of
“descriptors”, which enable computed properties.

引入新式類主要的改進是提供統一的帶有完整 meta-model 的 object model.
它可以擁有大量的使用價值, 類似於 內建 type的 subclass的能力 或者 描述符
descriptors 介紹(允許使用的計算屬性)

For compatibility reasons, classes are still old-style by default. New-style
classes are created by specifying another new-style class (i.e. a type) as a
parent class, or the “top-level type” object if no other parent is needed. The behaviour of new-style classes differs from that of old-style classes in
a number of important details in addition to what type() returns. Some of these
changes are fundamental to the new object model, like the way special methods
are invoked. Others are “fixes” that could not be implemented before for
compatibility concerns, like the method resolution order in case of multiple
inheritance.

考慮到相容的因素, class 使用預設是舊式類. 新式類通過指定的另一個新式類
(如 一個 type)作為父類, 或者是頂級類 object(如果沒有其他父類需要)建立.
除了 type() 的返回方式,新式類與舊式類的行為從大量重要細節區分. 這些變化
對於新式類 oject model 是基礎性的, 類似如 特別的方法被調用時的方式. 其他
特性屬於修複以前不能實現的相容性問題, 例如多重繼承下的 MRO 方法解析次序.

Tips:
1. 舊式類多重繼承使用的是左遞迴遍曆方式
2. 新式類多重繼承演算法使用 C3 演算法.

While this manual aims to provide comprehensive coverage of Python’s class
mechanics, it may still be lacking in some areas when it comes to its coverage
of new-style classes. Please see https://www.python.org/doc/newstyle/ for
sources of additional information.

該手冊目標是提供綜合性地覆蓋介紹 Python 的 class 機制, 在新式類的覆蓋
範圍內, 它可能依然會在某些方面缺少資訊. 請閱讀 https://www.python.org/doc/newstyle/
來獲額外的資訊源.

Old-style classes are removed in Python 3, leaving only new-style classes.
舊式類在 Python3 中去除, 只保留新式類.

原創文章,轉載請註明: 轉載自kaka_ace‘s blog

 

參考資料:

  1.http://www.zhihu.com/question/19754936

【Python】Python 新式類介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.