Why can't Lists be private variables in a class

Discussion in 'Python' started by fossil_blue, Mar 7, 2004.

  1. fossil_blue

    fossil_blue Guest

    Why we can't define a list as a private variable in a class such as

    __SYMTAB = [ ]

    class Myclass:
    def __init__(self):
    #some constructor here

    def __PlayWithList(arg1):
    # This doesn't work it compains about not having not having attribute
    index or append
    # Why?
    try: self.__SYMTAB.index(arg1)
    except ValueError: self.__SYMTAB.append(arg1)

    But it the list was public SYMTAB = [ ] it would work. Why?


    Thanks,
    Noel
    fossil_blue, Mar 7, 2004
    #1
    1. Advertising

  2. fossil_blue

    Joe Mason Guest

    In article <>, fossil_blue wrote:
    > Why we can't define a list as a private variable in a class such as
    >
    > __SYMTAB = [ ]
    >
    > class Myclass:
    > def __init__(self):
    > #some constructor here
    >
    > def __PlayWithList(arg1):
    > # This doesn't work it compains about not having not having attribute
    > index or append
    > # Why?
    > try: self.__SYMTAB.index(arg1)
    > except ValueError: self.__SYMTAB.append(arg1)
    >
    > But it the list was public SYMTAB = [ ] it would work. Why?


    This obviously isn't the actual code that's giving you the problem.
    It won't work in at least four ways, even before getting to problems
    with "index or append":

    1. The __init__ constructor doesn't have a body (just a "pass" statement
    will do) so it will complain when you try to define __PlayWithList

    2. __PlayWithList is a hidden name, and you haven't provided any way to
    call it.

    3. __PlayWithList doesn't have a self argument, it'll complain about
    the number of parameters when you do call it

    4. __SYMTAB isn't even part of Myclass, so it'll complain about not
    having attribute self.__SYMTAB

    Please post usable sample code. I bet when you fix these, it'll work.

    Joe
    Joe Mason, Mar 7, 2004
    #2
    1. Advertising

  3. fossil_blue

    Neal Holtz Guest

    (fossil_blue) wrote in message news:<>...
    > Why we can't define a list as a private variable in a class such as
    >
    > __SYMTAB = [ ]
    >
    > class Myclass:
    > def __init__(self):
    > #some constructor here
    >
    > def __PlayWithList(arg1):
    > # This doesn't work it compains about not having not having attribute
    > index or append
    > # Why?
    > try: self.__SYMTAB.index(arg1)
    > except ValueError: self.__SYMTAB.append(arg1)
    >
    > But it the list was public SYMTAB = [ ] it would work. Why?
    >
    >
    > Thanks,
    > Noel


    Because of 'Private name mangling' (identifiers beginning with '__' and not ending
    in '__' are considered private to the class) ?

    See
    http://www.python.org/doc/2.3.3/ref/atom-identifiers.html
    Neal Holtz, Mar 7, 2004
    #3
  4. fossil_blue

    Uwe Grauer Guest

    Neal,

    question was: Why can't Lists be private variables in a class

    Your answer is wrong in this context.

    Uwe

    Neal Holtz wrote:
    >
    >
    > Because of 'Private name mangling' (identifiers beginning with '__' and not ending
    > in '__' are considered private to the class) ?
    >
    > See
    > http://www.python.org/doc/2.3.3/ref/atom-identifiers.html
    Uwe Grauer, Mar 7, 2004
    #4
  5. fossil_blue

    Neal Holtz Guest

    Possibly.

    As a previous poster mentioned, there were so many things wrong
    with the example posted, that it was a bit difficult to know what
    actually was attempted and what went wrong. I *think* the OP
    wanted private variables, and his attempt didn't work -- though again
    the error would not have been what the OP said it was. On a near
    approximation of what I *think* was attempted, the error should be

    AttributeError: Myclass instance has no attribute '_Myclass__SYMTAB'

    Of course, to get the nearest thing to private variables, you do

    class Myclass:

    __SYMTAB = [ ]

    def __init__ ...

    and the name mangling docs still explain why that works and the other didn't.


    Uwe Grauer <> wrote in message news:<c2fbi2$m7g$>...
    > Neal,
    >
    > question was: Why can't Lists be private variables in a class
    >
    > Your answer is wrong in this context.
    >
    > Uwe
    >
    > Neal Holtz wrote:
    > >
    > >
    > > Because of 'Private name mangling' (identifiers beginning with '__' and not ending
    > > in '__' are considered private to the class) ?
    > >
    > > See
    > > http://www.python.org/doc/2.3.3/ref/atom-identifiers.html
    Neal Holtz, Mar 7, 2004
    #5
    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. DaveLessnau
    Replies:
    3
    Views:
    413
    Howard
    May 16, 2005
  2. Jeremy Bowers
    Replies:
    3
    Views:
    501
    Steven Bethard
    Jan 24, 2005
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    384
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,744
    Smokey Grindel
    Dec 2, 2006
  5. Gregor Kofler
    Replies:
    6
    Views:
    204
    Gregor Kofler
    Jun 27, 2008
Loading...

Share This Page