Class Methods vs. Functions

Python beginners may feel confused about the differences between methods and functions. Now, let's compare methods and functions to help everyone better understand Python.

method vs function

Method Function
定義 與對象(Object)相關聯並對該對象(Object)的數據進行操作的一段代碼 一組能執行某些目的的語句。有用戶自定義Function,亦有不少內置Function(Internal link of function),例如print等等。
基本句法
共同之處 進行一些程序(Operation),並返回一些值
特性 與物件(Object)相關聯,因此可更改在類別的數據
可隱藏地傳遞給物件,不需一定return任何數據 若需傳遞參數,則需顯著地返回數據。
存在於類別(Class)中 不需類別(Class)去定義
包含“self”作為其第一個參數 不需包含“self”參數