函数在python中是first-class citizens:
1.能作为其他函数的参数被传递
2.能从其他函数中作为值返回
3.能够分配成变量且存储在数据结构中
示例:
Python 3.6.0 (default, Oct 21 2017, 01:22:56)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def myfunc(a,b):
... return a+b
...
>>> funcs=[myfunc]
>>> funcs[0]
<function myfunc at 0x7fdd6a27ae18>
>>> funcs[0](2,3)
5
【什么是First-class citizen?】–摘自first-class citizens wiki百科
In programming language design, a first-class citizen (also type, object, entity, or value) in a given programming language is an entity which supports all the operations generally available to other entities. These operations typically include being passed as an argument, returned from a function, and assigned to a variable.
在编程语言设计中,在一个给定的编程语言里first-class citizen(也可以是类型,对象,实体或值)是一个实体(支持所有通常可用于其他实体的操作)。这些操作典型地包括作为参数被传递,从函数返回,并将其分配给一个变量。