Why can't Lists be private variables in a class

Neal Holtz nholtz at docuweb.ca
Sun Mar 7 21:25:27 CET 2004


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 <news at grauer-online.de> wrote in message news:<c2fbi2$m7g$2 at online.de>...
> 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

More information about the Python-list mailing list