Why can't Lists be private variables in a class

Neal Holtz nholtz at docuweb.ca
Sun Mar 7 14:16:18 CET 2004


noelrivera at excite.com (fossil_blue) wrote in message news:<c7c33240.0403062142.6762c4d1 at posting.google.com>...
> 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



More information about the Python-list mailing list