Python Function and Method
在Python中,Function和Method是有差別的,比如說,在 PIP 8 中就有分別定義Function和Method不同的Coding Style。
而Function和Method的主要差別在於是否有類別實例,說明如下:
Function
無類別實例。
例如:
def add(a, b):
return a + b
type(add)
執行結果:
function
Method
有類別實例。
例如:
class staff:
def __init__(self, id):
self.id = id
def get_id(self):
return self.id
s = staff('s001')
type(s.get_id)
執行結果:
method