integer and string compare, is that correct?

Discussion in 'Python' started by Hellmut Weber, Jan 10, 2010.

  1. Hi,
    being a causal python user (who likes the language quite a lot)
    it took me a while to realize the following:


    leo@sylvester py_count $ python
    Python 2.6.3 (r263:75183, Oct 26 2009, 12:34:23)
    [GCC 4.4.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> max = '5'
    >>> n = 5
    >>> n >= max

    False
    >>> n + max

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for +: 'int' and 'str'
    >>>



    Section 5.9 Comparison describes this.

    Can someone give me examples of use cases

    TIA

    Hellmut

    --
    Dr. Hellmut Weber
    Degenfeldstraße 2 tel +49-89-3081172
    D-80803 München-Schwabing mobil +49-172-8450321
    please: No DOCs, no PPTs. why: tinyurl.com/cbgq
    Hellmut Weber, Jan 10, 2010
    #1
    1. Advertising

  2. Hellmut Weber

    Peter Otten Guest

    Hellmut Weber wrote:

    > being a causal python user (who likes the language quite a lot)
    > it took me a while to realize the following:
    >
    >
    > leo@sylvester py_count $ python
    > Python 2.6.3 (r263:75183, Oct 26 2009, 12:34:23)
    > [GCC 4.4.1] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    > >>> max = '5'
    > >>> n = 5
    > >>> n >= max

    > False


    > Section 5.9 Comparison describes this.
    >
    > Can someone give me examples of use cases


    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:

    Python 3.1.1+ (r311:74480, Nov 2 2009, 15:45:00)
    [GCC 4.4.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 5 > "5"

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: unorderable types: int() > str()

    Checking for equality is still possible:

    >>> 5 == "5"

    False

    Peter
    Peter Otten, Jan 10, 2010
    #2
    1. Advertising

  3. Hellmut Weber

    Nobody Guest

    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).
    Nobody, Jan 10, 2010
    #3
  4. Hellmut Weber

    Peter Otten Guest

    Somebody wrote:

    > 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).


    The same type *name*. To play it safe you'll have to compare the id, too:

    >>> items = []
    >>> for i in range(2):

    .... class A: pass
    .... items.append(A())
    ....
    >>> sorted(items, key=lambda a: (type(a).__name__, a))

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: unorderable types: A() < A()
    >>> sorted(items, key=lambda a: (type(a).__name__, id(type(a)), a))

    [<__main__.A object at 0x14dfbd0>, <__main__.A object at 0x14dfc50>]

    Peter
    Peter Otten, Jan 10, 2010
    #4
  5. Hellmut Weber

    Dan Bishop Guest

    On Jan 10, 10:34 am, Nobody <> 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)
    >         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).


    But this method gives you 3.0 < 2 because 'float' < 'int'. Probably
    not what you want.
    Dan Bishop, Jan 11, 2010
    #5
  6. Hellmut Weber

    Marco Salden Guest

    On Jan 10, 1:26 pm, Hellmut Weber <> wrote:
    > Hi,
    > being a causal python user (who likes the language quite a lot)
    > it took me a while to realize the following:
    >
    > leo@sylvester py_count $ python
    > Python 2.6.3 (r263:75183, Oct 26 2009, 12:34:23)
    > [GCC 4.4.1] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    >  >>> max = '5'
    >  >>> n = 5
    >  >>> n >= max
    > False
    >  >>> n + max
    > Traceback (most recent call last):
    >    File "<stdin>", line 1, in <module>
    > TypeError: unsupported operand type(s) for +: 'int' and 'str'
    >  >>>
    >
    > Section 5.9 Comparison describes this.
    >
    > Can someone give me examples of use cases
    >
    > TIA
    >
    > Hellmut
    >
    > --
    > Dr. Hellmut Weber        
    > Degenfeldstraße 2         tel   +49-89-3081172
    > D-80803 München-Schwabing mobil +49-172-8450321
    > please: No DOCs, no PPTs. why: tinyurl.com/cbgq


    I would say you want to compare semantically an integer value with an
    integer value so why not:
    IDLE 1.1.3
    >>> max = '5'
    >>> n = 5
    >>> n==(int(max))

    True
    >>>

    ?
    (in Python 2.4...)

    Regards,
    Marco
    Marco Salden, Jan 11, 2010
    #6
  7. Hellmut Weber

    Nobody Guest

    On Sun, 10 Jan 2010 23:13:55 -0800, Dan Bishop wrote:

    >> 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).

    >
    > But this method gives you 3.0 < 2 because 'float' < 'int'. Probably
    > not what you want.


    If you're comparing instances of entirely arbitrary types, what
    you probably want (and the only thing you're going to get) is an
    entirely arbitrary ordering.

    The main case where such a comparison makes sense is for algorithms which
    require a total ordering (e.g. tree-like structures), and those won't care
    if 3<2 so long as the axioms for a total ordering hold.
    Nobody, Jan 11, 2010
    #7
  8. Nobody <> writes:

    > On Sun, 10 Jan 2010 23:13:55 -0800, Dan Bishop wrote:
    >
    >>> 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).

    >>
    >> But this method gives you 3.0 < 2 because 'float' < 'int'. Probably
    >> not what you want.

    >
    > If you're comparing instances of entirely arbitrary types, what
    > you probably want (and the only thing you're going to get) is an
    > entirely arbitrary ordering.
    >
    > The main case where such a comparison makes sense is for algorithms which
    > require a total ordering (e.g. tree-like structures), and those won't care
    > if 3<2 so long as the axioms for a total ordering hold.


    It won't work for several reasons. Offhand I can think of two:

    1. lists, tuples:

    >>> [1] < ['a']

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: unorderable types: int() < str()

    2. Partially ordered or unordered builtin types:

    >>> 1j < 1+1j

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: no ordering relation is defined for complex numbers

    --
    Arnaud
    Arnaud Delobelle, Jan 11, 2010
    #8
  9. Hellmut Weber

    Aahz Guest

    In article <hickp8$cfg$00$-online.com>,
    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:


    That is not precisely correct from my POV. The primary use case for
    order that works across types is sorting lists of heterogeneous data.
    Many people relied on that feature; however, experience showed that it
    caused more bugs than it fixed. But that doesn't obviate the use of the
    feature.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "If you think it's expensive to hire a professional to do the job, wait
    until you hire an amateur." --Red Adair
    Aahz, Jan 19, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joon
    Replies:
    1
    Views:
    515
    Roedy Green
    Jul 8, 2003
  2. Dan

    correct or not correct?

    Dan, Oct 2, 2003, in forum: HTML
    Replies:
    7
    Views:
    441
  3. Replies:
    10
    Views:
    771
    Roland Pibinger
    Jan 24, 2007
  4. J.Ram
    Replies:
    7
    Views:
    652
  5. Randy Kramer
    Replies:
    12
    Views:
    374
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page