Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'win32': 0.03; 'skip:[ 20': 0.04; 'explicitly': 0.05; 'interpreter': 0.05; 'subject:Python': 0.06; 'sys': 0.07; 'test,': 0.07; 'difference,': 0.09; 'function,': 0.09; 'latter': 0.09; 'subject:Why': 0.09; 'python': 0.11; 'def': 0.12; 'caring': 0.16; 'check.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'inf': 0.16; 'integer,': 0.16; 'integers,': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'numpy': 0.16; 'personally,': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'so;': 0.16; 'truncates': 0.16; 'typeerror:': 0.16; 'types,': 0.16; 'language': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'bit': 0.19; '>>>': 0.22; 'code,': 0.22; 'example': 0.22; 'import': 0.22; 'header:User- Agent:1': 0.23; "shouldn't": 0.24; "i've": 0.25; 'first,': 0.26; 'this:': 0.26; 'pass': 0.26; 'asking': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'correct': 0.29; 'am,': 0.29; 'raise': 0.29; "doesn't": 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'code': 0.31; '"",': 0.31; 'decimal': 0.31; 'equivalent.': 0.31; 'second,': 0.31; 'file': 0.32; 'probably': 0.32; 'handled': 0.32; '(most': 0.33; 'maybe': 0.34; 'case,': 0.35; 'received:84': 0.35; 'but': 0.35; 'in:': 0.36; 'yield': 0.36; 'subject:?': 0.36; 'should': 0.36; 'detail': 0.37; 'two': 0.37; 'list': 0.37; 'generic': 0.38; 'handle': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'little': 0.38; 'recent': 0.39; 'expect': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'ensure': 0.60; 'ian': 0.60; 'worry': 0.60; 'most': 0.60; "you're": 0.61; 'back': 0.62; 'more': 0.64; 'skip:1 20': 0.65; 'mar': 0.68; '20,': 0.68; 'fact,': 0.69; 'special': 0.74; '2014,': 0.84; 'complex,': 0.84; 'difference.': 0.84; 'divide': 0.84; 'float,': 0.84; 'sum.': 0.84; 'cast': 0.91; 'divided': 0.91; 'average': 0.93 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=YfUz5mhf c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=tLkI8qN_j18A:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=pGLkceISAAAA:8 a=Gn18xHyPcN4QI0HM9F8A:9 a=SbRvrOGoMU2M7WUy:21 a=Dd4kmHi6v2jBtOXH:21 a=QEXdDO2ut3YA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Sun, 20 Apr 2014 17:41:09 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Why Python 3? References: <7x8ur1esa5.fsf@ruckus.brouhaha.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 80 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1398012258 news.xs4all.nl 2895 [2001:888:2000:d::a6]:40500 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70420 On 2014-04-20 17:22, Ian Kelly wrote: > > On Apr 19, 2014 2:54 PM, "Chris Angelico" > wrote: > > > > On Sun, Apr 20, 2014 at 6:38 AM, Ian Kelly > wrote: > > >> Or you just cast one of them to float. That way you're sure you're > > >> working with floats. > > > > > > Which is inappropriate if the type passed in was a Decimal or a > complex. > > > > In that case, you already have a special case in your code, so whether > > that special case is handled by the language or by your code makes > > little difference. Is your function so generic that it has to be able > > to handle float, Decimal, or complex, and not care about the > > difference, and yet has to ensure that int divided by int doesn't > > yield int? Then say so; put in that special check. Personally, I've > > yet to meet any non-toy example of a function that needs that exact > > handling; most code doesn't ever think about complex numbers, and a > > lot of things look for one specific type: > > When I'm writing a generic average function, I probably don't know > whether it will ever be used to average complex numbers. That shouldn't > matter, because I should be able to rely on this code working for > whatever numeric type I pass in: > > def average(values): > return sum(values) / len(values) > > This works for decimals, it works for fractions, it works for complex > numbers, it works for numpy types, and in Python 3 it works for ints. > > > Maybe it's not your code that should be caring about what happens when > > you divide two integers, but the calling code. If you're asking for > > the average of a list of numbers, and they're all integers, and the > > avg() function truncates to integer, then the solution is to use sum() > > and explicitly cast to floating point before dividing. > > First, that's not equivalent. Try the following in Python 3: > > values = [int(sys.float_info.max / 10)] * 20 > print(average(values)) > > Now try this: > > print(average(map(float, values))) > > I don't have an interpreter handy to test, but I expect the former to > produce the correct result and the latter to raise OverflowError on the > call to sum. > Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> def average(values): ... return sum(values) / len(values) ... >>> values = [int(sys.float_info.max / 10)] * 20 >>> print(average(values)) 1.7976931348623158e+307 >>> print(average(map(float, values))) Traceback (most recent call last): File "", line 1, in File "", line 2, in average TypeError: object of type 'map' has no len() >>> print(average(list(map(float, values)))) inf >>> In fact, that's true back to Python 3.1 > Second, why should the calling code have to worry about this > implementation detail anyway? The point of a generic function is that > it's generic. >