Possibly Pythonic Tail Call Optimization (TCO/TRE)

Chris Angelico rosuav at gmail.com
Tue Jul 14 23:05:50 EDT 2015


On Wed, Jul 15, 2015 at 11:01 AM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 7/14/2015 1:52 PM, Chris Angelico wrote:
>>
>> On Wed, Jul 15, 2015 at 3:43 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
>>>
>>> I don't like the way integer overflows are explicitly undefined in
>>> modern C.
>>>
>>> Similarly, I don't like the way tail call behavior is undefined in
>>> Python.
>>
>>
>> Where in the Python spec is it stated that tail call behaviour is
>> undefined? The behaviour of the 'return' statement is well defined: it
>> evaluates its expression (or None), *then* removes the top of the call
>> stack and passes control back to the caller:
>>
>> https://docs.python.org/3/reference/simple_stmts.html#the-return-statement
>
>
> I do not see anything there explicitly about call stacks.

There isn't anything explicitly about call stacks, but it clearly
states that the expression is evaluated, and *then* processing of the
return statement occurs. Unlike C, Python doesn't have "undefined
behaviour"; there are some things which are "implementation-defined",
but that's a different concept. But the spec is reasonably clear by
implication, in my opinion; if tail calls are allowed to remove stack
entries, that definition will need to be edited. (That's not to say it
can't be, of course. But it would be a change.)

ChrisA



More information about the Python-list mailing list