python方法的綁定和未綁定
1、說明
未綁定對(duì)象的方法:無(wú)self參數(shù)的方法,通過定義類調(diào)用函數(shù),返回未綁定self的方法。
綁定對(duì)象的方法:帶self參數(shù)的方法,即實(shí)例方法,通過實(shí)例對(duì)象調(diào)用函數(shù)返回綁定self參數(shù)的方法。
2、實(shí)例
##fn.py
classBoundClass:
defaction(self):
print("boundclassaction....")
"""
python3.x未綁定self對(duì)象的方法均是類定義的函數(shù),注意這個(gè)還不是屬于靜態(tài)方法和類方法
"""
defunbound(num):##workinpy3.x,failinpy2.x
print("thenumberis%d"%num)
>>>t=BoundClass()
>>>t.action()##action方法綁定實(shí)例對(duì)象self,可以直接通過點(diǎn)號(hào)運(yùn)算調(diào)用
>>>m=BoundClass.action##m是沒有綁定實(shí)例對(duì)象self的方法
>>>m(t)##調(diào)用需要傳遞實(shí)例對(duì)象t
以上就是python方法綁定和未綁定的介紹,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)教程請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。