Add lists to class?

Michael Ekstrand mekstran at
Thu Sep 1 22:44:36 CEST 2005

On Sep 1, 2005, at 3:18 PM, BBands wrote:
> Something like:
> class master:
>     def __init__(self, list):
>         self.count = len(list)
>         for line in list:
>             self.line = [] # obviously this doesn't work

No, but this does:

class master:
	def __init__(self, lst):
		for line in list:
			setattr(self, line, [])

 From library reference section 2.1, Built-In Functions:
setattr(	object, name, value)
	This is the counterpart of getattr(). The arguments are an object, a 
string and an arbitrary value. The string may name an existing 
attribute or a new attribute. The function assigns the value to the 
attribute, provided the object allows it. For example, setattr(x, 
'foobar', 123) is equivalent to x.foobar = 123.

- Michael

More information about the Python-list mailing list