utility functions within a class?

bruno at modulix onurb at xiludom.gro
Tue May 9 05:01:49 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?

Yes. Language-inforced access restriction is totally useless (and is
quite easy to defeat in C++ and Java FWIW). Python's philosophy is that
programmers are not braindead monkey-coders that must be protected
(pardon the pun) from their own stupidity. There's no way to idiot-proof
a code anyway, so why bother ? The single-underscore prefix means
'implementation, don't touch or else', and this is quite enough. Most
python programmers won't touch it - unless they have perfectly valid
reasons to do so (yes, it happens), and then they will implicitely
accept the consequences.


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list