[Python 基礎教學] 一切皆為物件,到底什麼是物件 Object ?

Python objects 物件是什麼

一. Python 一切皆為物件(Object)

Everything in python is object.
Classes, functions, and even simple data types, such as integer and float

不論是 class、 function、 int、 float… 在 Python 中一切都是物件 (Object)

首先我們看看 wiki 維基百科中,解釋什麼是物件 (Object):

In computer science, an object can be a variable, a data structure, a function, or a method, and as such, is a value in memory referenced by an identifier.

https://en.wikipedia.org/wiki/Object_(computer_science)

如果以白話文來解釋,什麼是物件 (Object) 的話:
有個我們每天幾乎都會用的名詞「東西」。東西可以代表著是一張桌子、椅子、電腦、或是身邊任何物品都可以叫東西。你可以試試看給個給東西一個白話文解釋看看…

上述物件就像是東西的例子,是我從這篇文章看到的,覺得很生活化,推薦大家閱讀:淺談 Python Metaclass

二. 物件可以擁有屬性 (Attributes) 和方法 (Methods)

Everything object can have attributes and methods.
* Attribute: A variable stored in an instance or class is called an attribute.
* Method: A function stored in an instance or class is called a method.

物件「可以」擁有屬性和方法,「可以」的意思代表不強制一定要有;而屬性 (Attributes) 代表存在 Object 裡的變數,方法 (Method) 則代表存在 Object 裡的 function。

以白話文來說,如果東西是一隻狗,則屬性就像是狗身上的顏色、年紀、性別、體重,而方法就像是狗的行為,例如會跑、會叫、會吃和睡。

而 Python 會將這些屬性和方法存在字典中,我們可以調用 dir(Demo)Demo.__dict__ 來查看。

三. 物件擁有型別 (datatype)

一但有了物件 (object) 存在,就一定會存在著型別 (datatype)。譬如說我們可以說這個東西是一個杯子、那個東西是一個椅子或這個東西是一隻狗等等,杯子、椅子和狗就是這些東西的型別。

在 Python 中我們可以使用 type 來查看物件 (object) 的型別:

數字的型別是 int、函式的型別是 function,但當我們嘗試把 type(int)type(object) 時卻會發現型別是 type 呢

接下來我們會在下一小節 Metaclass 中解釋為什麼 type 的型態會是 type。

四. 什麼是 Metaclass 元類

A metaclass is a class whose instances are classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances.
https://en.wikipedia.org/wiki/Metaclass

所有的 class 是由 Metaclass 所 instance 出來的。

In Python, the builtin class type is a metaclass.

在 Python 中 built-in 的 type 就是 Metaclass;也就是說 type 不只可以拿回傳型別外,還可被用來創造 class,這也解釋了為什麼 type(int)type(object) 會回傳的型別是 type 了。

舉個例子,當我們今天寫 class Human 時:

其實背後是 instance type 實例化而來type(class_name, class_base, attribute)

補張流程圖,或許會更清楚一些:

什麼是 Metaclass
  1. 當我們在寫 class Human,會透過 built-in type instance (圖右) 和繼承 class object (built-in object class 也是 type instance 出來的) (圖上) 所 create Human 出來 (圖中)。
  2. 當我們在 human_obj = Human() (圖左) 時,則是再 instance 剛剛由 type() 出的 Human。

推薦延伸閱讀: Understanding Metaclasses in Python

五. Python Object 實例化的過程

在上一小節,我們理解了class Human 是由 type 所 instance 出來。接下來我們將著重於 human_obj = Human() 的過程。

Python Object 實例化的過程

流程(1) 當呼叫 Human() 時,會使用 type class 裡的 __call__ method。

流程(2) type.__call__ 裡面首先會呼叫 __new__ method,如果 Human class 裡面有沒有定義 __new__ ,則會從 object class 中尋找並呼叫。

流程(3) 返回 __new__ 所 create 的 obj
流程(4) 返回後 type.__call__ 會呼叫 __init___ method 進行初始化
流程(5) 返回 __init__ 初始化後的 obj
流程(6) 最後 __call__ 返回 obj

最後希望這張圖的講解,能帶大家理解物件實例化過程中:使用(__call__)、建立(__new__)、初始化(__init__) 的過程,是由不同的方法來負責執行。

關於 Python 物件導向教學的延伸閱讀:

▍本站的其他相關教學:

▍本篇參考資料:

那 [Python 基礎] 什麼是物件 Object 結束囉,感謝收看!
如有任何問題,歡迎底下留言或私訊,我會盡快回覆您

在〈[Python 基礎教學] 一切皆為物件,到底什麼是物件 Object ?〉中有 1 則留言

  1. 太喜歡把Object解釋成「東西」這個概念了,之前都太執著這些細節,學程式學到卡在這⋯

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *