Named tuples and projection
google at mrabarnett.plus.com
Fri Jun 20 16:56:32 CEST 2008
On Jun 20, 9:38 am, Giuseppe Ottaviano <giu... at gmail.com> wrote:
> I found the namedtuple very convenient for rapid prototyping code, for
> functions that have to return a number of results that could grow as
> the code evolves. They are more elegant than dicts, and I don't have
> to create a new explicit class. Unfortunately in this situation they
> lose the convenience of tuple unpacking: changing tuple's parameters
> would break other functions unpacking the result.
> One solution, with 3.0 syntax, would be unpacking only the used
> parameters, using always a *rest
> a, b, *rest = f()
> so that if the tuple grows, the code keeps working.
> However I find this confusing (and not avaliable in python 2.5).
> I don't know if similar solutions have been proposed, I came up with
> this one:
Provided you don't change the order of the items in the tuple, you can
just use slicing:
a, b = f()[ : 2]
More information about the Python-list