exception handing

Fredrik Lundh fredrik at pythonware.com
Sat Jun 29 12:33:18 EDT 2002


"Rhymes" wrote:

> I'm reading "Learning Python" and in the section "gotchas"
> of the exception handling chapter the book itself states
> that the exception matching is made by identity not equality.
> It also write an example such as this:
>
> >>> ex1 = "spam"
> >>> ex2 = "spam"
> >>>
> >>> ex1 == ex2, ex1 is ex2
> (1, 0)
>
> <--- here i get (1, 1) ---->

since strings cannot be modified in place, Python is free
to reuse the same string object when it notices that you're
assigning the same literal to two different variables.

Python 2.2 does this for literals that looks like Python
identifiers; Python 1.5 obviously didn't.

to get the effect the authors had in mind, you can do
this instead:

>>> ex1 = "sp am"
>>> ex2 = "sp am"

or

>>> ex1 = "spam"
>>> ex2 = "sp" + "am"

</F>





More information about the Python-list mailing list