Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   accessing class variables of private classes (http://www.velocityreviews.com/forums/t340354-accessing-class-variables-of-private-classes.html)

Uwe Mayer 01-16-2005 09:35 AM

accessing class variables of private classes
 
Hi,

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?

Thanks
Uwe

Peter Otten 01-16-2005 10:17 AM

Re: accessing class variables of private classes
 
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



Mark McEahern 01-16-2005 02:22 PM

Re: accessing class variables of private classes
 
Uwe Mayer wrote:

>Hi,
>
>I need to access class variables of a class I'd like to make private:
>
>

Use single underscores instead of double underscores--you won't have to
workaround the name mangling. Besides, nothing's really private anyway.

// m


All times are GMT. The time now is 12:11 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.