python
✓Verified·Scanned 2/18/2026
This skill provides concise Python guidance covering mutable defaults, import traps, scope, string and iteration pitfalls, classes, exceptions, numeric issues, file I/O, and concurrency. No security-relevant behaviors detected.
from clawhub.ai·vb8b4b9d·4.0 KB·0 installs
Scanned from 1.0.0 at b8b4b9d · Transparency log ↗
$ vett add clawhub.ai/ivangdavila/python
Mutable Default Arguments
def f(items=[])shares list across all calls — useitems=Nonethenitems = items or []- Same for dicts, sets, any mutable — default evaluated once at definition, not per call
- Class attributes with mutables shared across instances — define in
__init__instead
Import Traps
- Circular imports fail silently or partially — import inside function to break cycle
from module import *pollutes namespace — explicit imports always- Relative imports require package context —
python -m package.modulenotpython module.py - Import order matters for monkey patching — patch before target module imports patched module
__init__.pyruns on package import — keep it minimal
Scope Gotchas
UnboundLocalErrorwhen assigning to variable that exists in outer scope — usenonlocalorglobal- List comprehension has own scope, but loop variable leaks in Python 2 — not 3
except Exception as e—eis deleted after except block in Python 3- Default argument values bound at definition — closure over loop variable captures final value
String Pitfalls
isvs==:ischecks identity,==checks equality —"a" * 100 is "a" * 100may be False- f-strings evaluate at runtime —
f"{obj}"calls__str__each time str.split()vsstr.split(' ')— no arg splits on any whitespace and removes empties- Unicode normalization:
'café' != 'café'if composed differently — useunicodedata.normalize()
Iteration Traps
- Modifying list while iterating skips elements — iterate over copy:
for x in list(items): - Dictionary size change during iteration raises RuntimeError — copy keys:
for k in list(d.keys()): - Generator exhausted after one iteration — can't reuse, recreate or use
itertools.tee range()doesn't include end —range(5)is 0,1,2,3,4
Class Gotchas
__init__is not constructor —__new__creates instance,__init__initializes- Method resolution order (MRO) in multiple inheritance — use
super()correctly or break chain @propertymakes attribute read-only unless you add setter__slots__breaks__dict__and dynamic attributes — can't add new attributes- Mutable class attribute shared by all instances — each instance modifying same list
Exception Handling
- Bare
except:catchesSystemExitandKeyboardInterrupt— useexcept Exception: except A, B:is syntax error in Python 3 — useexcept (A, B):- Exception chaining:
raise NewError() from originalpreserves context finallyruns even on return — return in finally overrides try's returnelsein try/except runs only if no exception — often clearer than flag variable
Numeric Surprises
0.1 + 0.2 != 0.3— floating point, usedecimal.Decimalfor money- Integer division:
//always floors toward negative infinity —-7 // 2is -4, not -3 boolis subclass ofint—True + True == 2- Large integers have no overflow — but operations get slow
isfails for large integers — only small ints (-5 to 256) are cached
File and I/O
open()without context manager leaks file handles — always usewith open():- Default encoding is platform-dependent — always specify
encoding='utf-8' read()loads entire file to memory — usereadline()or iterate for large files- Binary mode
'rb'required for non-text — Windows line endings differ in text mode
Concurrency
- GIL prevents true parallel Python threads — use multiprocessing for CPU-bound
- Threading still useful for I/O-bound — network, file operations release GIL
async/awaitis not threading — single-threaded cooperative multitaskingmultiprocessingshares nothing by default — use Queue or Manager for communication- Daemon threads killed abruptly on exit — may corrupt data