Understanding Common Python Pitfalls and Unexpected Behaviors
This article explores a collection of surprising Python quirks—including string interning, the difference between is and ==, function return semantics, for‑loop variable handling, triple‑quoted strings, and in‑place operators—illustrating why even experienced developers encounter hidden pitfalls during everyday coding.
Introduction
The author clarifies that the piece is not a criticism of Python but a compilation of frequently encountered oddities that many developers, even senior ones, run into while writing Python code.
String Interning and Basic Operations
Python sometimes reuses immutable objects during compilation, a technique known as string interning . Zero‑length and single‑character strings are always interned; literals composed solely of letters, digits, or underscores may be interned, while strings containing other characters (e.g., "wtf!" ) are not. Implicit interning can lead to multiple variables referencing the same object, saving memory.
Function Return Values and finally Clause
In a try...finally block, the finally clause always executes, even after return , break , or continue . Consequently, the value returned by a function is determined by the last return statement that actually runs, and a return inside finally will override earlier returns.
Object Identity and the id() Function
Calling id() creates a temporary object, obtains its memory address, and then discards it. Because CPython reuses memory addresses, two separate objects can share the same id after the first object is destroyed, meaning id values are unique only for the lifetime of an object.
For‑Loop Variable Assignment
During each iteration, the iterator (e.g., range(4) ) yields the next value, which is unpacked and assigned to the loop variable, so an explicit assignment like i = 10 inside the loop does not affect the next iteration’s value.
The is Operator vs. Equality
is checks whether two references point to the same object, while == compares the values of the objects. The article provides examples showing that small integers (‑5 to 256) are pre‑allocated and therefore share identity, whereas larger integers do not.
Triple‑Quoted Strings
Triple quotes ( ''' or """ ) delimit multi‑line strings. If the closing triple quotes are missing, Python raises a SyntaxError . Implicit string concatenation also works across adjacent string literals.
Strange Plus Operator Behavior
The augmented assignment a += b is not always equivalent to a = a + b . For mutable types like lists, a += [5,6,7,8] invokes the list’s extend method, modifying the original list in place, whereas a = a + [...] creates a new list.
Additional Oddities
The article concludes with a series of visual examples (captured as images in the original) that demonstrate other quirky behaviors, encouraging readers to experiment directly in Python to discover these nuances.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.