Add lists to class?

Mike Meyer mwm at mired.org
Sat Sep 3 21:11:19 EDT 2005


"BBands" <bbands at gmail.com> 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
do:

        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:

     self._.append([])

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

        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list