py3k feature proposal: field auto-assignment in constructors

Wildemar Wildenburger lasses_weil at klapptsowieso.net
Sun Jan 27 13:48:15 EST 2008


Diez B. Roggisch wrote:
> Just for the fun of it, I implemented a decorator:
> 
> from functools import *
> from inspect import *
> 
> def autoassign(_init_):
>     @wraps(_init_)
>     def _autoassign(self, *args, **kwargs):
>         argnames, _, _, _ = getargspec(_init_)
>         for name, value in zip(argnames[1:], args):
>             setattr(self, name, value)
>         _init_(self, *args, **kwargs)
> 
>     return _autoassign
> 

This is neat. :) Could that maybe be extended to only assign selected 
args to the instance and let others pass unchanged. So that, for instance:

@autoassign("foo", "bar")
def __init__(self, foo, bar, baz):
     super(baz)

?W



More information about the Python-list mailing list