Python函數(shù)調(diào)用的幾種方式(類里面,類之間,類外面)
第一種 是在class內(nèi)部,方法之間的互相調(diào)用
舉一個(gè)簡(jiǎn)單的例子
class cat():
def __init__(self,age):
cat.age = age
def show_color(self):
print("The color of the cat is white")
def show_age(self):
self.show_color()
print("The age of the cat is "+str(self.age))
Ragdoll = cat(2)
Ragdoll.show_age()結(jié)果為

這里我只用實(shí)例Ragdoll調(diào)用了show_age方法,但因?yàn)樵趕how_age中調(diào)用了show_color方法,所以兩個(gè)方法最后都執(zhí)行了
類里面的方法想調(diào)用另外一個(gè)方法,需要在方法體中用self.方法名的方式來調(diào)用
同樣,類里面的方法想調(diào)用類的屬性,需要用self.屬性名的方式
第二種 是在類的外面,def函數(shù)之間的彼此調(diào)用
還是來看一個(gè)簡(jiǎn)單的例子
def show_num(num):
print(num)
def test(num):
if num>3:
show_num(num)
test(4)結(jié)果為

在類外面的函數(shù)之間,只需要函數(shù)名就可以調(diào)用彼此
另外要記得參數(shù)列表保持一致,如果你要調(diào)用的函數(shù)參數(shù)列表中有多個(gè)參數(shù),那么自身這個(gè)函數(shù)的參數(shù)列表中也要有那些參數(shù)
以下是錯(cuò)誤示范
def create_cat(color,age):
print("The age and color of the cat are"+str(age)+color)
def show_cat():
create_cat(color,age)
show_cat()結(jié)果是

正確的函數(shù)調(diào)用是這樣
def create_cat(color,age):
print("The age and color of the cat are "+str(age)+" and "+color)
def show_cat(color,age):
create_cat(color,age)
show_cat("white",3)結(jié)果是

第三種 是在類外面的函數(shù),調(diào)用在類里面的方法
class cat():
def __init__(self,age):
self.age = age
def age_increase(self):
self.age += 1
print("the age of the cat is "+str(self.age))
def year():
Ragdoll = cat(3)
Ragdoll.age_increase()
year()結(jié)果為

創(chuàng)建該類的實(shí)例對(duì)象,再通過實(shí)例調(diào)用方法,最后運(yùn)行函數(shù)即可
class cat():
def __init__(self,age):
self.age = age
self.color = "white"
def age_increase(self):
self.age += 1
print("the age of the cat is "+str(self.age))
def year():
Ragdoll = cat(3)
Ragdoll.age_increase()
print(Ragdoll.color)
year() 在函數(shù)中直接用實(shí)例調(diào)用屬性,獲取屬性值也是可以的結(jié)果為

第四種 是不同的類之間的方法的彼此調(diào)用
class cat():
def __init__(self,age1):
self.age1 = age1
def show_age(self):
print("the age of the cat is "+str(self.age1))
class dog():
def __init__(self,age2):
self.age2 = age2
def show_cat_age(self):
Ragdoll = cat(3)
Ragdoll.show_age()
Husky = dog(2)
Husky.show_cat_age()結(jié)果為

而如果想在這個(gè)類里面調(diào)用其他類里面的屬性值,則需要這樣做
class cat():
def __init__(self,age1):
self.age1 = age1
self.color = "white"
def show_age(self):
print("the age of the cat is "+str(self.age1))
class dog():
def __init__(self,age2,Ragdoll):
self.age2 = age2
self.Ragdoll = Ragdoll
def show_cat_age(self,Ragdoll):
Ragdoll = cat(3)
Ragdoll.show_age()
print(self.Ragdoll.color)
if self.Ragdoll.age1 > self.age2:
print("the cat is older than the dog")
def run():
Ragdoll = cat(3)
Husky = dog(2,Ragdoll)
Husky.show_cat_age(Ragdoll)
run()結(jié)果為

需要將對(duì)象作為參數(shù)放入類里面
到此這篇關(guān)于Python函數(shù)調(diào)用的幾種方式(類里面,類之間,類外面)的文章就介紹到這了,更多相關(guān)Python函數(shù)調(diào)用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Pycharm中python調(diào)用另一個(gè)文件類或者函數(shù)
- python中類的相互調(diào)用的實(shí)踐
- Python類的定義繼承調(diào)用比較方法技巧
- python 子類調(diào)用父類的構(gòu)造函數(shù)實(shí)例
- Python實(shí)現(xiàn)子類調(diào)用父類的初始化實(shí)例
- python 中不同包 類 方法 之間的調(diào)用詳解
- python 定義類時(shí),實(shí)現(xiàn)內(nèi)部方法的互相調(diào)用
- python調(diào)用函數(shù)、類和文件操作簡(jiǎn)單實(shí)例總結(jié)
- 調(diào)用其他python腳本文件里面的類和方法過程解析
- Python中不同類之間調(diào)用方法的四種方式小結(jié)
相關(guān)文章
python實(shí)現(xiàn)反轉(zhuǎn)部分單向鏈表
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)反轉(zhuǎn)部分單向鏈表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
python中的classmethod與staticmethod
這篇文章主要介紹了python中的classmethod與staticmethod,2022-01-01
一行代碼解決動(dòng)態(tài)執(zhí)行Python函數(shù)方法實(shí)例
這篇文章主要為大家介紹了如何用一行代碼解決動(dòng)態(tài)執(zhí)行Python函數(shù)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
pytorch利用Dataset讀取數(shù)據(jù)報(bào)錯(cuò)問題及解決
這篇文章主要介紹了pytorch利用Dataset讀取數(shù)據(jù)報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
教你利用python如何讀取txt中的數(shù)據(jù)
們使用python的時(shí)候經(jīng)常需要讀取txt文件中的內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于利用python如何讀取txt中數(shù)據(jù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
基于高德地圖API在Python中實(shí)現(xiàn)地圖功能的方法示例詳解
本文介紹在高德開放平臺(tái)中,申請(qǐng)、獲取地圖API的Key的方法,同時(shí)通過簡(jiǎn)單的Python代碼,調(diào)取API信息,對(duì)所得Key的可用性加以驗(yàn)證,感興趣的朋友一起看看吧2025-01-01
詳解python實(shí)現(xiàn)數(shù)據(jù)歸一化處理的方式:(0,1)標(biāo)準(zhǔn)化
這篇文章主要介紹了詳解python實(shí)現(xiàn)數(shù)據(jù)歸一化處理的方式:(0,1)標(biāo)準(zhǔn)化,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
使用Python爬了4400條淘寶商品數(shù)據(jù),竟發(fā)現(xiàn)了這些“潛規(guī)則”
這篇文章主要介紹了使用Python爬了4400條淘寶商品數(shù)據(jù),竟發(fā)現(xiàn)了這些“潛規(guī)則”,筆者用 Python 爬取淘寶某商品的全過程,并對(duì)商品數(shù)據(jù)進(jìn)行了挖掘與分析,最終得出結(jié)論。需要的朋友可以參考下2018-03-03
用Python調(diào)用win命令行提高工作效率的實(shí)例
今天小編就為大家分享一篇用Python調(diào)用win命令行提高工作效率的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08

