arguments of a function/metaclass

Stargaming stargaming at
Tue Jan 16 17:22:58 CET 2007

rubbishemail at schrieb:
> Hello,
> I have a member function with many (20) named arguments
> def __init__(self,a=1,b=2):
>     self.a=a
>     self.b=b
> I would like to get rid of the many redundant lines like self.a=a and
> set the members automatically.
> The list of default arguments could be given like
> def __init__(**kwargs):
>     arglist={"a":1,"b":2]
> if this makes things easier
> Of course there has to be a check that raises an error in case of an
> unknown argument not mentioned in this list.
> I am sure there is an elegant way how to do this, could you give me any
> hints???
> Many thanks
> Daniel

If it's a long list of arguments, it will stay a long list (i. e. 
representation) of arguments, whatever you do. You could minimize your 
layout, though, to e. g. use a decorator that takes a list of arguments 
automatically saved to self.
But that's just a "layout" (or design) issue and it will stay clumsy, 
whatever you do (perhaps splitting up the dict to many lines will make 
it more readable, but that's it).

To bring up a more liberate attempt, why don't you just save *all* args 
received (self.__dict__.update(kwargs)). If the user provides more 
arguments -- nevermind!
You would have to do something about default values though and here you 
got to use the list again, first updating self.__dict__ with the list 
and afterwards with kwargs.


More information about the Python-list mailing list