Tuesday, July 05, 2005

Python snippet - setattr

This isn't anything particular cool, probably I have seen it before, too. But I stick it here anyway:


class A(object):
def __init__(s, **kw):
for n,v in kw.items():
setattr(s,n,v)


Any key/value pairs in the keyword argument to the constructor call will be assigned as members in instances of the class.

Take care,

1 comment:

jetxee said...

It is really a nice trick! Thank you.