Add lists to class?

Larry Bates larry.bates at websafe.com
Thu Sep 1 17:38:02 EDT 2005


I think what you want is this (not tested):

class master:
    def __init__(self, list):
        self.count = len(list)
        for entry in list:
            self.__dict__[entry]= []


This will get you master class with three attributes

a = master(list)

a.one
a.two
a.three

each containing an empty list.  You can then do things
like:

a.one.append(something)
a.three.append(something)

Larry Bates

BBands wrote:
> I have a list with some strings in in it, 'one', 'two' 'three' and so
> on. I would like to add lists to a class with those names. I have no
> way of knowing what will be in the list or how long the list will be in
> advance.
> 
> Something like:
> 
> class master:
>     def __init__(self, list):
>         self.count = len(list)
>         for line in list:
>             self.line = [] # obviously this doesn't work
> 
> list = ['one', 'two', 'three']
> 
> a = master(list)
> 
> so that I get:
> 
> dir(a)
> 
> ['_doc__', '_init__', '_module__', 'one', 'two', 'three']
> 
> instead of:
> 
> dir(a)
> 
> ['_doc__', '_init__', '_module__', 'line']
> 
> 
> TIA,
> 
>      jab
> 



More information about the Python-list mailing list