美文网首页学习空间
Python之函数名称的应用

Python之函数名称的应用

作者: Joening | 来源:发表于2021-09-03 10:45 被阅读0次

函数名可以作为容器类数据类型元素
函数名可以作为函数的参数
函数名可以作为函数的返回值
函数名指向的是函数的内存地址

函数名指向的是函数的内存地址

def fun1():
    print('666')
print(fun1) #<function fun1 at 0x0000016E0AE9C1E0>

函数名就是变量

def fun1():
    print('666')

f = fun1
f1 = f
f2 = f1
f1() #666
f()  #666
f2() #666
fun1() #666
def fun1():
    print('in 6666')

def fun2():
    print('in 8888')

fun2 = fun1
fun2() #in 6666

函数名可以作为容器类数据类型元素

def fun1():
    print('in 6666')

def fun2():
    print('in 6666')

def fun3():
    print('in 6666')

lst = [fun1,fun2,fun3]
for i in lst:
    print(i)
    i() 
 
'''
<function fun1 at 0x0000027B0FEBC1E0>
in 6666
<function fun2 at 0x0000027B10055730>
in 6666
<function fun3 at 0x0000027B100556A8>
in 6666
'''

函数名可以作为函数的参数

def fun1():
    print('in 666')

def fun2(x):
    x()
    print('in 888')

fun2(fun1)

'''
in 666
in 888
'''

相关文章

网友评论

    本文标题:Python之函数名称的应用

    本文链接:https://www.haomeiwen.com/subject/ujzhwltx.html