Preventing execution of a method

rdmurray at bitdance.com rdmurray at bitdance.com
Thu Dec 11 11:35:47 EST 2008


On Thu, 11 Dec 2008 at 08:16, alex23 wrote:
> On Dec 12, 2:07 am, "Emanuele D'Arrigo" <man... at gmail.com> wrote:
>> I.e. if I have a class with two methods, doSomethingSafe() and
>> doSomethingDangerous(), is there a way to prevent another module from
>> executing doSomethingDangerous() but allow the execution of
>> doSomethingSafe()?
>>
>> My understanding is that in python this is not possible. Can you
>> confirm?
>
> Your understanding is correct.
>
> The Python convention is to prefix non-public methods/classes etc with
> an underscore, as in _doSomethingDangerous(). This is meant to
> indicate to anyone using your module that they shouldn't use this
> function, at least not without having a good understanding of what it
> does.

There is, however, also the possibility of prefixing the method name
with '__'.  The invokes 'name mangling', which makes it more difficult
(though not impossible, the idea is to avoid accidents) for the method
to be called from outside the class.

http://www.python.org/doc/2.5.2/tut/node11.html#SECTION0011600000000000000000.

--RDM


More information about the Python-list mailing list