PyTricks:Functions are first-class citizens in Python

函數在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(也可以是類型,對象,實體或值)是一個實體(支持所有通常可用於其他實體的操作)。這些操作典型地包括作爲參數被傳遞,從函數返回,並將其分配給一個變量。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章