Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: David Mertz

Rating No ratings yet

Python is not a functional programming language, but it is a multi-paradigm language that makes functional programming easy to perform, and easy to mix with other programming styles. In this paper, David Mertz, a director of Python Software Foundation, examines the functional aspects of the language and points out which options work well and which ones you should generally decline.Mertz describes ways to avoid Python’s imperative-style flow control, the nuances of callable functions, how to work lazily with iterators, and the use of higher-order functions. He also lists several third-party Python libraries useful for functional programming.Topics include:Using encapsulation and other means to describe "what" a data collection consists of, rather than "how" to construct a data collection Creating callables with named functions, lambdas, closures, methods of classes, and multiple dispatch Using Python’s iterator protocol to accomplish the same effect as a lazy data structure Creating higher-order functions that take functions as arguments and/or produce a function as a result

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical guide for Python developers who want to write more declarative, composable code by applying functional programming techniques—covering everything from avoiding imperative loops to building lazy pipelines and higher-order functions. 【Book Arc】 - **Opening (~0%–11%)**: Introduces Python as a multi-paradigm language where functional programming is both possible and valuable. Sets up the core tension: Python isn't purely functional, but its features let you mix styles effectively. - **Early (~11%–22%)**: Covers the first major technique—encapsulation and comprehensions—showing how to describe *what* a data collection contains rather than *how* to build it. Includes recursion as an alternative to explicit loops. - **Middle (~22%–33%)**: Dives into callables: named functions, lambdas, closures, callable class instances, methods, and multiple dispatch. Explains the trade-offs among these approaches for creating reusable, composable units of behavior. - **Late (~33%–44%)**: Explores lazy evaluation through Python's iterator protocol and the `itertools` module. Shows how iterators give you the same effect as lazy data structures without eager memory consumption. - **Ending (~44%–56%)**: Concludes with higher-order functions—functions that take or return other functions—plus the `operator` and `functools` modules, and decorators as a practical application of these ideas. 【Key Takeaways】 - **Encapsulation beats explicit construction** (Early): Use comprehensions and generator expressions to describe data transformations declaratively, avoiding verbose imperative loops that obscure intent. - **Recursion replaces iteration in functional style** (Early): Python's recursion limits make it a tool for clarity, not unbounded depth—use it where the recursive structure mirrors the problem. - **Callables come in many flavors** (Middle): Named functions, lambdas, closures, and callable instances each have strengths; lambdas are concise but limited, while closures and callable instances carry state more elegantly. - **Multiple dispatch adds flexibility** (Middle): The `multipledispatch` library lets you define functions that behave differently based on argument types, a powerful pattern for polymorphic behavior. - **Iterators give you laziness for free** (Late): Python's iterator protocol means you can process sequences one element at a time, enabling memory-efficient pipelines that would be impractical with lists. - **`itertools` is a functional toolbox** (Late): The module provides building blocks like `chain`, `cycle`, and `groupby` that compose into complex data transformations without explicit loops. - **Higher-order functions enable composition** (Ending): Functions like `map`, `filter`, and `reduce` (via `functools`) let you build pipelines where data flows through transformations, making code more modular and testable. - **Decorators are higher-order functions in practice** (Ending): They wrap functions to add behavior (logging, memoization, validation) without modifying the original code, a clean separation of concerns. 【Reading Tips】 - **Skim the preface and early examples** (~0%–11%): They set the philosophical context but the real value starts with the concrete techniques in the first chapter. - **Deep-read the callables chapter** (~22%–33%): This is where the subtle differences between lambdas, closures, and callable instances matter most—work through the examples to internalize when each is appropriate. - **Pay special attention to the iterator protocol** (~33%–44%): It's the foundation for lazy evaluation and the most conceptually dense part; trace through the examples to understand how `__iter__` and `__next__` interact. - **Practice with `itertools`** (~44%–56%): The module's functions are best learned by experimenting—try combining `chain`, `islice`, and `groupby` on real data to build fluency. - **Don't skip the decorators section** (Ending): It ties together higher-order functions with practical Python idioms you'll use daily, even if you never write purely functional code. 【Coverage Limits】 The excerpts focus on the first half of the book (through higher-order functions and decorators). They do not cover the third-party libraries chapter or any final practical applications that may appear later in the full text.
Excerpt 1
书名: Functional programming in Python (David Mertz) (Z-Library) 作者: David Mertz Python is not a functional programming language, but it is a multi-paradigm la...
View in text
Page 4
z Copyright © 2015 O’Reilly Media, Inc. All rights reserved. Attribution-ShareAlike 4.0 International (CC BY-SA 4.0). See: http://creativecommons.org/license...
View in text
Page 5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Encapsulation 1 Comprehensions 2 Recursion 5 Eliminating Loops 7 Callables. . . . . . . . . . ....
View in text
Excerpt 4
he purpose. One of the most preferred languages is C++. Why? Mainly because of its speed, its standards and the wide range of applications. C++ was developed...
View in text
Excerpt 5
ms must contain a function (or a piece of code) named main . The purpose of this function is to indicate the compiler where will the program start its execut...
View in text
Excerpt 6
ut for the purposes of this article, these three are enough. For the alphabetic category we have two types of data: char – Store one alphanumeric digit strin...
View in text
Tags
AI categories
PythonProgramming LanguageCode
ISBN: 1491928565
Publisher: O’Reilly Media
Publish Year: 2015
Language: English
Pages: 49
File Format: PDF
File Size: 1.6 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…