[Python-ideas] values in vs. values out
Eric Smith
eric at trueblade.com
Thu Jan 13 16:00:15 CET 2011
On 01/13/2011 09:30 AM, Luc Goossens wrote:
> Hi all,
>
> There's a striking asymmetry between the wonderful flexibility in
> passing values into functions (positional args, keyword args, default
> values, *args, **kwargs, ...) and the limited options for processing the
> return values (assignment).
> 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.
> So I was wondering whether this was ever discussed before (and recorded)
> inside the Python community.
> (naively what seems to be missing is the ability to use the assignment
> machinery that binds functions' formal params to the given actual param
> list also in the context of a return value assignment)
You can achieve something similar with PEP 3132's Extended Iterable
Unpacking:
>>> def f(): return 0, 1, 2, 3
...
>>> a, b, c, d, *unused = f()
>>> a, b, c, d, unused
(0, 1, 2, 3, [])
If you add more return values, they show up in unused.
>>> def f(): return 0, 1, 2, 3, 4
...
>>> a, b, c, d, *unused = f() # note caller is unchanged
>>> a, b, c, d, unused
(0, 1, 2, 3, [4])
Or you could return dicts.
Eric.
More information about the Python-ideas
mailing list