Re: math functions with non numeric args

Discussion in 'Python' started by MRAB, Jun 30, 2013.

  1. MRAB

    MRAB Guest

    On 30/06/2013 19:53, Andrew Berg wrote:
    > On 2013.06.30 13:46, Andrew Z wrote:
    >> Hello,
    >>
    >> print max(-10, 10)
    >> 10
    >> print max('-10', 10)
    >> -10
    >>
    >> My guess max converts string to number bye decoding each of the characters to it's ASCII equivalent?
    >>
    >> Where can i read more on exactly how the situations like these are dealt with?

    > This behavior is fixed in Python 3:
    >
    >>>> max('10', 10)

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > TypeError: unorderable types: int() > str()
    >
    > Python is strongly typed, so it shouldn't magically convert something from one type to another.
    > Explicit is better than implicit.
    >

    It doesn't magically convert anyway.

    In Python 2, comparing objects of different types like that gives a
    consistent but arbitrary result: in this case, bytestrings ('str') are
    greater than integers ('int'):

    >>> max('-10', 10)

    '-10'
    >>> max('10', -10)

    '10'
     
    MRAB, Jun 30, 2013
    #1
    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. Replies:
    3
    Views:
    537
    David Eppstein
    Sep 17, 2003
  2. VK
    Replies:
    15
    Views:
    1,321
    Dr J R Stockton
    May 2, 2010
  3. Andrew Z
    Replies:
    1
    Views:
    104
    Irmen de Jong
    Jun 30, 2013
  4. Andrew Berg
    Replies:
    0
    Views:
    114
    Andrew Berg
    Jun 30, 2013
  5. Ian Kelly
    Replies:
    0
    Views:
    114
    Ian Kelly
    Jun 30, 2013
Loading...

Share This Page