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

Discussion in 'Python' started by Fabio Z Tessitore, Aug 12, 2007.

  1. 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
     
    Fabio Z Tessitore, Aug 12, 2007
    #1
    1. Advertising

  2. Fabio Z Tessitore

    Steve Holden Guest

    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 -------------
     
    Steve Holden, Aug 12, 2007
    #2
    1. Advertising

  3. 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
     
    Fabio Z Tessitore, Aug 12, 2007
    #3
  4. Fabio Z Tessitore

    Peter Otten Guest

    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 fail> and <build on
    successful operation> while 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
     
    Peter Otten, Aug 12, 2007
    #4
  5. 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
     
    Fabio Z Tessitore, Aug 12, 2007
    #5
  6. Fabio Z Tessitore

    greg Guest

    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
     
    greg, Aug 13, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Salerno
    Replies:
    4
    Views:
    415
    John Salerno
    Jul 28, 2006
  2. John Salerno
    Replies:
    20
    Views:
    871
    John Salerno
    Aug 11, 2006
  3. Ed Jensen

    try/except/else/finally problem

    Ed Jensen, Jun 28, 2007, in forum: Python
    Replies:
    5
    Views:
    459
    Ben Finney
    Jun 29, 2007
  4. kj
    Replies:
    15
    Views:
    591
    Lawrence D'Oliveiro
    May 23, 2009
  5. David House

    try -> except -> else -> except?

    David House, Jul 6, 2009, in forum: Python
    Replies:
    2
    Views:
    358
    Bruno Desthuilliers
    Jul 6, 2009
Loading...

Share This Page