函数对象

# _*_ coding: utf-8 _*_
# 函数是第一类对象,即函数的内存地址可以当作数据传递

def foo():
print('from foo')
# 1.变量值(值 = 函数的内存地址)可以被引用
# f= foo
# print(f) # <function foo at 0x1082ed2f0> 函数的内存地址
# f()
# foo()
# from foo
# from foo

# 2.可以当作参数传递给另外一个函数
def bar(x):
print(x)

bar(foo)
# <function foo at 0x103a832f0>

# 3.返回值可以是函数
def function(x):
return x
f = function(foo)
print(f)
# <function foo at 0x10060f2f0>

# 4.可以当作容器类型的元素
l = [foo,1]
print(l)
l[0]()
# [<function foo at 0x106c432f0>, 1]
# from foo
原文地址:https://www.cnblogs.com/OutOfControl/p/9709584.html