[Python-ideas] A Continuations Compromise in Python

Arnaud Delobelle arnodel at googlemail.com
Sun May 3 20:58:59 CEST 2009


2009/5/3 Steven D'Aprano <steve at pearwood.info>:

> Guido's largest objection to TCO is that it ruins nice stack traces when
> you get an exception. I must admit I've never understood this argument.
> Perhaps I'm missing something, but I've never considered the stack
> trace you get in recursive functions useful. Here's an example:
>
>>>> def spam(n=0):
> ...     if n == 10: raise ValueError(
> ...     'Nobody expects the Spanish Inquisition!')
> ...     spam(n+1)
> ...
>>>> spam()
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "<stdin>", line 3, in spam
>  File "<stdin>", line 3, in spam
>  File "<stdin>", line 3, in spam
>  File "<stdin>", line 3, in spam
>  File "<stdin>", line 3, in spam
>  File "<stdin>", line 3, in spam
>  File "<stdin>", line 3, in spam
>  File "<stdin>", line 3, in spam
>  File "<stdin>", line 3, in spam
>  File "<stdin>", line 3, in spam
>  File "<stdin>", line 2, in spam
> ValueError: Nobody expects the Spanish Inquisition!
>
> To me, all those identical "line 3, in spam" lines are just noise. They
> get in the way of a nice stack trace! What is Guido seeing that I'm
> not? Hopefully he isn't counting them by hand to see how deep he got
> into the recursion!

Well if you have

>>> def foo():
...     bar()
...
>>> def bar():
...     baz()
...
>>> def baz():
...     raise ValueError('wrong value :S')
...
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in foo
  File "<stdin>", line 2, in bar
  File "<stdin>", line 2, in baz
ValueError: wrong value :S

With TCO, I guess you would get something like:

>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in baz
ValueError: wrong value :S

-- 
Arnaud



More information about the Python-ideas mailing list