math functions with non numeric args

2013-06-30 Thread Andrew Z
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?

Thank you
AZ
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: math functions with non numeric args

2013-06-30 Thread Andrew Berg
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.
-- 
CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: math functions with non numeric args

2013-06-30 Thread Irmen de Jong
On 30-6-2013 20: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?
 
 Thank you
 AZ
 

Use Python 3.x instead.

 max('-10',10)
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: unorderable types: int()  str()



Irmen

-- 
http://mail.python.org/mailman/listinfo/python-list



Re: math functions with non numeric args

2013-06-30 Thread Ian Kelly
On Sun, Jun 30, 2013 at 12:46 PM, Andrew Z form...@gmail.com 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?

No, it leaves the types as they are but simply considers strings to be
greater than numbers.  As another poster stated, this is fixed in
Python 3, where strings and numbers are considered inorderable.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: math functions with non numeric args

2013-06-30 Thread MRAB

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'

--
http://mail.python.org/mailman/listinfo/python-list