accessing class variables of private classes

Peter Otten __peter__ at web.de
Sun Jan 16 05:17:01 EST 2005


Uwe Mayer wrote:

> I need to access class variables of a class I'd like to make private:
> 
> i.e.
> class __Bar(object):
>   pass
> 
> class __Foo(__Bar):
>   def __init__(self):
>     super(__Foo, self).__init__()
> 
>>>> __Foo()
> Name Error: global name '_Foo__Foo' is not defined
> 
> Here I want to prevent the user of instanciating __Foo from outside of the
> module.
> 
> 
> i.e.
> class __A:
>   a_list = []
>   def __init__(self):
>     __A.a_list.append(self)
> 
>>>> __A()
> NameError: global name '_A__A' is not defined
> 
> Here I want to keep a list of instanciated objects of class __A, so I can
> update internal values if they are changed.
> 
> 
> Any ideas?

Use single underscores. Even better: don't impose bogus restrictions on
users of your module. I know, we're all dissenting children, but still...

> 
> Thanks
> Uwe




More information about the Python-list mailing list