Fundamentals 14 min read

10 Common Python Mistakes Every Developer Should Avoid

This article reviews ten frequent Python programming errors—including mutable default arguments, class variable misuse, incorrect exception handling, variable scope pitfalls, list mutation during iteration, closure late binding, circular imports, naming conflicts, Python 2/3 differences, and improper use of del—providing clear explanations and practical solutions to help developers write more reliable code.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
10 Common Python Mistakes Every Developer Should Avoid

Python is a powerful and flexible language, but developers often fall into subtle pitfalls that can cause confusing bugs. This guide summarizes ten common Python mistakes and shows how to avoid them.

Common Mistake 1: Using an expression as a function's default argument

When a default value is a mutable type, it is evaluated only once at function definition time, causing the same object to be reused across calls.

Calling foo() repeatedly appends to the same list instead of creating a new one.

Solution: use None as the default and create a new list inside the function.

Common Mistake 2: Misusing class variables

Class variables are shared across all instances and subclasses. Accessing an undefined attribute in a subclass falls back to the base class, which can lead to unexpected behavior.

Common Mistake 3: Incorrect exception block parameters

In Python 2, the except clause must bind the exception to a variable using as. Using an identifier after except without as does not catch the intended exception.

Correct form:

except (IndexError, KeyError) as e:

Common Mistake 4: Misunderstanding LEGB variable resolution

Python follows the LEGB rule (Local, Enclosing, Global, Built‑in). Assigning to a variable inside a function makes it local, which can shadow variables from outer scopes and cause UnboundLocalError.

Common Mistake 5: Modifying a list while iterating over it

Removing elements from a list during iteration leads to skipped items or index errors. Use a list comprehension or iterate over a copy instead.

Common Mistake 6: Late binding in closures

Closures capture variables by reference, so all generated functions may see the final loop value. Use default arguments to bind the current value.

Common Mistake 7: Circular module dependencies

Importing modules that reference each other can cause AttributeError if a name is accessed before it is defined. Delay the import or restructure code to break the cycle.

Common Mistake 8: Naming conflicts with standard library modules

Creating a module with the same name as a standard‑library module (e.g., email.py) can cause the interpreter to import the wrong module, leading to hard‑to‑diagnose errors.

Common Mistake 9: Ignoring Python 2 vs Python 3 differences

Exception objects are not accessible outside the except block in Python 3. Keep a reference to the exception if you need it later.

Common Mistake 10: Misusing the del method

When a module is cleaned up, its globals are set to None before __del__ runs, causing AttributeError. Register cleanup functions with atexit.register() to ensure proper finalization.

By understanding and avoiding these ten pitfalls, Python developers can write cleaner, more reliable code and prevent subtle bugs that often arise from language nuances.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonbest practicesVariablesExceptionscommon mistakesdefault-arguments
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.