Adding a list of descriptors to a class

Bob B. rbriski at gmail.com
Tue Aug 7 12:56:32 EDT 2007


> Probably the simplest thing is to just add the attributes after the
> class body, e.g.::
>
>      >>> class MyClass(object):
>      ...     pass
>      ...
>      >>> for attr in ['attr1', 'attr2']:
>      ...     setattr(MyClass, attr, MyDesc(attr))
>      ...
>      >>> c = MyClass()
>      >>> c.attr1
>      'blah'
>

That worked.  Thanks.





More information about the Python-list mailing list