Fluent Python: Clear, Concise, and Effective Programming

Fluent Python: Clear, Concise, and Effective Programming

English | 2015 | ISBN: 978-1-49194-600-8 | 770 Pages | PDF, EPUB | 17 MB

Python’s simplicity lets you become productive quickly, but this often means you aren’t using everything it has to offer. With this hands-on guide, you’ll learn how to write effective, idiomatic Python code by leveraging its best—and possibly most neglected—features. Author Luciano Ramalho takes you through Python’s core language features and libraries, and shows you how to make your code shorter, faster, and more readable at the same time.
Many experienced programmers try to bend Python to fit patterns they learned from other languages, and never discover Python features outside of their experience. With this book, those Python programmers will thoroughly learn how to become proficient in Python 3.
This book covers:

  • Python data model: understand how special methods are the key to the consistent behavior of objects
  • Data structures: take full advantage of built-in types, and understand the text vs bytes duality in the Unicode age
  • Functions as objects: view Python functions as first-class objects, and understand how this affects popular design patterns
  • Object-oriented idioms: build classes by learning about references, mutability, interfaces, operator overloading, and multiple inheritance
  • Control flow: leverage context managers, generators, coroutines, and concurrency with the concurrent.futures and asyncio packages
  • Metaprogramming: understand how properties, attribute descriptors, class decorators, and metaclasses work

An array of sequences

Tuples are not just immutable lists

Some introductory texts about Python present tuples as “immutable lists”, but that is short selling them. Tuples do double-duty: they can be used as immutable lists and also as records with no field names. This use is sometimes overlooked, so we will start with that.

Tuples as records

Tuples hold records: each item in the tuple holds the data for one field and the position of the item gives its meaning.

If you think of a tuple just as an immutable list, the quantity and the order of the items may or may not be important, depending on the context. But when using a tuple as a collection of fields, the number of items is often fixed and their order is always vital. Example 2-7 shows tuples being used as records. Note that in every expression below, sorting the tuple would destroy the information because the meaning of each data item is given by its position in the tuple.

Object Oriented Idioms

The real meaning of an object’s id is implementation-dependent. In CPython, id() returns the memory address of the object, but it may be something else in another Python interpreter. The key point is that the id is guaranteed to be a unique numeric label, and it will never change during the life of the object.

Function parameters as references

The only mode of parameter passing in Python is call by sharing. That is the same mode used in most OO languages, including Ruby, SmallTalk and Java. Call by sharing means that each formal parameter of the function gets a copy of each reference in the arguments. In other words, the parameters inside the function become aliases of the actual arguments.

The result of this scheme is that a function may change any mutable object passed as a parameter, but it cannot change the identity of those objects, i.e. it cannot replace altogether an object with another. Example 8-11 shows a simple function using += on one of its parameters. As we pass numbers, lists and tuples to the function, the actual arguments passed are affected in different ways.

Weak references

The presence of references is what keeps an object alive in memory. When the reference count of an object reaches zero, the garbage collector disposes of it. But sometimes it is useful to have a reference to an object that does not keep it around longer than necessary. A common use case is a cache.

Weak references to an object do not increase its reference count. The object that is the target of a reference is called the referent. Therefore, we say that a weak reference does not prevent the referent from being garbage collected.

Weak references are useful in caching applications because you don’t want the cached objects to be kept alive just because they are referenced by the cache.