[Python-ideas] kwargs for return

David Mertz mertz at gnosis.cx
Sat Jan 26 12:30:24 EST 2019


On Sat, Jan 26, 2019 at 10:31 AM Steven D'Aprano <steve at pearwood.info>
wrote:

> In what way is it worse, given that returning a namedtuple with named
>
fields is backwards compatible with returning a regular tuple? We can
> have our cake and eat it too.
> Unless the caller does a type-check, there is no difference. Sequence
> unpacking will still work, and namedtuples unlike regular tuples can
> support optional attributes.
>

I suppose the one difference is where someone improperly relies on tuple
unpacking.

Old version:

def myfun():
    # ...
    return a, b, c

# Call site
val1, val2, val3 = myfun()


New version:


def myfun():
    # ...
    return a, b, c, d


Now the call site will get "ValueError: too many values to unpack".
Namedtuples don't solve this problem, of course.  But they don't make
anything worse either.

The better approach, of course, is to document the API as only using
attribute access, not positional.  I reckon dataclasses from the start
could address that concern... but so can documentation alone.  E.g.:

Old version (improved):

def myfun():

    mydata = namedtuple("mydata", "a b c")

    # ...
    return mydata(a, b, c)

# Call site
ret = myfun()

val1, val2, val3 = ret.a, ret.b, ret.c


New version (improved)

def myfun():

    mydata = namedtuple("mydata", "a b c d e")

    # ...
    return mydata(a, b, c, d, e)

Now the call site is completely happy with no changes (assuming it doesn't
need to care about what values 'ret.d' or 'ret.e' contain... but presumably
those extra values are optional in some way.

Moreover, we are even perfectly fine if we had created namedtuple("mydata",
"e d c b a") for some reason, completely changing the positions of all the
named attributes in the improved namedtuple.

-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190126/ee65909c/attachment.html>


More information about the Python-ideas mailing list