Preventing direct access to a class

Aahz Maruch aahz at panix.com
Sun Feb 4 20:16:55 EST 2001


In article <dijr7t8fc2cilu9n9bl4fmbmb1bm4ssfsc at 4ax.com>,
Daniel Klein  <danielk at aracnet.com> wrote:
>On 4 Feb 2001 12:58:32 -0800, aahz at panix.com (Aahz Maruch) wrote:
>>In article <dc4r7t41dchrbrdabst1ung9mop3u4dnoj at 4ax.com>,
>>Daniel Klein  <danielk at aracnet.com> wrote:
>>>
>>>I have a situation where I need to prevent direct access to a class. 
>>
>>BTW, the standard Python idiom for declaring this is to precede the
>>class name with a single underscore (see threading.py for a real
>>example):
>>
>>class _foo:
>>    pass
>
>Yes, I see. But this only prevents access to class A if the user does
>'from/import'.

Thing is, because of the way Python works, there is no way to absolutely
prevent someone from doing anything.  I mean, if nothing else, they've
got access to your source code, right?  ;-)  So what you want to do is
be clear about your intentions and trust that anyone using your code is
going to Do The Right Thing.  Python has a number of mechanisms for
making your intentions clear; using a leading underscore is one of the
simplest and most powerful.
-- 
                      --- Aahz (Copyright 2001 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

"Let's go home and turn on MTV.  I want to watch some Pop-Up Videos."
"Pop-Up Videos is not on MTV, it's on VH-1."
"'MTV' is a generic."



More information about the Python-list mailing list