Suggestion for (re)try statement

S

Sori Schwimmer

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
 
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

Sori said:
Hi,

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

try(retrys=0,timeout=0):
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.).
 
R

Rocco Moretti

Sori said:
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.
 
G

Grant Edwards

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top