Less than is more than ?
Dave Brueck
dave at solussoftware.com
Fri Dec 20 12:05:01 EST 2002
On Fri, 20 Dec 2002, Andrew Thompson wrote:
> I just got bitten by 100 < '10' returning 1 -- or true.
>
> Obviously I meant to use string.atoi() here,
.. or just int(x) or float(x) or whatever ...
> but problem is that my program compiled and ran happily!
And it should! This also works:
class Foo: pass
f = Foo()
f < '5'
> 100.__cmp__('foo') raises an Exception, as expected.
Yeah, but the Exception is a syntax error exception, right? ;-)
>>> 100.__cmp__('foo')
File "<stdin>", line 1
100.__cmp__('foo')
^
SyntaxError: invalid syntax
If you tried:
100 .__cmp__('foo')
then you would get a TypError, but you would also get a TypeError for
100 .__cmp__('10')
too.
> Have I missed something here?
Just the documentation :) :
5.6 Comparing Sequences and Other Types
"Note that comparing objects of different types is legal. The outcome is
deterministic but arbitrary: the types are ordered by their name. ...
Mixed numeric types are compared according to their numeric value, so 0
equals 0.0, etc."
Note that __cmp__ gets called only if the class specifically implements it
and an appropriate rich comparison operator (e.g. __gt__) is not defined.
HTH,
Dave
More information about the Python-list
mailing list