comparing str's with ints

John Roth johnroth at ameritech.net
Mon Apr 29 15:25:44 EDT 2002


"Andrew Dalke" <dalke at dalkescientific.com> wrote in message
news:aak5n9$efr$1 at slb6.atl.mindspring.net...
> Frank Tobin:
> >What in the world is supposed to be the semantics of comparing a
string
> >with an integer?  Seems like a type trap...
>
> As I understand the history, earlier versions of Python didn't support
> throwing exceptions from an internal cmp().  Instead, it used a
> well-defined but arbitrary comparison scheme to handle failures.
> Though that scheme kept being tweaked as mismatches were found between
> it and expectations.
>
> I agree that it's a type trap.
>

Exactly. As I understand it, the semantics of comparing
two dissimilar types are quite simple. They are supposed
to provide consistent results on any given release, but
not otherwise. Consistent simply means that the same
comparison should provide the same result, not that it
should mean anything to anyone other than the interpreter.

In other words, don't do that.

If you really, truely have to do Perl type comparisons
between integers and strings, you might consider
deriving your own integer and string types, and providing
a custom __cmp__() method.

John Roth






More information about the Python-list mailing list