首页
技术
登录
6mi
u
盘
搜
搜 索
技术
Python笔记(29)装饰器之函数即变量
Python笔记(29)装饰器之函数即变量
技术
2026-04-10
4
#!/user/bin/env python
# -*- coding:utf-8 -*-
# author:berlin
# 实现装饰器知识储备:
#1、 函数即“变量”,有时候函数也相当于变量
# 2、高阶函数
# 3、嵌套函数
# 高阶函数+嵌套函数 => 装饰器
# 情况1
# def foo():
# print('in the foo')
# bar() #因函数bar未定义,所以会报错
# foo()
# 情况2
# def bar():
# print('in the bar')
#
# def foo():
# print('in the foo')
# bar()
# foo()
# 情况3
# def foo():
# print('in the foo')
# bar()
#
# def bar():
# print('in the bar')
#
# foo()
# 情况4
# def foo():
# print('in the foo')
# bar()
# foo()
#
# def bar(): #bar函数在调用之后,会报错
# print('in the bar')
# 总结:
# 如 变量x=2、函数foo、函数bar,若比喻是一个大房间中,那么x、foo、bar都会被分配到各自一个
# 小房间,变量值和函数体都处在房间中,而x、foo、bar都是各自房间的门牌号。当变量x(门牌号)
# 被删除了,即变量被python的内存回收机制回收了,那么函数foo、bar(门牌号)同样也没有了,
# 同理也会被回收,故实质上函数与变量一样,所以函数即“变量”。
转载请注明原文地址:https://ipadbbs.8miu.com/read-64114.html
最新回复
(
0
)