- 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.
- Visual C++程序設(shè)計教程
- 解構(gòu)產(chǎn)品經(jīng)理:互聯(lián)網(wǎng)產(chǎn)品策劃入門寶典
- Building a Game with Unity and Blender
- 劍指JVM:虛擬機實踐與性能調(diào)優(yōu)
- Hands-On Image Processing with Python
- HTML5游戲開發(fā)案例教程
- Linux環(huán)境編程:從應(yīng)用到內(nèi)核
- C++程序設(shè)計基礎(chǔ)教程
- Python高效開發(fā)實戰(zhàn):Django、Tornado、Flask、Twisted(第3版)
- Getting Started with NativeScript
- Java面向?qū)ο蟪绦蛟O(shè)計
- 21天學(xué)通C++(第5版)
- Web App Testing Using Knockout.JS
- 3ds Max印象 電視欄目包裝動畫與特效制作
- C++ System Programming Cookbook