Named tuples and projection
Giuseppe Ottaviano
giuott at gmail.com
Fri Jun 20 12:36:39 EDT 2008
>>
> [snip]
> Provided you don't change the order of the items in the tuple, you can
> just use slicing:
>
> a, b = f()[ : 2]
Yes, this is what you would normally do with tuples. But i find this
syntax very implicit and awkward. Also, you cannot skip elements, so
you often end up with things like
a, _, b = f()[:3]
The use case I posted is just the most recent that I stumbled upon.
But often a project function would have saved me a lot of typing.
Consider for example when named tuples are used for rows in an ORM (or
attributes in XML):
for person in people:
name, age, address = person['name age address']
# code
More information about the Python-list
mailing list