integer and string compare, is that correct?
danb_83 at yahoo.com
Mon Jan 11 08:13:55 CET 2010
On Jan 10, 10:34 am, Nobody <nob... at nowhere.com> wrote:
> 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)
> > (type(a).__name__, a) > (type(b).__name__, b)
> The second elements will only be compared if the first elements are equal
> (i.e. the values have the same type).
But this method gives you 3.0 < 2 because 'float' < 'int'. Probably
not what you want.
More information about the Python-list