accessing class variables of private classes

Discussion in 'Python' started by Uwe Mayer, Jan 16, 2005.

  1. Uwe Mayer

    Uwe Mayer Guest

    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
     
    Uwe Mayer, Jan 16, 2005
    #1
    1. Advertising

  2. Uwe Mayer

    Peter Otten Guest

    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
     
    Peter Otten, Jan 16, 2005
    #2
    1. Advertising

  3. 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
     
    Mark McEahern, Jan 16, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dwok
    Replies:
    7
    Views:
    705
    Hal Rosser
    Mar 4, 2005
  2. DaveLessnau
    Replies:
    3
    Views:
    430
    Howard
    May 16, 2005
  3. Jeremy Bowers
    Replies:
    3
    Views:
    519
    Steven Bethard
    Jan 24, 2005
  4. earthwormgaz
    Replies:
    6
    Views:
    361
    Emmanuel Deloget
    Nov 29, 2006
  5. Gregor Kofler
    Replies:
    6
    Views:
    214
    Gregor Kofler
    Jun 27, 2008
Loading...

Share This Page