Newbie question: exceptions in Learning Python
Alex Martelli
aleaxit at yahoo.com
Thu May 3 12:51:08 EDT 2001
"Leighton Pritchard" <lep at aber.ac.uk> wrote in 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.
Alas, the book is assuming too much here -- Python has always had
the freedom to give identical immutable objects the same identity
if and when it notices they're identical.
Just change the binding of ex2 to, say,
ex2 = 'spa' + 'm'
and it should still work as intended in current Python, since
the latter isn't (yet) doing any optimization of constant
expressions.
> Was something changed between Python versions?
Just an implementation detail -- slightly better optimization
in Python 2, with no change to defined-semantics.
> Or am I doing something
> basic and stupid?
No, you're doing absolutely nothing wrong.
Alex
More information about the Python-list
mailing list