Possibly Pythonic Tail Call Optimization (TCO/TRE)

Marko Rauhamaa marko at pacujo.net
Wed Jul 15 13:24:40 CEST 2015

Ned Batchelder <ned at nedbatchelder.com>:

> On Wednesday, July 15, 2015 at 6:56:10 AM UTC-4, Marko Rauhamaa wrote:
>> Ned Batchelder <ned at nedbatchelder.com>:
>> > I don't understand this, can you explain more? Are you saying that the
>> > Python specification shouldn't specify what x becomes?:
>> >
>> >     def who_knows():
>> >         pass
>> >
>> >     x = who_knows()
>> Yes, that's what I'm saying. The implementation would be free to assign
>> any value whatsoever to x.
> I don't understand why that is helpful for TCE?  Can you explain?  How does
> specifying None make "smooth TCE" difficult?

As Chris already pointed out, tail procedure calls can't be eliminated
otherwise. An example:

   def some_func():

Now, for Python to replace the call to "more_stuff()" with a simple
jump, there can't be an implicit "return None" following it. Instead,
"some_func()" must be allowed to return whatever "more_stuff()" returns.

You could require the programmer to write:

   def some_func():
       return more_stuff()

but that's not good style. In fact, it is not good style to write code
that omits "return None" when None needs to be returned. IOW, the
unspecifiedness of procedure return values is already the unwritten
assumption in good Python code.


More information about the Python-list mailing list