網(wǎng)站首頁 編程語言 正文
函數(shù)的特性:
- 函數(shù)是對(duì)象
- 函數(shù)可以刪除
- 函數(shù)名字和函數(shù)本身完全是分開的
- 函數(shù)因?yàn)槭菍?duì)象,可以結(jié)合數(shù)據(jù)結(jié)構(gòu)使用
- 函數(shù)因?yàn)槭菍?duì)象,可以作為函數(shù)參數(shù)
- 函數(shù)因?yàn)槭菍?duì)象,可以在嵌套在函數(shù)體內(nèi)
- 內(nèi)層函數(shù)對(duì)象能夠記憶住父函數(shù)的變量
- 所有的函數(shù)都是對(duì)象,但是所有的對(duì)象并不一定都是函數(shù)
對(duì)象成為函數(shù)需要實(shí)現(xiàn)__call__協(xié)議
函數(shù)是對(duì)象:
# 函數(shù)是對(duì)象:支持賦值操作。 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)槭菍?duì)象,可以結(jié)合數(shù)據(jù)結(jié)構(gòu)使用:
# 函數(shù)因?yàn)槭菍?duì)象,就可以結(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)槭菍?duì)象,可以作為函數(shù)參數(shù):
# 函數(shù)因?yàn)槭菍?duì)象,就可以作為函數(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)槭菍?duì)象,可以在嵌套在函數(shù)體內(nèi):
# 函數(shù)因?yàn)槭菍?duì)象,可以在嵌套在函數(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ù)對(duì)象能夠記憶住父函數(shù)的變量:也稱為閉包
# 內(nèi)層函數(shù)對(duì)象能夠記憶住父函數(shù)的變量 def test7(n): def inner(m): return m + n return inner result7 = test7(7) print(result7(6)) # 13
所有的函數(shù)都是對(duì)象,但是所有的對(duì)象并不一定都是函數(shù):
# 所有的函數(shù)都是對(duì)象,但是所有的對(duì)象并不一定都是函數(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 所有的對(duì)象并不一定都是函數(shù)
對(duì)象成為函數(shù)需要實(shí)現(xiàn)__call__協(xié)議:
# 對(duì)象成為函數(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é)
原文鏈接:https://blog.csdn.net/hzblucky1314/article/details/122659958
相關(guān)推薦
- 2022-07-11 spring boot中動(dòng)態(tài)代理導(dǎo)致自定義注解掃描失敗以及解決辦法
- 2022-04-12 python?獲取list?長度_python
- 2022-06-01 C語言?超詳細(xì)介紹與實(shí)現(xiàn)線性表中的帶頭雙向循環(huán)鏈表_C 語言
- 2022-04-15 C語言各種操作符透徹理解上篇_C 語言
- 2023-10-25 更簡單的方法實(shí)現(xiàn)el-calendar日歷組件中點(diǎn)擊上個(gè)月、今天、下個(gè)月按鈕時(shí)的點(diǎn)擊事件
- 2022-06-21 C語言詳解如何實(shí)現(xiàn)帶頭雙向循環(huán)鏈表_C 語言
- 2022-08-06 使用Gorm操作Oracle數(shù)據(jù)庫踩坑記錄_Golang
- 2022-03-08 Qt?儀表盤的實(shí)現(xiàn)示例_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支