By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,626 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,626 IT Pros & Developers. It's quick & easy.

Suggestion for (re)try statement

P: n/a
Hi,

I think that would be useful to have an improved
version of the "try" statement, as follows:

try(retrys=0,timeout=0):
# things to try
except:
# what to do if failed

and having the following semantic:

for i in range(retrys):
try:
# things to try
except:
if i < retrys:
i += 1
sleep(timeout)
else:
# what to do if failed
else:
break

Of course, "break" may be the last statement in the
"try" branch, and "try"'s "else" may be ommited
completely.

Can't think of a syntax to keep it look like a
statement rather than a function.

Opinions? Is it worth for a PEP?

Sorin

__________________________________
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com
Oct 27 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Sori Schwimmer wrote:
Hi,

I think that would be useful to have an improved
version of the "try" statement, as follows:

try(retrys=0,timeout=0): <snip> sleep(timeout)

<snip>

At the very least, "timeout" is the wrong wording, "delay" would be more
appropriate. A timeout is usually associated with starting a task and
waiting for it to complete, and continuing if it fails to complete in a
given timeframe, typically also aborting the task at the same time (ie.
executing a database query, connecting to a server, waiting for an
event/lock, etc.).

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Oct 28 '05 #2

P: n/a
Sori Schwimmer wrote:
Hi,

I think that would be useful to have an improved
version of the "try" statement, as follows:

try(retrys=0,timeout=0):
# things to try
except:
# what to do if failed

and having the following semantic:

for i in range(retrys):
try:
# things to try
except:
if i < retrys:
i += 1
sleep(timeout)
else:
# what to do if failed
else:
break


The gold standard for language syntax changes is "compelling use cases"
- if introduced, how often will the construct be used? Is there a python
program out there (preferably in the standard library) which would be
*markedly* improved by the change? What is so repugnant about the
equivalent, currently valid way of writing it? -- Hypothetical and
theoretical arguments don't carry much weight in the Python community
("Practicality beats purity" and all that.)

And remember - your goal isn't ultimately to convince me or someother
person on comp.lang.python, it's to convince Guido.
Oct 28 '05 #3

P: n/a
On 2005-10-27, Sori Schwimmer <sx***@yahoo.com> wrote:
Hi,

I think that would be useful to have an improved
version of the "try" statement, as follows:

try(retrys=0,timeout=0):
# things to try
except:
# what to do if failed

and having the following semantic:

for i in range(retrys):
try:
# things to try
except:
if i < retrys:
i += 1
sleep(timeout)
else:
# what to do if failed
else:
break
The "i += 1" line is almost certainly wrong.
Of course, "break" may be the last statement in the
"try" branch, and "try"'s "else" may be ommited
completely.
And that's pretty much exactly how I usually write it:

for i in range(retries):
try:
whatever
break
except retryableExceptionList:
sleep(delay)
Can't think of a syntax to keep it look like a statement
rather than a function.

Opinions?
I don't see what's wrong with the for loop construct.
You can add an else: clause to the for loop to detect the case
where you ran out of retries:

for i in range(retries):
try:
whatever
break
except retryableExceptionList:
sleep(delay)
else:
whatelse
Is it worth for a PEP?


I don't think you can come up with a syntax that is really that
much better than the for loop, but give it a go if you like.

--
Grant Edwards grante Yow! My BIOLOGICAL ALARM
at CLOCK just went off... It
visi.com has noiseless DOZE FUNCTION
and full kitchen!!
Oct 28 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.