Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code

Lonnie Princehouse finite.automaton at
Mon Jul 11 23:28:35 CEST 2005

IIRC, the self.__dict__.update(locals())  trick confuses psyco.

But you can make a decorator to achieve the same result.  There's not
really a convincing case for extending python syntax.

def attribute_decorator(f):
    import inspect
    argnames = inspect.getargspec(f)[0]
    def decorator(*args, **keywords):
        bound_instance = args[0]
        for name, value in zip(argnames[1:], args[1:]):
            setattr(bound_instance, name, value)
        return f(*args, **keywords)
    return decorator

#--------- example use:

class foo(object):
    def __init__(self, thing):
        print "init: self.thing is", repr(self.thing)

f = foo('hello world')


More information about the Python-list mailing list