Mailing List Archive

Q: names with underscores and style
I'm writing an abstract class, call it MyClass.
It's meant to be used like this:

class SubClass(MyClass):
# implementation...
obj = SubClass()
obj.run()

The run() method of MyClass calls other methods of MyClass.
My question is whether all of the methods other than run()
should be named with a leading underscore (since no one but
the programmer doing the subclassing should have to see them)
or should I begin with a letter the names of run() *and* all
of the methods that I intend for programmers to override.

Basically, I'm trying to figure out when to use a leading
underscore for a method/member name. Is there some rule of
thumb for underscores in Python names?

---Tom
Q: names with underscores and style [ In reply to ]
>>>>> "TAB" == Thomas A Bryan <tbryan@arlut.utexas.edu> writes:

TAB> Basically, I'm trying to figure out when to use a leading
TAB> underscore for a method/member name. Is there some rule of
TAB> thumb for underscores in Python names?

Attributes with DLUNTU (double leading underscore, no trailing
underscores) are treated as private attributes, and Python munges
their names. E.g. self.__privatemeth() Use these when you definitely
don't want subclasses to access or override them (Python doesn't
actually /enforce/ any access control -- remember we're all consenting
adults :).

My own personal convention is to use SLUNTU (single leading ...) when
I want attributes that are part of the `protected' interface of a
class. E.g. self._override_this()

DLUDTU (double leading ... double trailing), e.g. self.__init__() are
reserved for Python's use.

-Barry