473,387 Members | 1,603 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,387 software developers and data experts.

who is simpler? try/except/else or try/except

Hi all,

reading Dive Into Python, on Chapter 6 (exception), I've found:

"This code comes from the getpass module, a wrapper module for getting a
password from the user"

try:
import termios, TERMIOS
except ImportError:
try:
import msvcrt
except ImportError:
try:
from EasyDialogs import AskPassword
except ImportError:
getpass = default_getpass
else:
getpass = AskPassword
else:
getpass = win_getpass
else:
getpass = unix_getpass

Knowing that this code is very simple, my question is about simplicity. I
think it is simpler the following code. I haven't a long experience on
Python, so I'd like to know your opinions about.

try:
import termios, TERMIOS
getpass = unix_getpass

except ImportError:
try:
import msvcrt
getpass = win_getpass

except ImportError:
try:
from EasyDialogs import AskPassword
getpass = AskPassword

except ImportError:
getpass = default_getpass

thanks,
Fabio
Aug 12 '07 #1
5 1892
Fabio Z Tessitore wrote:
Hi all,

reading Dive Into Python, on Chapter 6 (exception), I've found:

"This code comes from the getpass module, a wrapper module for getting a
password from the user"

try:
import termios, TERMIOS
except ImportError:
try:
import msvcrt
except ImportError:
try:
from EasyDialogs import AskPassword
except ImportError:
getpass = default_getpass
else:
getpass = AskPassword
else:
getpass = win_getpass
else:
getpass = unix_getpass

Knowing that this code is very simple, my question is about simplicity. I
think it is simpler the following code. I haven't a long experience on
Python, so I'd like to know your opinions about.

try:
import termios, TERMIOS
getpass = unix_getpass

except ImportError:
try:
import msvcrt
getpass = win_getpass

except ImportError:
try:
from EasyDialogs import AskPassword
getpass = AskPassword

except ImportError:
getpass = default_getpass
In matters of style such as this there *are* only opinions. I don't
think there are definite grounds for preferring either one.

If you were to propose such a patch on the python-dev list, it would
almost certainly be rejected - not because it is necessarily worse, but
because it would represent a change of style only.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 12 '07 #2
Il Sun, 12 Aug 2007 13:49:18 -0400, Steve Holden ha scritto:
>>
In matters of style such as this there *are* only opinions. I don't
think there are definite grounds for preferring either one.
Opinions are what I'd like to see, because most of you have bigger
experience than me. maybe this example is too trivial for that ... ;-)

If you were to propose such a patch on the python-dev list,
Oh no, I didn't think that

thanks,

bye
Fabio
Aug 12 '07 #3
Fabio Z Tessitore wrote:
reading Dive Into Python, on Chapter 6 (exception), I've found:

"This code comes from the getpass module, a wrapper module for getting a
password from the user"
****************try:
************************from EasyDialogs import AskPassword
****************except ImportError:
************************getpass = default_getpass
****************else:
************************getpass = AskPassword
Knowing that this code is very simple, my question is about simplicity. I
think it is simpler the following code. I haven't a long experience on
Python, so I'd like to know your opinions about.
>
****************try:
************************from EasyDialogs import AskPassword
************************getpass = AskPassword
****************except ImportError:
************************getpass = default_getpass
I think you are asking the wrong question. The difference between these two
functionally equivalent snippets is in expressiveness rather than
simplicity.

The first can be read as

try:
<operation that may fail>
except <Error I can handle>:
<fix it>
else:
<build on successful operation>

When you move the else suite into the try...except

try:
<operation that may fail>
<build on successful operation#
except <Error I can handle>:
<fix it>

you blur the difference between <operation that may failand <build on
successful operationwhile at the same time introducing the implicit
constraint that the latter does not fail with <Error I can handle>.
Therefore the original code gives the reader a much clearer notion of the
author's intention. This may not be a problem for the simple code at hand
but is definitely a bad habit to get into.

Peter

Aug 12 '07 #4
Il Sun, 12 Aug 2007 20:06:23 +0200, Peter Otten ha scritto:

[cut]
at the same time introducing the implicit
constraint that the latter does not fail with <Error I can handle>.
Therefore the original code gives the reader a much clearer notion of
the author's intention. This may not be a problem for the simple code
at hand but is definitely a bad habit to get into.
thanks Peter, that's the kind of opinion I'm interesting to.

bye
Fabio
Aug 12 '07 #5
Peter Otten wrote:
try:
<operation that may fail>
except <Error I can handle>:
<fix it>
else:
<build on successful operation>

When you move the else suite into the try...except

try:
<operation that may fail>
<build on successful operation#
except <Error I can handle>:
<fix it>
Note that in general the semantics of these are different,
since in the first version the except clause will only catch
exceptions in <operation that may fail>, whereas in the
second it may catch exceptions in <build on successful operation>
as well.

It probably doesn't make a difference in this example, but
there are situations where it can.

--
Greg
Aug 13 '07 #6

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

Similar topics

39
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
7
by: Robert Brewer | last post by:
Alex Martelli wrote in another thread: > One sign that somebody has moved from "Python newbie" to "good Python > programmer" is exactly the moment they realize why it's wrong to code: > > ...
0
by: John J. Lee | last post by:
Bare "except:", with no exception specified, is nasty because it can easily hide bugs. There is a case where it seemed useful to me in the past. Now it seems like a bad idea to me (but I think...
2
by: Greg Strong | last post by:
Hello All, Is there a simpler way to count text boxes with data in them when the 4 text boxes are in the header of a form? I've written the code below and it works. ,----- | Private Sub...
32
by: Rene Pijlman | last post by:
One of the things I dislike about Java is the need to declare exceptions as part of an interface or class definition. But perhaps Java got this right... I've writen an application that uses...
4
by: John Salerno | last post by:
My code is below. The main focus would be on the OnStart method. I want to make sure that a positive integer is entered in the input box. At first I tried an if/else clause, then switched to...
20
by: John Salerno | last post by:
I'm starting out with this: try: if int(text) 0: return True else: self.error_message() return False except ValueError: self.error_message()
5
by: Ed Jensen | last post by:
I'm using: Python 2.3.2 (#1, Oct 17 2003, 19:06:15) on sunos5 And I'm trying to execute: #! /usr/bin/env python try:
6
by: mcse jung | last post by:
Here is asample program that writes a program and then executes it. Do you knowof a much simpler way of writing a program that writes a program? """...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.