Fluent Python: Clear, Concise, and Effective Programming
Rate it:
1%
Flag icon
For example, the syntax obj[key] is supported by the __getitem__ special method.
1%
Flag icon
If a collection has no __contains__ method, the in operator does a sequential scan.
1%
Flag icon
special methods is that they are meant to be called by the Python interpreter, and not by
2%
Flag icon
__repr__ should be unambiguous and, if possible, match the source code necessary to re-create the object being represented.
2%
Flag icon
Contrast __repr__ with __str__, which is called by the str() constructor and implicitly used by the print function. __str__ should return a string suitable for display to end users.
2%
Flag icon
By default, instances of user-defined classes are considered truthy, unless either __bool__ or __len__ is implemented.
2%
Flag icon
Container sequences hold references to the objects they contain, which may be of any type, while flat sequences physically store the value of each item within its own memory space, and not as distinct objects. Thus, flat sequences are more compact, but they are limited to holding primitive values like characters, bytes, and numbers.
3%
Flag icon
In Python code, line breaks are ignored inside pairs of [], {}, or (). So you can build multiline lists, listcomps, genexps, dictionaries and the like without using the ugly \ line continuation escape.
3%
Flag icon
Listcomps can generate lists from the Cartesian product of two or more iterables.
3%
Flag icon
list is arranged as if the for loops were nested in the same order as they appear in the listcomp.
3%
Flag icon
To initialize tuples, arrays, and other types of sequences, you could also start from a listcomp, but a genexp saves memory because it yields items one by one using the iterator protocol instead of building a whole list just to feed another constructor.
3%
Flag icon
“unpacking.” Here we are not interested in the second item, so it’s assigned to _, a dummy variable.
3%
Flag icon
The preceding code also shows a further use of tuple unpacking: enabling functions to return multiple values in a way that is convenient to the caller.
4%
Flag icon
In Python 3, this idea was extended to apply to parallel assignment as well:
4%
Flag icon
The collections.namedtuple function is a factory that produces subclasses of tuple enhanced with field names and a class name — which helps debugging.
4%
Flag icon
be passed as positional arguments to the constructor (in contrast, the tuple constructor takes a single iterable).
4%
Flag icon
Instead of filling your code with hardcoded slices, you can name them. See how readable this makes the for loop at the end of the example.
4%
Flag icon
... 0.....6.................................40........52...55........
4%
Flag icon
34.95    1    $34.95 ... """ >>> SKU = slice(0, 6)
4%
Flag icon
In other words, to evaluate a[i, j], Python calls a.__getitem__((i, j)).
4%
Flag icon
NumPy uses ... as a shortcut when slicing arrays of many dimensions; for example, if x is a four-dimensional array, x[i, ...] is a shortcut for x[i, :, :, :,].
4%
Flag icon
Mutable sequences can be grafted, excised, and otherwise modified in place using slice notation on the left side of an assignment statement or as the target of a del statement.
5%
Flag icon
When the target of the assignment is a slice, the right side must be an iterable object, even if it has just one item.
5%
Flag icon
To concatenate multiple copies of the same sequence, multiply it by an integer.
5%
Flag icon
This is an important Python API convention: functions or methods that change an object in place should return None to make it clear to the caller that the object itself was changed, and no new object was created.
6%
Flag icon
Because the sorting algorithm is stable, “grape” and “apple,” both of length 5, are in the original order.
7%
Flag icon
The append and popleft operations are atomic, so deque is safe to use as a FIFO queue in multithreaded applications without the need for using locks.
9%
Flag icon
A defaultdict is configured to create items on demand whenever a missing key is searched.
10%
Flag icon
infix operators, so, given two sets a and b, a | b returns their union, a & b computes the intersection, and a - b the difference.
11%
Flag icon
The hash() built-in function works directly with built-in types and falls back to calling __hash__ for user-defined types.
11%
Flag icon
Figure 3-3
11%
Flag icon
For user-defined types, the __slots__ class attribute changes the storage of instance attributes from a dict to a tuple in each instance. This will be discussed in “Saving Space with the __slots__ Class Attribute” (Chapter 9).
11%
Flag icon
Optimization is the altar where maintainability is sacrificed.
18%
Flag icon
If a class defines a __call__ method, then its instances may be invoked as functions.
18%
Flag icon
building a local copy prevents unexpected side effects on any list passed as an argument.
23%
Flag icon
The message from Peter Norvig’s design patterns slides is that the Command and Strategy patterns — along with Template Method and Visitor — can be made simpler or even “invisible” with first-class functions, at least for some applications of these patterns.
23%
Flag icon
Expert Python Programming
23%
Flag icon
The second crucial fact is that they are executed immediately when a module is loaded.
23%
Flag icon
Note that register runs (twice) before any other function in the module.
23%
Flag icon
The main point of Example 7-2 is to emphasize that function decorators are executed as soon as the module is imported, but the decorated functions only run when they are explicitly invoked.
24%
Flag icon
If we want the interpreter to treat b as a global variable in spite of the assignment within the function, we use the global declaration:
24%
Flag icon
a closure is a function with an extended scope that encompasses nonglobal variables referenced in the body of the function but not defined there.
25%
Flag icon
Python has three built-in functions that are designed to decorate methods: property, classmethod, and staticmethod.
25%
Flag icon
because lru_cache uses a dict to store the results, and the keys are made from the positional and keyword arguments used in the calls, all the arguments taken by the decorated function must be hashable.
27%
Flag icon
We also visited two awesome function decorators provided in the functools module of standard library: @lru_cache() and @singledispatch
27%
Flag icon
The theme is the distinction between objects and their names. A name is not the object; a name is a separate thing.
27%
Flag icon
A surprising trait of tuples is revealed: they are immutable but their values may change.
27%
Flag icon
Python variables are like reference variables in Java, so it’s better to think of them as labels attached to objects.
27%
Flag icon
To understand an assignment in Python, always read the right-hand side first: that’s where the object is created or retrieved.
28%
Flag icon
An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The is operator compares the identity of two objects; the id() function returns an integer representing its identity.
« Prev 1