[Python-ideas] kwargs for return

Stephen J. Turnbull turnbull.stephen.fw at u.tsukuba.ac.jp
Sun Jan 27 06:28:00 EST 2019

Thomas Güttler Lists writes:

 > Example:
 >      status = backend.transmit_data()
 > But later you want to add something to the [API's return value].

Common Lisp has this feature.  There, it's called "multiple values".
The called function uses a special form that creates a multiple value.
The primary value is always available, whether the return is a normal
value or a multiple value.  Another special form is used to access the
secondary values (and if it's not used, any secondary values are
immediately discarded).  In Python-like pseudo-code:

    def divide(dividend, divisor):
        # A special form, represented as syntax.
        return_multiple_values (dividend // divisor, dividend % divisor)

    def greatest_smaller_multiple(dividend, divisor):
        result = divisor * divide(dividend, divisor)
        # Secondary values are not accessible from result.
        return result

    def recreate_dividend(dividend, divisor):
        # The ``multiple_values`` builtin is magic: there is no other way
        # to "see" multiple values.
        # It returns a tuple.
        # For keyword returns, have a convention that the second value
        # is a dict or use an appropriate "UnboxedMultipleValues" class.
        vs = multiple_values(divide(dividend, divisor))
        return vs[0] * divisor + vs[1]

But Common Lisp experience suggests

(1) if you want to use this feature to change the API, you generally
    want to do a full refactoring anyway, because

(2) the called function can (and often enough to be problematic,
    *does*) creep in the direction of *requiring* attention to the
    secondary values, can (and sometimes *does*) lead to subtle bugs
    at call sites that only use the primary value.

It's unclear to me that this feature can be safely used in the way
your example suggests.


More information about the Python-ideas mailing list