Why can't Lists be private variables in a class

Joe Mason joe at notcharles.ca
Sun Mar 7 07:08:18 CET 2004


In article <c7c33240.0403062142.6762c4d1 at posting.google.com>, 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



More information about the Python-list mailing list