utility functions within a class?

B

bruno at modulix

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.

In python, as discussed earlier :), you can make them semi-private by
using the '__method_name'

Blair, please, don't give bad advices. The 'double-leading-underscore'
stuff has some side-effects (name-mangling), and is meant to protect an
attribute from accidental overloading. The convention for 'protected'
attributes (which should really be named 'implementation attributes') is
a *single* leading underscore.
 
B

bruno at modulix

John said:
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.
 
J

John Salerno

bruno said:
The convention for 'protected'
attributes (which should really be named 'implementation attributes')

FWIW, I agree. When I hear 'protected attribute' I think of an attribute
that can only be used within its class and subclasses of it.
 
J

John Salerno

bruno said:
yes, they will:

class Parrot(object):
def _func1(self, whatever):
print whatever

def talk(self):
self._func1('vroom')
HTH.

yes, all your posts did help...this is exactly what i ended up doing,
and i see now why i needed to make them regular methods, despite the
fact that they weren't used outside the class (because the instance was
still used *inside* the class to call them)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top