Program calling unwanted functions

sohcahtoa82 at gmail.com sohcahtoa82 at gmail.com
Mon Dec 22 19:04:48 EST 2014


On Monday, December 22, 2014 3:57:31 PM UTC-8, Dave Angel wrote:
> On 12/22/2014 06:48 PM, Ian Kelly wrote:
> > On Dec 22, 2014 2:37 PM, "Dave Angel" <davea at davea.name> wrote:
> >>
> >> I'll pick on one function first, called instructions().  If the user
> > types something invalid, you print "Invalid input." and call the function
> > again.  In this case, because the call is at the end, no harm is usually
> > done, but it would be tricky to explain why.  If the user happened to type
> > the wrong input 1000 times, you'd hit a recursion limit and crash, but
> > that's unlikely unless you have a very stubborn user.
> >
> > Point of interest, Python (or at least CPython) doesn't optimize tail-call
> > recursion, so in fact this sort of unbounded recursion (a "stack leak", if
> > you will) is equally bad no matter where it appears in the function.
> >
> 
> Which is exactly why I mentioned the recursion limit of about 1000.
> 
> 
> -- 
> DaveA

Python 2.7.3 (default, Dec  4, 2012, 17:28:13)
[GCC 4.4.5 20110214 (Red Hat 4.4.5-6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def test(t):
...     print(t)
...     test(t+1)
...
>>> test(1)
1
2
3
4
<snip>
998
999
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in test
  File "<stdin>", line 3, in test
  File "<stdin>", line 3, in test
<snip>
  File "<stdin>", line 3, in test
  File "<stdin>", line 3, in test
RuntimeError: maximum recursion depth exceeded

Huh...there actually is a limit of about 1,000.  I'm assuming this is hard-coded?  I did a similar test with Java a while back and was getting different results every time.



More information about the Python-list mailing list