integer and string compare, is that correct?
Nobody
nobody at nowhere.com
Sun Jan 10 11:34:46 EST 2010
Hellmut Weber wrote:
>> being a causal python user (who likes the language quite a lot)
>> it took me a while to realize the following:
>> >>> max = '5'
>> >>> n = 5
>> >>> n >= max
>> False
>
>> Section 5.9 Comparison describes this.
>>
>> Can someone give me examples of use cases
Peter Otten wrote:
> The use cases for an order that works across types like int and str are weak
> to non-existent. Implementing it was considered a mistake and has been fixed
> in Python 3:
>>>> 5 > "5"
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: unorderable types: int() > str()
If you actually need to perform comparisons across types, you can rely
upon the fact that tuple comparisons are non-strict and use e.g.:
> a = 5
> b = '5'
> (type(a).__name__, a) < (type(b).__name__, b)
True
> (type(a).__name__, a) > (type(b).__name__, b)
False
The second elements will only be compared if the first elements are equal
(i.e. the values have the same type).
More information about the Python-list
mailing list