## [1. The Python Data Model](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#6|9799) 1.91%, 2025-01-11 20:24:45 UTC Color: #FED000 > The first thing to know about special methods is that they are meant > to be called by the Python interpreter, and not by you. You don’t > write my\_object.\_\_len\_\_(). You write len(my\_object) and, if > my\_object is an instance of a user-defined class, then Python calls > the \_\_len\_\_ method you implemented. ## [1. The Python Data Model](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#6|10103) 1.92%, 2025-01-11 20:24:45 UTC Color: #64C466 > the interpreter takes a shortcut when dealing for built-in types like > list, str, bytearray, or extensions like the NumPy arrays. Python > variable-sized collections written in C include a struct2 called > PyVarObject, which has an ob\_size field holding the number of items > in the collection. ## [1. The Python Data Model](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#6|10512) 1.95%, 2025-01-11 20:24:46 UTC Color: #64C466 > and this is much faster than calling a method. ## [1. The Python Data Model](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#6|11185) 1.98%, 2025-01-11 20:24:46 UTC Color: #FED000 > If you need to invoke a special method, it is usually better to call > the related built-in function (e.g., len, iter, str, etc.). These > built-ins call the corresponding special method, but often provide > other services and—for built-in types—are faster than method calls ## [Why len Is Not a Method](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#6|24566) 2.71%, 2025-04-14 17:58:50 UTC Color: #EB4D3D > len(x) runs very fast when x is an instance of a built-in type. No > method is called for the built-in objects of CPython: the length is > simply read from a field in a C struct. ## [Why len Is Not a Method](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#6|24904) 2.73%, 2025-04-08 13:14:58 UTC Color: #EB4D3D > In other words, len is not called as a method because it gets special > treatment as part of the Python Data Model, just like abs. ## [Further Reading](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#6|29661) 2.99%, 2025-04-08 13:18:37 UTC Color: #FED000 > In contrast, consider Go. Some objects in that language have features > that are magic, in the sense that we cannot emulate them in our own > user-defined types. For example, Go arrays, strings, and maps support > the use brackets for item access, as in a\[i\]. But there’s no way to > make the \[\] notation work with a new collection type that you > define. ## [Further Reading](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#6|30608) 3.04%, 2025-04-08 13:17:43 UTC Color: #FED000 > The *metaobject* part refers to the objects that are the building > blocks of the language itself. In this context, *protocol* is a > synonym of *interface*. So a *metaobject protocol* is a fancy synonym > for object model: an API for core language constructs. ## [2. An Array of Sequences](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#7|3217) 3.27%, 2025-01-22 16:37:09 UTC Color: #FED000 > A *container sequence* holds references to the objects it contains, > which may be of any type, while a *flat sequence* stores the value of > its contents in its own memory space, not as distinct Python objects ## [2. An Array of Sequences](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#7|46022) 5.61%, 2025-01-22 16:41:49 UTC Color: #FED000 > A common feature of list, tuple, str, and all sequence types in Python > is the support of slicing operations, which are more powerful than > most people realize. ## [2. An Array of Sequences](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#7|48086) 5.72%, 2025-01-22 16:46:13 UTC Color: #FED000 > The notation a:b:c is only valid within \[\] when used as the indexing > or subscript operator, and it produces a slice object: slice(a, b, c) ## [2. An Array of Sequences](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#7|48428) 5.74%, 2025-01-22 16:46:52 UTC Color: #EB4D3D > knowing about slice objects is useful because it lets you assign names > to slices, just like spreadsheets allow naming of cell ranges. ## [2. An Array of Sequences](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#7|52303) 5.95%, 2025-01-22 16:39:14 UTC Color: #FED000 > When the target of the assignment is a slice, the righthand side must > be an iterable object, even if it has just one item. ## [list.sort Versus the sorted Built-In](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#7|61643) 6.46%, 2025-04-21 22:39:34 UTC Color: #FED000 > In contrast, the built-in function sorted creates a new list and > returns it. It accepts any iterable object as an argument, including > immutable sequences and generators ( ## [list.sort Versus the sorted Built-In](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#7|62129) 6.49%, 2025-04-21 22:41:17 UTC Color: #FED000 > key > A one-argument function that will be applied to each item to produce > its sorting key. For example, when sorting a list of strings, > key=str.lower can be used to perform a case-insensitive sort, and > key=len will sort the strings by character length. ## [Memory Views](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#7|71861) 7.02%, 2025-03-12 20:28:38 UTC Color: #FED000 > The built-in memoryview class is a shared-memory sequence type that > lets you handle slices of arrays without copying bytes ## [Memory Views](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#7|72173) 7.04%, 2025-04-19 06:39:33 UTC Color: #FED000 > A memoryview is essentially a generalized NumPy array structure in > Python itself (without the math). It allows you to share memory > between data-structures (things like PIL images, SQLite databases, > NumPy arrays, etc.) without first copying. This is very important for > large data sets. ## [NumPy](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#7|75111) 7.20%, 2025-04-19 06:40:09 UTC Color: #FED000 > For advanced array and matrix operations, NumPy is the reason why > Python became mainstream in scientific computing applications. NumPy > implements multi-dimensional, homogeneous arrays and matrix types that > hold not only numbers but also user-defined records, and provides > efficient element-wise operations. ## [What Is Hashable](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#8|13997) 9.34%, 2025-04-19 06:22:31 UTC Color: #EB4D3D > The hash code of a correctly implemented object is guaranteed to be > constant only within one Python process. ## [What Is Hashable](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#8|14277) 9.36%, 2025-04-19 06:23:35 UTC Color: #FED000 > If an object implements a custom \_\_eq\_\_() that takes into account > its internal state, it will be hashable only if its \_\_hash\_\_() > always returns the same hash code. In practice, this requires that > \_\_eq\_\_() and \_\_hash\_\_() only take into account instance > attributes that never change during the life of the object. ## [collections.ChainMap](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#8|32748) 10.37%, 2025-04-08 12:41:33 UTC Color: #FED000 > A ChainMap instance holds a list of mappings that can be searched as > one. The lookup is performed on each input mapping in the order it > appears in the constructor call, and succeeds as soon as the key is > found in one of those mappings. ## [9. Decorators and Closures](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#15|369) 30.49%, 2025-04-22 13:28:02 UTC Color: #FED000 > Function decorators let us “mark” functions in the source code to > enhance their behavior in some way. This is powerful stuff, but > mastering it requires understanding closures—which is what we get when > functions capture variables defined outside of their bodies. ## [Decorators 101](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#15|2715) 30.61%, 2025-04-22 13:29:42 UTC Color: #FED000 > A decorator is a callable that takes another function as an argument > (the decorated function). > > A decorator may perform some processing with the decorated function, > and returns it or replaces it with another function or callable > object.2 ## [Closures](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#15|14184) 31.24%, 2025-04-08 12:40:39 UTC Color: #FED000 > Actually, a closure is a function—let’s call it f—with an extended > scope that encompasses variables referenced in the body of f that are > not global variables or local variables of f. Such variables must come > from the local scope of an outer function that encompasses f. > > It does not matter whether the function is anonymous or not; what > matters is that it can access nonglobal variables that are defined > outside of its body. ## [The nonlocal Declaration](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#15|20437) 31.58%, 2025-04-22 13:31:51 UTC Color: #FED000 > the nonlocal keyword was introduced in Python 3. It lets you declare a > variable as a free variable even when it is assigned within the > function. If a new value is assigned to a nonlocal variable, the > binding stored in the closure is changed ## [Variable Lookup Logic](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#15|21165) 31.62%, 2025-04-23 09:18:55 UTC Color: #64C466 > ## Variable Lookup Logic > > When a function is defined, the Python bytecode compiler determines > how to fetch a variable x that appears in it, based on these rules:3 > > * If there is a global x declaration, x comes from and is assigned to > the x global variable module.4 > > * If there is a nonlocal x declaration, x comes from and is assigned > to the x local variable of the nearest surrounding function where x > is defined. > > * If x is a parameter or is assigned a value in the function body, > then x is the local variable. > > * If x is referenced but is not assigned and is not a parameter: > > * x will be looked up in the local scopes of the surrounding > function bodies (nonlocal scopes). > > * If not found in surrounding scopes, it will be read from the > module global scope. > > * If not found in the global scope, it will be read from > \_\_builtins\_\_.\_\_dict\_\_. ## [9. Decorators and Closures](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#15|55713) 33.53%, 2025-02-01 16:41:54 UTC Color: #fed000 > Dynamic Scope Versus Lexical Scope > > The designer of any language with first-class functions faces this > issue: being a first-class object, a function is defined in a certain > scope but may be invoked in other scopes. The question is: how to > evaluate the free variables? The first and simplest answer is “dynamic > scope. ## [12. Special Methods for Sequences](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#19|45) 39.06%, 2025-04-07 10:42:19 UTC Color: #64C466 > Don’t check whether it *is*-a duck: check whether it *quacks*-like-a > duck, *walks*-like-a duck, etc., etc., depending on exactly what > subset of duck-like behavior you need to play your language-games > with. (comp.lang.python, Jul. 26, 2000) > > Alex Martelli ## [Protocols and Duck Typing](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#19|8974) 39.54%, 2025-04-07 10:38:13 UTC Color: #64C466 > In the context of object-oriented programming, a protocol is an > informal interface, defined only in documentation and not in code. ## [Two Kinds of Protocols](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#20|8025) 42.75%, 2025-04-07 10:37:26 UTC Color: #FED000 > In addition to static protocols, Python provides another way of > defining an explicit interface in code: an abstract base class (ABC). ## [Programming Ducks](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#20|8326) 42.77%, 2025-04-07 10:36:59 UTC Color: #FED000 > two of the most important in Python: the sequence and iterable > protocols. The interpreter goes out of its way to handle objects that > provide even a minimal implementation of those protocols, ## [Python Digs Sequences](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#20|11518) 42.94%, 2025-04-07 10:35:10 UTC Color: #FED000 > the special treatment Python gives to anything vaguely resembling a > sequence. The iterable protocol in Python represents an extreme form > of duck typing: the interpreter tries two different methods to iterate > over objects. ## [Enhancing Classes with a Class Decorator](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#33|29751) 91.54%, 2025-04-19 06:44:30 UTC Color: #FED000 > the leading \_ reduces the chance of naming conflicts with > user-defined attributes and methods of the decorated class. ## [What Happens When: Import Time Versus Runtime](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#33|32422) 91.69%, 2025-04-08 07:44:10 UTC Color: #64C466 > At import time, the interpreter: > > 1. Parses the source code of a *.py* module in one pass from top to > bottom. This is when a SyntaxError may occur. > > 2. Compiles the bytecode to be executed. > > 3. Executes the top-level code of the compiled module. ## [What Happens When: Import Time Versus Runtime](https://reader.bookfusion.com/books/5368154-fluent-python-2nd-edition?type=epub_reflowable#33|33074) 91.72%, 2025-04-08 07:43:14 UTC Color: #F7CE46 > In particular, the import statement is not merely a declaration,11 but > it actually runs all the top-level code of a module when it is > imported for the first time in the process.