Objects, Types, and Protocols
Reference Counting and Garbage Collection
Python manages objects through automatic garbage collection. All objects are reference-counted. An object's referencecount is increased whenever it's assigned to a new name or placed in a container such as a list, tuple, or dictionary:
This example creates a single object containing the value 37. a
is a name that initially refers to the newly created object. When b
is assigned a, b becomes a new name for the
Shallow Copy vs. Deep Copy
A shallow copy creates a new object, but populates it with references to the items contained in the original object. A deep copy creates a new object and recursively copies all the objects it contains.
Shallow Copy
Here's an example:
In this case, a
and b
are separate list objects, but the elements they contain are shared. Therefore, a modification to one of the elements of a
also modifies an element of b
, as shown.
Deep Copy
There is no built-in operator to create deep copies of objects, but you can use the copy.deepcopy()
function in the standard library:
Use of deepcopy()
is actively discouraged in most programs. Copying of an object is slow and often unnecessary. Reserver deepcopy() for situations where you actually need a copy because you're about to mutate data and you don't want your changes to affect the original object. Also, be aware that deepcopy() will fail with objects that involve system or runtime state (such as open files, network connections, threads, generators, and so on).
First-Class Objects
All objects in Python are said to be first-class. This means that all objects that can be assigned to a name can also be treated as data. As data, objects can be stored as variables, passed as arguments, returned from functions, compared against other objects, and more.
Assigning Weird Things to a Dictionary
For example, here is a simple dictionary containing two values:
The first class nature of objects can be seen by adding some more unusual items to this dictionary:
In this example, the items
dictionary now contains a function, a module, an exception, and a method of another object. If you want, you can use dictionary lookups on items
in place of the original names and the code will still work. For example:
Why This is Useful
The fact that everything in Python is first-class is often not fully appreciately by newcomers. However, it can be used to write very compact and flexible code.
For example, suppose you have a line of text such as "ACME,100,490.10" and you want to convert it into a list of values with appropriate type conversions. Here's a clever way to do it by creating a list of types (which are first-class object) and executing a few common list-processing operations:
Placing functions or classes in a dictionary is a common technique for eliminating complex if-elif-else statements. For example, if you have code like this:
You could rewrite it using a dictionary:
This latter form is also more flexible as new cases can be added by inserting more entries into the dictionary without having to modify a large if-elif-else
statement block.
Last updated