- 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.
- Go語言高效編程:原理、可觀測性與優(yōu)化
- FLL+WRO樂高機(jī)器人競賽教程:機(jī)械、巡線與PID
- Python機(jī)器學(xué)習(xí):手把手教你掌握150個(gè)精彩案例(微課視頻版)
- Haxe Game Development Essentials
- AutoCAD 2009實(shí)訓(xùn)指導(dǎo)
- Python程序設(shè)計(jì)與算法基礎(chǔ)教程(第2版)(微課版)
- Python編程:從入門到實(shí)踐(第3版)
- Serverless Web Applications with React and Firebase
- Xcode 6 Essentials
- Learning iOS Security
- 零基礎(chǔ)輕松學(xué)C++:青少年趣味編程(全彩版)
- Oracle Database XE 11gR2 Jump Start Guide
- H5頁面設(shè)計(jì)與制作(全彩慕課版·第2版)
- jQuery Essentials
- Visual Basic 開發(fā)從入門到精通