Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: math functions with non numeric args

Reply
Thread Tools

Re: math functions with non numeric args

 
 
MRAB
Guest
Posts: n/a
 
      06-30-2013
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'

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: math functions with non numeric args Ian Kelly Python 0 06-30-2013 07:07 PM
math functions with non numeric args Andrew Z Python 1 06-30-2013 06:57 PM
Re: math functions with non numeric args Andrew Berg Python 0 06-30-2013 06:53 PM
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
When passing functions as args,how to pass extra args for passed function? python@sarcastic-horse.com Python 3 09-17-2003 12:25 AM



Advertisments