2

Confusing Equality with Identity

Most objects in Python are mutable. Moreover, all objects in Python are accessed by reference. For objects that are immutable, such as strings, numbers, and frozensets, comparing for equality or inequality rarely raises a concern about whether those objects are also identical. However, for mutable objects such as mutable collections, it becomes very important to distinguish identity from mere equality.

In many programming languages, a distinction is made between pass by value, pass by address, and pass by reference (occasionally pass by name occurs as well). Python behaves most similarly to reference passing, but in Python lingo we often emphasize Python’s semantics by calling the behavior pass by object reference ...

Get Better Python Code: A Guide for Aspiring Experts now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.