Python's Exception, and Capitalization

R

Ray

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
 
D

D H

Ray said:
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.org/peps/pep-0341.html
 
R

Ray

D said:
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()?

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
 
W

wittempj

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

Aahz

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

bruno modulix

Ray said:
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()
 
M

Michael Hudson

Ray said:
Um, OK, so is it customary in modern Python programs to follow Java
convention? then methods/functions should be written someMethod() or
myFunction()?


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top