what is the idiom for copy lots of params into self?
bearophileHUGS at lycos.com
bearophileHUGS at lycos.com
Wed Jan 10 20:06:34 EST 2007
Emin:
> This saves a lot of code and makes it easier to see what is going on,
> but it seems like there should be a better idiom for this task. Any
> suggestions?
I know two ways of doing it, one way requires very light code into the
__init__ and it uses a decorator that takes the parameters and updates
self, but I have seen it's not much reliable in the implementation I
have.
The other way requires a bit more code into the method, but the
function code is quite short, easy to understand, and it seems reliable
enough (code from web.py, a bit modified):
def selfassign(self, locals):
for key, value in locals.iteritems():
if key != 'self':
setattr(self, key, value)
Used on the first line as:
def __init__(self, foo, bar, baz=1):
selfassign(self, locals())
Bye,
bearophile
More information about the Python-list
mailing list