accessing class variables of private classes

U

Uwe Mayer

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__()
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)
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
 
P

Peter Otten

Uwe said:
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__()
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)
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...
 
M

Mark McEahern

Uwe said:
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
 

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
474,216
Messages
2,571,119
Members
47,720
Latest member
mohdkaif002

Latest Threads

Top