Tags: capitalization, exception, grasp1, guys, programming, python, pythons, reading, thingsabout

Pythons Exception, and Capitalization

On Programmer » Python

9,963 words with 8 Comments; publish: Sun, 30 Dec 2007 22:58:00 GMT; (20062.50, « »)

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(AException e) {

// handle

} catch(BException e) {

// handle

} catch(CException 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

All Comments

Leave a comment...

  • 8 Comments
    • Ray wrote:

      > 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?

      I would guess that object is considered a primitive/basic type like int

      or float or string.

      > 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.

      Yeah java capitalizes anything that is a class like String, Object,

      Integer, and lowercases its primitives like int, byte.

      > 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()?)

      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.

      > 2. I'm quite baffled that you either have try/except, or try/finally.

      Apparently that will be fixed sometime:

      http://python.miscellaneousmirror.o...s/pep-0341.html

      #1; Sun, 30 Dec 2007 22:59:00 GMT
    • D H wrote:

      > 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.

      Um, OK, so is it customary in modern Python programs to follow Java

      convention? then methods/functions should be written someMethod() or

      myFunction()?

      > > 2. I'm quite baffled that you either have try/except, or try/finally.

      > Apparently that will be fixed sometime:

      > http://python.miscellaneousmirror.o...s/pep-0341.html

      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

      #2; Sun, 30 Dec 2007 23:00:00 GMT
    • 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

      #3; Sun, 30 Dec 2007 23:01:00 GMT
    • In article <1123856890.712908.247920.python.todaysummary.com.o13g2000cwo.googlegroups. com>,

      Ray <ray_usenet.python.todaysummary.com.yahoo.com> wrote:

      >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(AException e) {

      > // handle

      >} catch(BException e) {

      > // handle

      >} catch(CException 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?

      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.python.todaysummary.com.pythoncraft.com) <*> http://www.pythoncraft.com/

      The way to build large Python applications is to componentize and

      loosely-couple the hell out of everything.

      #4; Sun, 30 Dec 2007 23:02:00 GMT
    • Ray wrote:

      > 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?

      I always wondered too... But then, you'll notice that Python's builtin

      types are usually all lowercase.

      (snip)

      > So when we write our own

      > classes, exceptions, etc., we should capitalize it like in Java or C#?

      Yes, definitively.

      > 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()?)

      The all_lower_underscore 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.

      > 2. I'm quite baffled that you either have try/except, or try/finally.

      yes, it's a PITA.

      > In Java, it is quite common to do this:

      (snip)

      > It seems that since except and finally are mutually exclusive I can't

      > do this. So... what's the usual idiom for this?

      try:

      try:

      do_something()

      except Exception, e:

      handle_error(e)

      else:

      do_this_too()

      finally:

      do_this_whatever()

      --

      bruno desthuilliers

      python -c "print '.python.todaysummary.com.'.join(['.'.join([w[::-1] for w in p.split('.')]) for

      p in 'onurb.python.todaysummary.com.xiludom.gro'.split('.python.todaysummary.com.')])"

      #5; Sun, 30 Dec 2007 23:03:00 GMT
    • "Ray" <ray_usenet.python.todaysummary.com.yahoo.com> writes:

      > D H wrote:

      >> 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.

      > Um, OK, so is it customary in modern Python programs to follow Java

      > convention? then methods/functions should be written someMethod() or

      > myFunction()?

      >> > 2. I'm quite baffled that you either have try/except, or try/finally.

      >>

      >> Apparently that will be fixed sometime:

      >> http://python.miscellaneousmirror.o...s/pep-0341.html

      > 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,

      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

      #8; Sun, 30 Dec 2007 23:06:00 GMT