I usually use a single underscore, for example. _myvar
for protected (as in C ++) methods / attributes that can be used by derived classes and use double underscore, for example. __var
, when it should not be used by anyone else, and double-underlined names at the class definition level are mutilated , so they cannot be redefined in a derived class, for example
class A(object): def result1(self):
It seems here that the derived class B
overrides both _calc1
and __calc2
, but __calc2
not overridden, since its name is already distorted with the class name and therefore output
a1 a2 b1 a2
instead
a1 a2 b1 b2
but ultimately select any convention and document it, also in the above case itβs not so that the base class cannot override the private one, here is the way :)
class B(A): def _calc1(self): return 'b1' def _A__calc2(self): return 'b2'
source share