Add lists to class?

Mike Meyer mwm at
Sun Sep 4 03:11:19 CEST 2005

"BBands" <bbands at> writes:

>> Why don't you use a real list instead?
> I am using lists... I just showed the naming schema. Here is how they
> are implemented.
> for var in range(len(self.symbols)):
>     setattr(self, "_" + str(var), [])

That's not the list he's talking about. And I agree with him.

Why are you setting the attreibutes "_" + str(var)? Why not just store
them in a dictionary?

>> I don't understand what
>> self.__dict__["_" + str(var)] gets you.
> It let's me access lists that aren't known at write time.

Yes, but it's ugly, makes one address space server for two different
sets of variables, and has potential for some nasty bugs. Why not just

        self.lists[var] = []

Or, if you're really tied to "_", do:

    self._[var] = []

Or, given that your example used increasing numeric names, just use a
list here:


which you would then reference as self._[0], self._[1], etc.

Mike Meyer <mwm at>
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

More information about the Python-list mailing list