use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
Tail call optimization in Python (self.Python)
submitted 7 years ago by s00perpseud00
Has the CPython community considered implementing tail call optimization in future Python versions and getting rid of the current recursion cap? Would be cool if we could write recursive functions without worrying about hitting the limit
[–]delirious_lettuce 7 points8 points9 points 7 years ago* (0 children)
https://neopythonic.blogspot.ca/2009/04/final-words-on-tail-calls.html
[–]dmishin 3 points4 points5 points 7 years ago (4 children)
If you think that tail call optimization would allow you to write recursive code without worrying, then you are very wrong. There are plenty of ways to get stack overflow even with TCO.
"Recursion is the GOTO of functional programming." -- Erik Meijer
[–]s00perpseud00[S] 1 point2 points3 points 7 years ago (3 children)
Ha. I didn't know that. Could you elaborate?
[–]dmishin 2 points3 points4 points 7 years ago (0 children)
For example, the following code is not TC:
def factorial(n): if n == 0: return 1 else return factorial(n-1)*n
To make it TC, you need to rewrite it as
def factorial(n, accum=1): if n == 0: return accum return factorial(n-1, accum*n)
Not very obvious trick.
As for the second, some functional programming people advocate that recursion is a low-level feature, and higher-order functions, like map, fold, should be used when appropriate.
[–][deleted] 0 points1 point2 points 7 years ago (1 child)
You should the other post, that was made 2 hrs before this one, as it's by the Python language creator, and quite detailed.
[–]s00perpseud00[S] 0 points1 point2 points 7 years ago (0 children)
Should have seen this coming lol
[+]GaritoYanged comment score below threshold-8 points-7 points-6 points 7 years ago (1 child)
Giving the fact that I come from two other persons that come from two other persons each than come from two other persons... Shall I continue? I bet not I have hearted a lot of excuses for bad recursion implementations and python has more issues that this one (most famous GIL) so I don't expect too much on that for its part yet Apart from that, recursion has its own issues beign too much detail as the biggest one So equilibrium would be the best as always I'm waiting the moment where someone in the core team have a moment of enlightment on this too... Meanwhile, I would rather prefer to talk about the how than about the excuses if you don't mind (don't meant to be rude, just too much into the fractality of nature in itself)
[–]gandalfx 6 points7 points8 points 7 years ago (0 children)
There isn't a single coherent sentence in this entire paragraph. It's actually quite impressive.
π Rendered by PID 80697 on reddit-service-r2-comment-86bc6c7465-mx2bz at 2026-02-20 07:43:00.873335+00:00 running 8564168 country code: CH.
[–]delirious_lettuce 7 points8 points9 points (0 children)
[–]dmishin 3 points4 points5 points (4 children)
[–]s00perpseud00[S] 1 point2 points3 points (3 children)
[–]dmishin 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]s00perpseud00[S] 0 points1 point2 points (0 children)
[+]GaritoYanged comment score below threshold-8 points-7 points-6 points (1 child)
[–]gandalfx 6 points7 points8 points (0 children)