Why can't Lists be private variables in a class

F

fossil_blue

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
 
J

Joe Mason

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
 
N

Neal Holtz

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
 
U

Uwe Grauer

Neal,

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

Your answer is wrong in this context.

Uwe
 
N

Neal Holtz

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top