Newbie question: exceptions in Learning Python

Gilles Lenfant glenfant.nospam at bigfoot.com
Thu May 3 12:42:16 EDT 2001


You'd better use classes to define your custom exceptions.
More work to design them but more flexibility to handle them...

"Leighton Pritchard" <lep at aber.ac.uk> a écrit dans le message news:
3af18204.1129701034 at news.aber.ac.uk...
> 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