Common Python Pitfalls in Versions, Iteration, Sets, and Concurrency
Summary
This introductory programming note describes several Python mistakes that can make code behave unexpectedly or become harder to maintain. It recommends checking which Python version a program targets, favoring clear code over compressed one-liners, and remembering that braces create an empty dictionary rather than an empty set. It also shows that direct iteration, zip, and enumerate can make loops clearer than indexing by list length.
The note explains that Python’s Global Interpreter Lock limits parallel execution of Python code across threads in common implementations, while multiprocessing can enable parallel work at added cost. It also covers the distinction between old-style and new-style classes in Python 2 and warns that mutable default arguments persist across function calls; a None default avoids sharing the same list. These are practical illustrations rather than a complete reference: details vary by Python version and implementation, and the discussion is not specific to trading systems or performance measurement.
Key ideas
- Check Python versions when code must run across different environments.
- Prefer readable, efficient code over dense one-line constructions.
- An empty set is created differently from an empty dictionary.
- Use direct iteration, zip, or enumerate when they fit the loop.
- Mutable default arguments can retain state between function calls.
- The Global Interpreter Lock limits Python thread parallelism in common implementations.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.