utility functions within a class?

Scott David Daniels scott.daniels at acm.org
Mon May 8 14:20:04 EDT 2006


John Salerno wrote:
> John Salerno wrote:
>> blair.bethwaite at gmail.com wrote:
>>> John Salerno wrote:
>>>> What I originally meant was that they would not be called from an
>>>> instance *outside* the class itself, i.e. they won't be used when
>>>> writing another script, they are only used by the class itself.
>>>
>>> Yep, so you want to encapsulate the functionality that those methods
>>> provide, which is the whole point of building them in a class in the
>>> first place.  And you want them to be private to the class so that they
>>> do not form part of the classes public/external interface.
>>
>> But it's right that they should still be regular instance methods? So 
>> from within the class I still call them as self._generate_head(), etc.?
> 
> I tried the underscore method, but I was still able to call it as a 
> regular instance method in the interpreter. Is that what's supposed to 
> happen?

If you called it, "__generate_head" you'd get more of what you expect.

The Python philosophy is "we're all adults here," and the single leading
underscore is meant to tell other users that the method is not for
general use.  Remember, you can't stop a user who insists on making
mistakes.  The double leading underscore is really meant for something
else (names in "mixin" classes that don't accidentally collide with
names in subclasses, for example).

IMO, distutils, for example, over-uses the double leading underscore
names; it makes it awkward for me to get to information that I'd like
to obtain.  The double leading underscore should be there if there is
no reasonable use to getting to this info.  I suspect leading double
underscore names are more frequently written by Java and C++ fugitives
who still have the "save the user from himself" attitude.

--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list