[Tutor] class initialization with a lot of parameters
Dave Angel
davea at ieee.org
Tue Nov 10 01:05:26 CET 2009
C.T. Matsumoto wrote:
> Hello All,
>
> I'm making a class and the parameters I'm feeding the class is getting quite
> large. I'm up
> to 8 now. Is there any rules of thumb for classes with a lot of parameters?
> I was thinking
> to put the parameters into a tuple and then in the __init__ of the class,
> iterate over the tuple
> and assign attributes.
>
> <snip>
>
>
Don't do it. Putting the parameters into a tuple only makes sense if
they're somehow related to each other. And if all the parameters are in
the tuple, all you've done is to add another parenthesis pair around the
argument list.
Now, if the parameters are related to each other (like the coordinates
of an n-dimensional point), then it makes sense to group them. As Alan
said, a class can be good for that. So can tuples, but only if there's
some connection, or if they already were being treated as a tuple.
Note that if you want to declare your parameters as a tuple, you can use
the * notation in the parameter list. And if you want to pass the
arguments as a tuple, you can use the * notation in the argument list.
Or both. But you need to have a reason, other than "too many parameters."
DaveA
More information about the Tutor
mailing list