[Python-ideas] values in vs. values out

Ben Finney ben+python at benfinney.id.au
Thu Jan 13 16:13:24 CET 2011


Luc Goossens <luc.goossens at cern.ch> writes:

> Hence, whenever I upgrade a function with a new keyword arg and a
> default value, I do not have to change any of the existing calls,
> whereas whenever I add a new element to its output tuple, I find
> myself chasing all existing code to upgrade the corresponding
> assignments with an additional (unused) variable.

If your function is returning a bunch of related values in a tuple, and
that tuple keeps changing as you re-design the code, that's a code
smell.

The tuple should instead be a user-defined type (defined with ‘class’),
the elements of the tuple should instead be attributes of the type, and
the return value should be a single object of that type.

The type can grow new attributes as you change the design, without the
calling code needing to know every attribute.

This refactoring is called “Replace Array With Object”
<URL:http://www.refactoring.com/catalog/replaceArrayWithObject.html> in
the Java world, but it's just as applicable in Python.

-- 
 \         “How wonderful that we have met with a paradox. Now we have |
  `\                        some hope of making progress.” —Niels Bohr |
_o__)                                                                  |
Ben Finney




More information about the Python-ideas mailing list