473,796 Members | 2,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python's Exception, and Capitalization

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

Aug 12 '05 #1
8 1982
D H
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.or.../pep-0341.html
Aug 12 '05 #2
Ray
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.or.../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

Aug 12 '05 #3
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>

Aug 12 '05 #4
In article <11************ **********@o13g 2000cwo.googleg roups.com>,
Ray <ra********@yah oo.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(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?


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 (aa**@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.
Aug 12 '05 #5
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_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.
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_whateve r()
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Aug 12 '05 #6
You might find the Python Style Guide to be helpful:

http://www.python.org/doc/essays/styleguide.html

Aug 12 '05 #7
Ray
Steve M wrote:
You might find the Python Style Guide to be helpful:

http://www.python.org/doc/essays/styleguide.html


Nice! Thanks Steve.

Ray

Aug 13 '05 #8
"Ray" <ra********@yah oo.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.or.../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
Aug 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1365
by: PT | last post by:
Hi, I'm not looking to get into a debate about case-sensitive vs. insensitive programming languages, but I was looking for suggestions about how it might be possible to add a hook to the python parser similar to the Apache mod_speling module (links below). So that for example if there is a NameError exception, python walks the globals() and locals() to see if it might be just a minor spelling/capitalization error. I can see how this...
4
1441
by: John J. Lee | last post by:
Anybody know when 2.4b1 is currently planned for release? I'm sure I saw somebody in python-dev mention a date, but I can't find that post now. John
2
2093
by: Terry Reedy | last post by:
According to PCGamer, Jan05, p52: "*Civilization IV* has been designed to fully support the mod community. The game is written using flexible XML data files and the Python scripting language so modders will have no trouble at all creating their own personalized worlds, units, techniques, and historical events. Advanced modders will even be able to control the AI." Other commercial games have use Python as an undocumented private...
5
1890
by: Xah Lee | last post by:
python has this nice unicodedata module that deals with unicode nicely. #-*- coding: utf-8 -*- # python from unicodedata import * # each unicode char has a unique name. # one can use the “lookup” func to find it
6
10241
by: Corepaul | last post by:
I am new to Access 2000. My operating system is Windows 2000. In the early stage of development I noticed something weird. On my form, I have a Command Button named "btnAlbumUp". The first time that I reference this button in VBA code, the Visual Basic Editor capitalizes the P changing btnAlbumUp.SetFocus to
28
4337
by: liorm | last post by:
Hi everyone, I need to write a web app, that will support millions of user accounts, template-based user pages and files upload. The client is going to be written in Flash. I wondered if I coudl get your opinions - what do you think is the best language to use for the server? Python or Java? And I'm talking scalability, object oriented, development tools etc. Thansk for any idea! I'd love to hear it Happy New 2006,
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10237
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7553
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.