[SciPy-Dev] SciPy-Dev Digest, Vol 122, Issue 4

Matt Newville newville at cars.uchicago.edu
Thu Dec 5 22:35:13 EST 2013


Hi Daniele,

> A named tuple would be much preferable over a dictionary IMHO. It is a
> much leaner structure and it still support unpacking if someone is
> inclined to do that.  The Python standard library returns named tuples
> in a few places as well.
>
> Cheers,
> Daniele
>

Perhaps you could enlighten me on this?  I don't claim to know one way
or the other whether a named tuple is "a much leaner structure" than a
dict or a Bunch or an instance of an empty class.   Is this is an
important consideration for how to structure a collection of return
values of high-level scipy functions?

Named tuple can be accessed either by index or by field name.  Any
future changes to such a return argument *must* be made by appending
to the existing values, and would probably cause a lot of grief anyway
(for those relying on there being exactly 8 return values, say, it
would break code).   Since none of the other approaches even have an
order, it would seem that the point of advocating for  named tuple
would be because order was important.  If that were the case, I would
think everyone you asked would immediately come up with the same
answers to these questions:
  a) should the set of return values be ordered?
  b) what is the right order?
  c) are you confident this order s not only right but will always be right?

If you were writing  divmod(), I think you could get such answers (and
probably just use a tuple).

I would have a harder time answering these for minimize(),
curve_fit(), or linfit().  The discussion here about linfit() was not
so much working out the right order for outputs, but about what the
right outputs were.  David expressed the idea of just sticking with
two outputs at least partly (I think) because those two (best_values,
covariance) had an obvious order and everything else could derived
from them, and it was hard to decide what a right order for any
possibly useful derived values might be.  To me, that sort of suggests
that an ordered output is not the best choice

I'm happy to be shown why named tuples would be the way to go, but I'm
not seeing it right now.  I hope you can help me out.

Cheers,

--Matt Newville



More information about the SciPy-Dev mailing list