How to set object parameters nicely?

allen.fowler allen.fowler at yahoo.com
Wed Dec 2 19:54:26 EST 2009


On Dec 2, 6:36 pm, Carl Banks <pavlovevide... at gmail.com> wrote:

> For the record, I don't really agree that a lot of parameters is code
> smell.  It's maybe a red flag that you are doing too much in one
> function and/or class, but nothing inherently shady.
>
> One thing to ask yourself: are there a lot of combinations of
> parameters that don't make sense?  For example, do you have a lot of
> cases where, say, if one parameter is set to x, then parameters a, b,
> c, and d do nothing?  That would indicate that you should break your
> function/class up into smaller, more targeted parts.
>
> However, if all your parameters are orthogonal, that is, if all or
> most combinations make sense, then there's no reason ten or twenty
> parameters isn't perfectly reasonable.
>
> Whenever I have ten parameters in an __init__, I ususally just write
> out the assignments, although more often than not the object's
> attributes don't correspond to the parameters one-to-one, so I'd have
> to write them out anyway.
>

Thank you for the thoughtful insight.

In this case, and I am trying to create a number of ORM-like objects.
(Though, there is no database involved.)

So, instances of these classes are acting as records that are shuttled
around in the system, and the object's properties are acting as
values.  The parameters are (mostly) orthogonal, but do need defaults,
and some must be required.





More information about the Python-list mailing list