create instance attributes for every method argument

Larry Bates larry.bates at websafe.com`
Sat Jul 19 22:31:47 CEST 2008


Berco Beute wrote:
> I remember reading somewhere how to create an instance attribute for
> every method argument, but although Google is my friend, I can't seem
> to find it. This could likely be done way more elegant:
> 
> =========================
> class Test(object):
> 
>     def __init__(self, a, b, c, d, e, f):
>         self.a = a
>         self.b = b
>         self.c = c
>         self.d = d
> =========================
> 
> 2B

IMHO you can't do much better than that with positional arguments, but you can 
if they are keyword arguments.

class foo(object):
     def __init__(self, **kwargs):
     self.__dict__.update(kwargs)

-Larry




More information about the Python-list mailing list