Python Function and Method

Python中,FunctionMethod是有差別的,比如說,在 PIP 8 中就有分別定義FunctionMethod不同的Coding Style

FunctionMethod的主要差別在於是否有類別實例,說明如下:

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

results matching ""

    No results matching ""