一 函数名的使用
python中的函数可以像变量一样,动态创建,销毁,当参数传递,作为值返回,叫第一类对象.其他语言功能有限
1.1 函数名是个特殊的变量,可以当做变量赋值
def func():
print("有志者,事竟成,破釜沉舟,百二秦关终属楚")
res = 4+3j
res = func
# res() = func()
# 把func当成变量赋值给res ,此刻res也成为了函数,调用函数需要在res后面加上()
res()
执行
[root@node10 python]# python3 test.py
有志者,事竟成,破釜沉舟,百二秦关终属楚
1.2 函数名可以作为容器类型数据的元素
def func1():
print(11)
def func2():
print(22)
def func3():
print(33)
lst = [func1,func2,func3]
print(lst)
# 循环调用列表当中的每一个函数
for i in lst:
i()
执行
[root@node10 python]# python3 test.py
[, , ]
11
22
33
1.3 函数名可以作为函数的参数
def func1(func):
# 函数的调用处
res = func()
print(res)
# 函数的定义处
def func2():
return 123
func1(func2)