python實(shí)例方法的使用注意及代碼實(shí)例
1、實(shí)例方法是從屬于實(shí)例對象的方法,定義實(shí)例方法時,第一個參數(shù)必須為 self。self 指當(dāng)前的實(shí)例對象。
2、調(diào)用實(shí)例方法是,不需要也不能給 self 傳值,self 由解釋器自動傳參。
實(shí)例
class getMin():
# 實(shí)例方法
def fun(self, arr, n):
print(arr[n-1])
# 類方法
@classmethod
def class_fun(cls):
print("this is class function")
if __name__ == "__main__":
arr = input().strip().split(" ")
int_arr = []
for item in arr:
int_arr.append(int(item))
n = int(input())
instance = getMin()
# 用實(shí)例調(diào)用實(shí)例方法
instance.fun(int_arr, n)
# 用類調(diào)用方法
getMin.fun(instance, int_arr, n)
# 實(shí)例調(diào)用類方法
instance.class_fun()
# 類調(diào)用類方法
getMin.class_fun()到此這篇關(guān)于python實(shí)例方法的使用注意及代碼實(shí)例的文章就介紹到這了,更多相關(guān)python實(shí)例方法的使用注意內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pytorch自己加載單通道圖片用作數(shù)據(jù)集訓(xùn)練的實(shí)例
今天小編就為大家分享一篇Pytorch自己加載單通道圖片用作數(shù)據(jù)集訓(xùn)練的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python使用tkinter實(shí)現(xiàn)小時鐘效果
這篇文章主要為大家詳細(xì)介紹了Python使用tkinter實(shí)現(xiàn)小時鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-02-02
利用Python第三方庫xlwt寫入數(shù)據(jù)到Excel工作表實(shí)例代碼
大家應(yīng)該都知道xlwt是python中寫入到excel的庫,下面這篇文章主要給大家介紹了關(guān)于利用Python第三方庫xlwt寫入數(shù)據(jù)到Excel工作表的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07

