473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

About python 2.5 and its try statement.

I can't remember the proposal number, but many of you reading will have
probably read the features that will be added to python 2.5. The actual
part I wanted to talk about was the finally part of try. Isn't it
totally defeating a compiler's job by executing the finally part even
if there is an error in the previous statements? Or have I understood
something wrong?

Jun 26 '06 #1
3 1048
Hi,
defcon8 wrote:
I can't remember the proposal number, but many of you reading will have
probably read the features that will be added to python 2.5. The actual
part I wanted to talk about was the finally part of try. Isn't it
totally defeating a compiler's job by executing the finally part even
if there is an error in the previous statements? Or have I understood
something wrong?


try ... finally already exists in current python; what's new is that it
can be combined in 1 statement with try ... except ... finally.

No, this is not defeating a compilers job. It's very useful.

It defines an amount of cleanup that needs to happen no matter what,
exception or no exception. Example: closing an open file. You'll want
to close the file, whether after you done the job you intended to do,
or after an exception occurred while doing something.

So you put 'f.close()' in your finally - suite.

Another example of a useful 'finally' is logging function-exit -- not
the actual 'return' statement, but just a log-statement recording
function-exit.

(Putting a 'return' statement in a 'finally' suite is totally defeating
the point of exceptions that you want thrown to the caller, yes. So
don't put your 'return' statement there.)

Clearer?

Cheers,

--Tim

Jun 26 '06 #2
defcon8 wrote:
I can't remember the proposal number,
http://docs.python.org/dev/whatsnew/pep-341.html

but many of you reading will have probably read the features that will be added to python 2.5. The actual
part I wanted to talk about was the finally part of try.
It has been here from the start (well, IIRC, it's since at least 1.5.2)
- the only limitation was that you couldn't use both 'except' and
'finally' in the same statement, so you had to wrap'em when needed.
Isn't it
totally defeating a compiler's job by executing the finally part even
if there is an error in the previous statements?
I don't see how this relates to the compiler. But anyway, the whole
point of the 'finally' clause is (and has ever been) to *ensure* this
block gets executed *whatever* happened. FWIW, the canonical use case is
resource aquisition/release.
Or have I understood
something wrong?


Seems so - unless it's me misunderstading your question.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 26 '06 #3
"defcon8" wrote:
I can't remember the proposal number, but many of you reading will have
probably read the features that will be added to python 2.5. The actual
part I wanted to talk about was the finally part of try. Isn't it
totally defeating a compiler's job by executing the finally part even
if there is an error in the previous statements? Or have I understood
something wrong?


sounds like you're confusing compilation and execution, and compilation errors
(syntax errors) with runtime errors (exceptions).

try-finally is a runtime thing, not a compile time thing.

</F>

Jun 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
3
by: Link | last post by:
Dear sir, I am used to developing C program on MSVC7.0 platform. This is my first time to use Python for calling by C program. Now, it is hard to deal with the problem about extracting the...
3
by: T.T.H. | last post by:
Hi I am coding a C++ COM object which I want to access in Python. For that I do have some detail questions and need help since I am not that familiar with the documentation itself of Python and...
3
by: Brian L. | last post by:
After following the discussion on Generator expressions on python-dev for the last few weeks, and finally getting used to list comprehensions as opposed to map/filter, it occurred to me that the...
4
by: Gabriel Zachmann | last post by:
I am relatively new to python, but i know some other languages, such as C++, fairly well. After reading up a little bit in the reference manual, section 4.1, and searching this NG, i still have 2...
0
by: Gary Herron | last post by:
Ohad Frand wrote: There is no way you can consider 'elif', 'else', 'except', and 'from' statements. However, as someone pointed out, the kwlist from the keyword module is the closest thing we...
0
by: Ohad Frand | last post by:
Hi Thanks a lot for your reply I think the main uses for it is to study the language and to see that I didn't miss anything else or that something is changed from one version to another. The...
270
by: Jordan | last post by:
Hi everyone, I'm a big Python fan who used to be involved semi regularly in comp.lang.python (lots of lurking, occasional posting) but kind of trailed off a bit. I just wrote a frustration...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.