Python's Exception, and Capitalization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ray

    #1

    Python's Exception, and Capitalization

    Hello guys,

    OK, I've been reading some more about Python. There are some things
    about Python exception that I haven't been able to grasp:

    1. This is a small thing, but why is object spelled "object", and the
    mother of all exception "Exception" (with capital E)? Why is not object
    spelled "Object" then? Especially since Exception's descendants are
    named with the first letter of each word capitalized? I mean, in Java,
    it's Object. Whereas in C++, they're quite consistent, standard stuff
    are usually all lowercaps (basic_string, iostream, etc.). Python seems
    to have a mix of both.

    Am I right in assuming that object is object (with lower case 'o')
    because it is built-in, and Exception is not? So when we write our own
    classes, exceptions, etc., we should capitalize it like in Java or C#?
    By the way, what's the convention for functions? It's a bit confusing
    because when I see Python builtins, it seems that they follow C++ style
    (e.g.: has_key, readline). So... does it mean that when I write my own
    function, customarily I'd say myFunction() (or MyFunction()?)

    2. I'm quite baffled that you either have try/except, or try/finally.
    In Java, it is quite common to do this:

    try {
    // something
    } catch(AExceptio n e) {
    // handle
    } catch(BExceptio n e) {
    // handle
    } catch(CExceptio n e) {
    // handle
    } finally {
    // whatever happens, execute this
    }

    It seems that since except and finally are mutually exclusive I can't
    do this. So... what's the usual idiom for this?

    Thanks!
    Ray

  • D H

    #2
    Re: Python's Exception, and Capitalization

    Ray wrote:[color=blue]
    > Hello guys,
    >
    > OK, I've been reading some more about Python. There are some things
    > about Python exception that I haven't been able to grasp:
    >
    > 1. This is a small thing, but why is object spelled "object", and the
    > mother of all exception "Exception" (with capital E)? Why is not object
    > spelled "Object" then?[/color]

    I would guess that object is considered a primitive/basic type like int
    or float or string.

    [color=blue]
    > I mean, in Java,
    > it's Object. Whereas in C++, they're quite consistent, standard stuff
    > are usually all lowercaps (basic_string, iostream, etc.). Python seems
    > to have a mix of both.[/color]

    Yeah java capitalizes anything that is a class like String, Object,
    Integer, and lowercases its primitives like int, byte.

    [color=blue]
    > By the way, what's the convention for functions? It's a bit confusing
    > because when I see Python builtins, it seems that they follow C++ style
    > (e.g.: has_key, readline). So... does it mean that when I write my own
    > function, customarily I'd say myFunction() (or MyFunction()?)[/color]

    Yeah, the python standard library has been built by lots of different
    people. It wasn't designed by one entity using one standard like the
    java standard library or .NET/Mono class library.

    [color=blue]
    > 2. I'm quite baffled that you either have try/except, or try/finally.[/color]

    Apparently that will be fixed sometime:

    Comment

    • Ray

      #3
      Re: Python's Exception, and Capitalization

      D H wrote:[color=blue]
      > Yeah, the python standard library has been built by lots of different
      > people. It wasn't designed by one entity using one standard like the
      > java standard library or .NET/Mono class library.[/color]

      Um, OK, so is it customary in modern Python programs to follow Java
      convention? then methods/functions should be written someMethod() or
      myFunction()?
      [color=blue][color=green]
      > > 2. I'm quite baffled that you either have try/except, or try/finally.[/color]
      >
      > Apparently that will be fixed sometime:
      > http://python.miscellaneousmirror.or.../pep-0341.html[/color]

      Ah, okay. I'm quite surprised to see the date--it was _that_ recent! :)
      But currently, how have you--Python guys who code for a living--been
      handling this case? I know that you can put another try inside a try,
      but obviously the need for that is not common enough in idiomatic
      Python program (or else this would have been a PEP in, say, 2000).

      Cheers
      Ray

      Comment

      • wittempj@hotmail.com

        #4
        Re: Python's Exception, and Capitalization

        For youtr try, except, finally:

        you can construct something like this:
        try:
        try:
        print 'egg' + 1
        except ValueError, e:
        print e
        finally:
        print 'spam'

        It results in:
        py>
        spam

        Traceback (most recent call last):
        File "C:/Martin/test.py", line 3, in -toplevel-
        print 'egg' + 1
        TypeError: cannot concatenate 'str' and 'int' objects
        py>

        If you catch the TypeError you get:


        try:
        try:
        print 'egg' + 1
        except TypeError, e:
        print e
        finally:
        print 'spam'

        py>
        cannot concatenate 'str' and 'int' objects
        spam
        py>

        Comment

        • Aahz

          #5
          Re: Python's Exception, and Capitalization

          In article <1123856890.712 908.247920@o13g 2000cwo.googleg roups.com>,
          Ray <ray_usenet@yah oo.com> wrote:[color=blue]
          >
          >2. I'm quite baffled that you either have try/except, or try/finally.
          >In Java, it is quite common to do this:
          >
          >try {
          > // something
          >} catch(AExceptio n e) {
          > // handle
          >} catch(BExceptio n e) {
          > // handle
          >} catch(CExceptio n e) {
          > // handle
          >} finally {
          > // whatever happens, execute this
          >}
          >
          >It seems that since except and finally are mutually exclusive I can't
          >do this. So... what's the usual idiom for this?[/color]

          Keep in mind that Python actually predates Java. The way this is
          handled now is

          try:
          try:
          except:
          else:
          finally:

          Way back when, Guido thought that it would be confusing to allow both
          except and finally clauses in the same try because people wouldn't know
          what ordering to use (particularly with the else clause).
          --
          Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

          The way to build large Python applications is to componentize and
          loosely-couple the hell out of everything.

          Comment

          • bruno modulix

            #6
            Re: Python's Exception, and Capitalization

            Ray wrote:[color=blue]
            > Hello guys,
            >
            > OK, I've been reading some more about Python. There are some things
            > about Python exception that I haven't been able to grasp:
            >
            > 1. This is a small thing, but why is object spelled "object", and the
            > mother of all exception "Exception" (with capital E)? Why is not object
            > spelled "Object" then?[/color]

            I always wondered too... But then, you'll notice that Python's builtin
            types are usually all lowercase.

            (snip)
            [color=blue]
            > So when we write our own
            > classes, exceptions, etc., we should capitalize it like in Java or C#?[/color]

            Yes, definitively.
            [color=blue]
            > By the way, what's the convention for functions? It's a bit confusing
            > because when I see Python builtins, it seems that they follow C++ style
            > (e.g.: has_key, readline). So... does it mean that when I write my own
            > function, customarily I'd say myFunction() (or MyFunction()?)[/color]

            The all_lower_under score is the common usage, at least in the std lib,
            but feel free to use your favorite convention for this. It's up to you
            as long as you stick to a given convention for a whole project.
            [color=blue]
            > 2. I'm quite baffled that you either have try/except, or try/finally.[/color]
            yes, it's a PITA.
            [color=blue]
            > In Java, it is quite common to do this:[/color]
            (snip)[color=blue]
            > It seems that since except and finally are mutually exclusive I can't
            > do this. So... what's the usual idiom for this?[/color]

            try:
            try:
            do_something()
            except Exception, e:
            handle_error(e)
            else:
            do_this_too()
            finally:
            do_this_whateve r()


            --
            bruno desthuilliers
            python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
            p in 'onurb@xiludom. gro'.split('@')])"

            Comment

            • Steve M

              #7
              Re: Python's Exception, and Capitalization

              You might find the Python Style Guide to be helpful:

              The official home of the Python Programming Language


              Comment

              • Ray

                #8
                Re: Python's Exception, and Capitalization

                Steve M wrote:[color=blue]
                > You might find the Python Style Guide to be helpful:
                >
                > http://www.python.org/doc/essays/styleguide.html[/color]

                Nice! Thanks Steve.

                Ray

                Comment

                • Michael Hudson

                  #9
                  Re: Python's Exception, and Capitalization

                  "Ray" <ray_usenet@yah oo.com> writes:
                  [color=blue]
                  > D H wrote:[color=green]
                  >> Yeah, the python standard library has been built by lots of different
                  >> people. It wasn't designed by one entity using one standard like the
                  >> java standard library or .NET/Mono class library.[/color]
                  >
                  > Um, OK, so is it customary in modern Python programs to follow Java
                  > convention? then methods/functions should be written someMethod() or
                  > myFunction()?
                  >[color=green][color=darkred]
                  >> > 2. I'm quite baffled that you either have try/except, or try/finally.[/color]
                  >>
                  >> Apparently that will be fixed sometime:
                  >> http://python.miscellaneousmirror.or.../pep-0341.html[/color]
                  >
                  > Ah, okay. I'm quite surprised to see the date--it was _that_ recent! :)
                  > But currently, how have you--Python guys who code for a living--been
                  > handling this case? I know that you can put another try inside a try,[/color]

                  It's what I do, and I'm not bothered by it, TBH. Nested try's have an
                  explicitness bonus.

                  Cheers,
                  mwh

                  --
                  Anything that doesn't autonomously leave the fridge, or at the very
                  least waves protest banners, will be eaten. -- Rik Steenwinkel, asr

                  Comment

                  Working...