01-Python中函數(shù)和方法區(qū)別

一、函數(shù)和方法的認(rèn)知

首先摒棄錯(cuò)誤認(rèn)知:并不是類中的調(diào)用都叫方法.

接著上概念

函數(shù)

函數(shù)是封裝了一些獨(dú)立的功能,可以直接調(diào)用,能將一些數(shù)據(jù)(參數(shù))傳遞進(jìn)去進(jìn)行處理,然后返回一些數(shù)據(jù)(返回值),也可以沒有返回值??梢灾苯釉谀K中進(jìn)行定義使用。所有傳遞給函數(shù)的數(shù)據(jù)都是顯式傳遞的。

方法

方法和函數(shù)類似,同樣封裝了獨(dú)立的功能,但是方法是只能依靠類或者對象來調(diào)用的,表示針對性的操作。
方法中的數(shù)據(jù)self和cls是隱式傳遞的,即方法的調(diào)用者
方法可以操作類內(nèi)部的數(shù)據(jù)

簡單的說,函數(shù)在python中獨(dú)立存在,可直接使用的,而方法是必須被別人調(diào)用才能實(shí)現(xiàn)的。
靜態(tài)方法除外(與類和對象均無關(guān),通過類名和對象名均可被調(diào)用,屬函數(shù))

先來一段代碼:

def fun():
    pass
print(fun)

結(jié)果如下:

<function fun at 0x000002215AE5C268>

可以看出:

單獨(dú)定義的function都屬于函數(shù),就比如上面的fun()函數(shù)。

但在類中定義的function,就需要分情況來看了。

再看一段代碼:

class Apple:

    def fun1(self):
        return 'normal'

    @staticmethod
    def fun2():
        return 'staticmethod'

    @classmethod
    def fun3(cls):
        return 'classmethod'


print(Apple.fun1)
print(Apple.fun2)
print(Apple.fun3)

print("------------------------------------我是分割線-------------------------------------")

apple = Apple()
print(apple.fun1)
print(apple.fun2)
print(apple.fun3)

運(yùn)行結(jié)果如下:

<function Apple.fun1 at 0x0000025520CCD048>    # 函數(shù)
<function Apple.fun2 at 0x0000025520FB8620>    # 函數(shù)
<bound method Apple.fun3 of <class '__main__.Apple'>>    # 方法
------------------------------------我是分割線-------------------------------------
<bound method Apple.fun1 of <__main__.Apple object at 0x0000025520FB3C18>>  # 方法
<function Apple.fun2 at 0x0000025520FB8620>    # 函數(shù)
<bound method Apple.fun3 of <class '__main__.Apple'>>    # 方法

由此可以得出:

在一個(gè)類中

當(dāng)類直接調(diào)用function時(shí),
定義在@staticmethod下的func,如func2,和普通func,如func1屬于`函數(shù)`,
定義在@classmethod下的func,如func3,屬于`方法`(即稱為類方法)。
當(dāng)類實(shí)例化對象后,如`apple對象`,再調(diào)用function時(shí),
普通func,如fun1,就被稱為是`實(shí)例化方法`,
定義在@staticmethod下的func,與class和實(shí)例化對象無關(guān),所以依然屬于`函數(shù)`,
定義在@classmethod下的func,與class內(nèi)部有關(guān),屬于類的方法。

即:

  • @classmethod下定義的func屬于方法,@staticmethod下定義的func屬于函數(shù)。
  • 而類class中定義的普通func要分是類調(diào)用還是類對象調(diào)用
    1.類調(diào)用:函數(shù)
    2.類對象調(diào)用:方法

總結(jié):

  • 與類和實(shí)例無綁定關(guān)系的 func 都屬于函數(shù)(function); ---> 如:靜態(tài)函數(shù)和調(diào)用的普通func()
  • 與類和實(shí)例有綁定關(guān)系的 func 都屬于方法(method). ---> 如:類方法和類對象調(diào)用的普通func()

二、區(qū)分函數(shù)和方法

class Lwd(object):
    age = 12
    def __init__(self):
        self.name="ydd"
    def func(self):
        print("名字:" + self.name)
    def func2(self):
        print("年齡:" + str(age))

lwd = Lwd()
lwd.func()    # ydd
# 類直接調(diào)用func,需要傳入數(shù)據(jù)(參數(shù))
# 否則報(bào)錯(cuò): missing 1 required positional argument: self
Lwd.func(lwd)    # ydd

lwd.func2()      # 年齡:12
Lwd.func2(lwd)    # 年齡:12

另一段代碼:

from types import FunctionType, MethodType
print(isinstance(lwd.func, FunctionType))  # False
print(isinstance(lwd.func, MethodType))   # True   #說明這是一個(gè)方法

print(isinstance(Lwd.func, FunctionType))  # True   #說明這是一個(gè)函數(shù)
print(isinstance(Lwd.func, MethodType))  # False
@墨雨出品 必屬精品 如有雷同 純屬巧合
`非學(xué)無以廣才,非志無以成學(xué)!`
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容