Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49510

Re: math functions with non numeric args

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'anyway.': 0.05; 'explicit': 0.07; 'string': 0.09; '-10': 0.09; 'ascii': 0.09; 'converts': 0.09; 'integers': 0.09; 'types:': 0.09; 'python': 0.11; 'bye': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'str()': 0.16; 'subject:non': 0.16; 'typeerror:': 0.16; 'with?': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'comparing': 0.24; "shouldn't": 0.24; 'header:In-Reply-To:1': 0.27; 'fixed': 0.29; "doesn't": 0.30; 'andrew': 0.30; 'characters': 0.30; 'strongly': 0.30; 'gives': 0.31; '"",': 0.31; '>>>>': 0.31; 'another.': 0.31; 'file': 0.32; '(most': 0.33; 'guess': 0.33; 'subject:with': 0.35; 'something': 0.35; 'case,': 0.35; 'convert': 0.35; 'objects': 0.35; 'but': 0.35; 'consistent': 0.36; 'to:addr :python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'read': 0.60; 'more': 0.64; 'different': 0.65; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'behavior': 0.77; 'reply-to:addr:python.org': 0.84; 'dealt': 0.91
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=KrN0hwmN c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=K2DDQYBT4xIA:10 a=kqmNAzXxcqYA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=2yUZsYxkAUsA:10 a=kChZ7zzEzi27511D9IwA:9 a=wPNLvfGTeEIA:10
X-AUTH mrabarnett:2500
Date Sun, 30 Jun 2013 20:12:19 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130620 Thunderbird/17.0.7
MIME-Version 1.0
To python-list@python.org
Subject Re: math functions with non numeric args
References <CAPrcKXLzK1A=y6B6Z5srroT5tnyd1kMO43GS=2mxGpvtxK73xQ@mail.gmail.com> <51D07EC3.7030307@gmail.com>
In-Reply-To <51D07EC3.7030307@gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4039.1372619537.3114.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1372619537 news.xs4all.nl 15976 [2001:888:2000:d::a6]:37286
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:49510

Show key headers only | View raw


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'

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: math functions with non numeric args MRAB <python@mrabarnett.plus.com> - 2013-06-30 20:12 +0100

csiph-web