'1' + 1 ==> True ???

Ben Finney bignose-hates-spam at and-benfinney-does-too.id.au
Thu Mar 18 03:34:14 CET 2004


On Thu, 18 Mar 2004 03:45:56 +0100, Nicola Mingotti wrote:
> I obtained this result : 
> ------
>>>> '1' > 1 
> True
> ------
> with python2.2.3 and 2.3.3 .
> (Yes , in python 2.2 i get 1 instead of True)

This should not be surprising; the values are different, so one will be
"greater than" the other; but comparing them is not of much use.

> but 
> ------
>>>> '1' + 1
> TypeError 
> -------

This, too, should not be surprising.  Adding a character and a number
has no unambiguously correct meaning.

> and 
> -----------------------------
>>>>'1'.__gt__.__doc__
> 'x.__gt__(y) <==> x>y'
> -----------------------------

Showing that the comparison is pretty much what you'd expect.  Again,
though, the result of comparing different types like character and
integer isn't particularly useful.

> so i'm a little puzzled :)
>
> Is this a bug or is there a very good reason to this strange behaviour

What behaviour did you expect?  Without knowing that it's hard to
explain why the reality is different.

-- 
 \       "Judge: A law student who marks his own papers."  -- Henry L. |
  `\                                                           Mencken |
_o__)                                                                  |
Ben Finney <http://bignose.squidly.org/>



More information about the Python-list mailing list