By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

accessing class variables of private classes

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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


Jul 18 '05 #2

P: n/a
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
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.