Python's Exception, and Capitalization

bruno modulix onurb at xiludom.gro
Fri Aug 12 12:15:27 EDT 2005


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 '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list