A question on modification of a list via a function invocation
Gregory Ewing
greg.ewing at canterbury.ac.nz
Fri Sep 8 05:00:32 EDT 2017
Marko Rauhamaa wrote:
> I definitely trust that:
>
> a = b
> assert a is b
>
> even when b holds an immutable object.
That's true, but there's rarely a good use case for that
fact that isn't better addressed with an equality comparison
rather than an 'is' test.
As an example, back in the days of string exceptions,
the established idiom was
MyException = "MyException"
try:
...
raise MyException
...
except MyException:
...
Which worked, but it was error-prone. Writing
except "MyException":
instead would *probably* work, but wasn't guaranteed.
If the implementation had matched string exceptions
by equality rather than identity, that wouldn't have
been an issue.
--
Greg
More information about the Python-list
mailing list