Newbie question: exceptions in Learning Python

Leighton Pritchard lep at aber.ac.uk
Thu May 3 12:06:54 EDT 2001


Hi,

I'm working through Lutz & Ascher's great book 'Learning Python', and
I'm trying to run through the exception examples they give. Sadly, one
of these isn't giving quite the response printed in the book.

On p.209 the example runs:
>>> ex1 = "spam"
>>> ex2 = "spam"
>>> ex1 == ex2, ex1 is ex2
(1, 0)

to demonstrate that matching is by identity, not equality.

However, when I run this on Python 2.0 under WindowsNT, I get the
following:

>>> ex1 = "spam"
>>> ex2 = "spam"
>>> ex1 == ex2, ex1 is ex2
(1, 1)
>>> ex1
'spam'
>>> ex2
'spam'
>>> ex1 is ex2
1

So now, when I try raising an exception with an equal but not
identical object it doesn't fail how it's supposed to:

>>> try: raise ex1
except ex1: print "got it"

got it
>>> try: raise ex1
except ex2: print "got it"

got it

Was something changed between Python versions? Or am I doing something
basic and stupid?
Leighton Pritchard
lep at aber.ac.uk



More information about the Python-list mailing list