I understand why Python requires explicit self
qualifier when referring to instance attributes.
But I often forget it, since I didn't need it in C++.
The bug I introduce this way is sometimes extremely hard to catch; e.g., suppose I write
if x is not None:
f()
instead of
if self.x is not None:
f()
Suppose attribute x
is usually None
, so f()
is rarely called. And suppose f()
only creates a subtle side effect (e.g., a change in a numeric value, or clearing the cache, etc.). Unless I have insane amount of unit tests, this mistake is likely to remain unnoticed for a long time.
I am wondering if anyone knows coding techniques or IDE features that could help me catch or avoid this type of bug.