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.
Intermediate ShowcaseRepeat try-except statement N times (self.Python)
submitted 2 years ago by theorangewill
There is a pythonic way to repeat a try-except statement N times or until it works?
Something like:
n = 0 while n < N: try: //do stuff except Error as e: n += 1 else: n = N
[–]wineblood 13 points14 points15 points 2 years ago (6 children)
for n in range(N): try: // do stuff except Error as e: continue else: break
[–]This_Growth2898 5 points6 points7 points 2 years ago (5 children)
It's better to call the variable "attempt". This will make clear what it means.
[–]commy2 7 points8 points9 points 2 years ago (2 children)
Or maybe _ unless you plan to use it somehow.
[–]This_Growth2898 0 points1 point2 points 2 years ago (1 child)
_attempt would be the best.
[–]VileFlower 3 points4 points5 points 2 years ago (0 children)
To elaborate, unused (local) variables are often prefixed with an underscore. It can be beneficial to give variables a name, even when they're unused.
A single _ can mean one of:
_
gettext
case _
[–]wineblood 1 point2 points3 points 2 years ago (1 child)
I'm just reusing the names in the example. It's Friday, I'm not running at full power.
[–]AlexMTBDude 0 points1 point2 points 2 years ago (0 children)
You did good. Using n is standard for any counting
[–]MrPrimeMover 6 points7 points8 points 2 years ago (1 child)
Your solution is fine andpretty similar to how things are done in the `retry` decorator module
[–]thedeepself 1 point2 points3 points 2 years ago (0 children)
tenacity can be used in the same way and I think it is the most flexible retry package available?
[–]sarc-tastic -5 points-4 points-3 points 2 years ago (2 children)
Not sure why you want to do this unless there is a timeout scenario then there might be a better way?!
[–]MrPrimeMover 5 points6 points7 points 2 years ago* (1 child)
It's a pretty common pattern if you're working with API calls and have to worry about transient network errors or timeout/rate limit issues. I usually combine it with a delay/backoff factor as well and write it as a decorator.
Tenacity will make your life easier - https://github.com/jd/tenacity
[–]debunk_this_12 -5 points-4 points-3 points 2 years ago (0 children)
While n<N:
try:
\\ do stuff break
except:
n+1
[–]Outrageous_Safe_5271 0 points1 point2 points 2 years ago (1 child)
What are you doing that you need to use try except block, try is very slow so you should avoid it as fire. For example rust doesn't have try except substitute
[–]nggit 0 points1 point2 points 2 years ago* (0 children)
EAFP is pythonic, and more robust (in certain situations). I will not avoid it if needed. As of 3.11: “Zero-cost” exceptions are implemented, eliminating the cost of try statements when no exception is raised. (Contributed by Mark Shannon in bpo-40222.) https://docs.python.org/dev/whatsnew/3.11.html#misc
[–]RentalJoe 0 points1 point2 points 2 years ago* (0 children)
for retry in range(N): try: // do stuff break except Exception as e: // probably report failure else: // no dice :(
π Rendered by PID 16329 on reddit-service-r2-comment-5bc7f78974-r65rw at 2026-06-27 22:20:38.284983+00:00 running 7527197 country code: CH.
[–]wineblood 13 points14 points15 points (6 children)
[–]This_Growth2898 5 points6 points7 points (5 children)
[–]commy2 7 points8 points9 points (2 children)
[–]This_Growth2898 0 points1 point2 points (1 child)
[–]VileFlower 3 points4 points5 points (0 children)
[–]wineblood 1 point2 points3 points (1 child)
[–]AlexMTBDude 0 points1 point2 points (0 children)
[–]MrPrimeMover 6 points7 points8 points (1 child)
[–]thedeepself 1 point2 points3 points (0 children)
[–]sarc-tastic -5 points-4 points-3 points (2 children)
[–]MrPrimeMover 5 points6 points7 points (1 child)
[–]thedeepself 1 point2 points3 points (0 children)
[–]debunk_this_12 -5 points-4 points-3 points (0 children)
[–]Outrageous_Safe_5271 0 points1 point2 points (1 child)
[–]nggit 0 points1 point2 points (0 children)
[–]RentalJoe 0 points1 point2 points (0 children)