[Python-ideas] return from -- breadth of usage

Chris Angelico rosuav at gmail.com
Mon Jan 20 16:39:33 CET 2014


On Tue, Jan 21, 2014 at 2:29 AM, spir <denis.spir at gmail.com> wrote:
> def perform (input):    # a "action"
>     data = prepare(input)
>     process(data)   # tail call
>
> def result (input):     # a "function" properly speaking
>     data = prepare(input)
>     return process(data)   # tail call

To Python, the second one could be a tail call, but the first one
isn't. It's really:

def perform (input):    # a "action"
    data = prepare(input)
    process(data)
    return None

If process() happens to return None, then it becomes a tail call, but
since Python has no way of knowing if this will be the case, it can't
optimize anything away. (Conversely, if the interpreter knew that
perform()'s return value was going to be ignored, the same
optimization could be made, but it can't assume that either.)

But if 'return from' syntax is added, I don't think it'll be much of
an issue to put explicit return statements in functions where you know
it'll always be None.

def perform (input):    # a "action"
    data = prepare(input)
    return from process(data) # now a tail call

ChrisA


More information about the Python-ideas mailing list