Repository navigation
Expand to 19 verified Python pitfalls with fixes and runnable examples - #1
Merged
Merged
Conversation
- Fix the integer caching example: `c = 257; d = 257; c is d` is True
in a script (the compiler merges identical constants) and only False
in the REPL. Use int("257") to show the real behavior.
- Add 12 new pitfalls: zip() truncation and element swallowing, lazy
generator errors, list multiplication aliasing, shared class
attributes, deleted exception names, UnboundLocalError, bool/int key
collisions, implicit string concatenation, trailing-comma tuples,
strip() character sets, banker's rounding, return in finally.
- Group pitfalls by category, add a contents table, and give every
entry a "Why" and a "Fix".
- Add a self-checking script per pitfall in examples/ and a CI
workflow that runs them on CPython 3.10-3.14.
- Add LICENSE (MIT), CONTRIBUTING.md and .gitignore.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
3 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reviews the original seven pitfalls, fixes one incorrect example, adds twelve new ones, and makes the repo easier to browse and find.
Correction
c = 257; d = 257; print(c is d)printsTruewhen run as a script, because the compiler merges identical constants in one compilation unit. It only printsFalsewhen typed line by line into the REPL. The example now usesint("257")to show the real cache boundary, and the REPL/script difference is documented as its own surprise.New pitfalls
zip()truncates and swallows an element from an iterator · generators delay their errors (plusinconsuming iterators and empty iterators being truthy, added to #1)[[0] * 3] * 3aliasing · shared mutable class attributesexcept ... as edeletese·UnboundLocalErrorfrom a line that never runsTrue,1and1.0collide as dict keysstrip()takes a character set · banker's rounding ·returninfinallyStructure
[2, 4, 6, 8], which visibly leaves[4, 8].Verification
examples/NN_*.py: one self-checking script per pitfall thatasserts the claimed output and the fix..github/workflows/verify.ymlruns every example on CPython 3.10 to 3.14.LICENSE(MIT),CONTRIBUTING.mdand.gitignore.Test plan
🤖 Generated with Claude Code