- Functional Python Programming
- Steven F. Lott
- 199字
- 2021-08-27 19:20:22
First-class functions
Functional programming is often succinct and expressive. One way to achieve it is by providing functions as arguments and return values for other functions. We'll look at numerous examples of manipulating functions.
For this to work, functions must be first-class objects in the runtime environment. In programming languages such as C, a function is not a runtime object. In Python, however, functions are objects that are created (usually) by the def statements and can be manipulated by other Python functions. We can also create a function as a callable object or by assigning lambda to a variable.
Here's how a function definition creates an object with attributes:
>>> def example(a, b, **kw): ... return a*b ... >>> type(example) <class 'function'> >>> example.__code__.co_varnames ('a', 'b', 'kw') >>> example.__code__.co_argcount 2
We've created an object, example, that is of the function() class. This object has numerous attributes. The __code__ object associated with the function object has attributes of its own. The implementation details aren't important. What is important is that functions are first-class objects and can be manipulated just like all other objects. We previously displayed the values of two of the many attributes of a function object.
- C#高級(jí)編程(第10版) C# 6 & .NET Core 1.0 (.NET開發(fā)經(jīng)典名著)
- Visual Basic 6.0程序設(shè)計(jì)計(jì)算機(jī)組裝與維修
- Java Web基礎(chǔ)與實(shí)例教程(第2版·微課版)
- CentOS 7 Server Deployment Cookbook
- INSTANT Sencha Touch
- 基于免疫進(jìn)化的算法及應(yīng)用研究
- 人臉識(shí)別原理及算法:動(dòng)態(tài)人臉識(shí)別系統(tǒng)研究
- C/C++常用算法手冊(cè)(第3版)
- SAS數(shù)據(jù)統(tǒng)計(jì)分析與編程實(shí)踐
- PHP+MySQL網(wǎng)站開發(fā)項(xiàng)目式教程
- HTML5從入門到精通(第4版)
- C#程序設(shè)計(jì)教程(第3版)
- Hands-On GUI Programming with C++ and Qt5
- Oracle實(shí)用教程
- Node.js從入門到精通