一篇文章帶你深入學(xué)習(xí)Python函數(shù)
函數(shù)的特性:
- 函數(shù)是對象
- 函數(shù)可以刪除
- 函數(shù)名字和函數(shù)本身完全是分開的
- 函數(shù)因?yàn)槭菍ο?,可以結(jié)合數(shù)據(jù)結(jié)構(gòu)使用
- 函數(shù)因?yàn)槭菍ο?,可以作為函?shù)參數(shù)
- 函數(shù)因?yàn)槭菍ο?,可以在嵌套在函?shù)體內(nèi)
- 內(nèi)層函數(shù)對象能夠記憶住父函數(shù)的變量
- 所有的函數(shù)都是對象,但是所有的對象并不一定都是函數(shù)
對象成為函數(shù)需要實(shí)現(xiàn)__call__協(xié)議
函數(shù)是對象:
# 函數(shù)是對象:支持賦值操作。
def test1(n):
print(f"hello, {n}!")
# test1的函數(shù)名賦值給 new1
new1 = test1
new1("tom") # hello, tom!
函數(shù)可以刪除:
# 函數(shù)可以刪除
def test2(n):
print(f"hello, {n}!")
del test2
# test2("tom") # NameError: name 'test2' is not defined
函數(shù)名字和函數(shù)本身完全是分開的:刪除test3,不影響已經(jīng)賦值的new3的調(diào)用
# 函數(shù)名字和函數(shù)本身完全是分開的:刪除test3,不影響已經(jīng)賦值的調(diào)用
def test3(n):
print(f"hello, {n}!")
new3 = test3
del test3
new3("Jam") # hello, Jam!
函數(shù)因?yàn)槭菍ο螅梢越Y(jié)合數(shù)據(jù)結(jié)構(gòu)使用:
# 函數(shù)因?yàn)槭菍ο?,就可以結(jié)合數(shù)據(jù)結(jié)構(gòu)使用
def test4(n):
print(f"hello, {n}!")
data = [1, "a", {"name": "tom"}, test4]
for i in data:
from types import FunctionType
if type(i) == FunctionType:
test4("Lily") # hello, Lily!
函數(shù)因?yàn)槭菍ο螅梢宰鳛楹瘮?shù)參數(shù):
# 函數(shù)因?yàn)槭菍ο?,就可以作為函?shù)參數(shù)
def test5(n):
print("原始函數(shù)執(zhí)行")
print(f"hello, {n}")
def new5(n):
n("Golang")
new5(test5) # 原始函數(shù)執(zhí)行 hello, Golang
函數(shù)因?yàn)槭菍ο?,可以在嵌套在函?shù)體內(nèi):
# 函數(shù)因?yàn)槭菍ο?,可以在嵌套在函?shù)體內(nèi)
def test6(n):
def inner1(m):
new = m + "!!!"
return new
def inner2(m):
new = m + "***"
return new
if n > 3:
return inner2
else:
return inner1
result6 = test6(3)
print(result6("tom")) # tom!!!
內(nèi)層函數(shù)對象能夠記憶住父函數(shù)的變量:也稱為閉包
# 內(nèi)層函數(shù)對象能夠記憶住父函數(shù)的變量
def test7(n):
def inner(m):
return m + n
return inner
result7 = test7(7)
print(result7(6)) # 13
所有的函數(shù)都是對象,但是所有的對象并不一定都是函數(shù):
# 所有的函數(shù)都是對象,但是所有的對象并不一定都是函數(shù)
class A:
pass
a = A()
def test8():
pass
from types import FunctionType
print(isinstance(a, A)) # True
print(isinstance(test8, FunctionType)) # True
print(isinstance(a, FunctionType)) # False 所有的對象并不一定都是函數(shù)
對象成為函數(shù)需要實(shí)現(xiàn)__call__協(xié)議:
# 對象成為函數(shù)需要實(shí)現(xiàn)__call__協(xié)議
class B:
def __init__(self, n):
self.n = n
def __call__(self, m):
return self.n + m
b = B(9)
print(b(9)) # 18
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
python實(shí)現(xiàn)自動登錄人人網(wǎng)并采集信息的方法
這篇文章主要介紹了python實(shí)現(xiàn)自動登錄人人網(wǎng)并采集信息的方法,涉及Python模擬登陸及正則匹配的相關(guān)技巧,需要的朋友可以參考下2015-06-06
教你使用Pandas直接核算Excel中的快遞費(fèi)用
文中仔細(xì)說明了怎么根據(jù)賬單核算運(yùn)費(fèi).首先要確定運(yùn)費(fèi)規(guī)則,然后根據(jù)運(yùn)費(fèi)規(guī)則編寫代碼,生成核算列(快遞費(fèi) = 省份*重量),最后輸入賬單,進(jìn)行核算.將腳本件生成EXE文件,就可以使用啦,需要的朋友可以參考下2021-05-05
Django框架實(shí)現(xiàn)的普通登錄案例【使用POST方法】
這篇文章主要介紹了Django框架實(shí)現(xiàn)的普通登錄案例,結(jié)合實(shí)例形式分析了Django框架使用POST方法進(jìn)行頁面登錄、校驗(yàn)等相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
Python判斷一個三位數(shù)是否為水仙花數(shù)的示例
今天小編就為大家分享一篇Python判斷一個三位數(shù)是否為水仙花數(shù)的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
關(guān)于python pygame游戲進(jìn)行聲音添加的技巧
這篇文章主要給大家分享的是pygame游戲進(jìn)行聲音添加的方法,這文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!2021-10-10

