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!newsfeed1.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'explicitly': 0.05; 'that?': 0.05; 'subject:Python': 0.06; 'float': 0.07; 'difference,': 0.09; 'feature.': 0.09; 'integers': 0.09; 'operand': 0.09; 'pretend': 0.09; 'subject:Why': 0.09; 'types:': 0.09; 'cc:addr:python-list': 0.11; 'behave': 0.16; 'caring': 0.16; 'check.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'integer,': 0.16; 'integers,': 0.16; 'personally,': 0.16; 'so;': 0.16; 'truncates': 0.16; 'typeerror:': 0.16; 'language': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; '>>>': 0.22; 'code,': 0.22; 'example': 0.22; 'cc:addr:python.org': 0.22; 'specify': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'compare': 0.26; 'asking': 0.27; 'header:In-Reply- To:1': 0.27; 'point': 0.28; 'function': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; "skip:' 10": 0.31; '"",': 0.31; 'are.': 0.31; 'bunch': 0.31; 'decimal': 0.31; 'division': 0.31; 'int,': 0.31; 'file': 0.32; 'handled': 0.32; '(most': 0.33; 'sense': 0.34; 'maybe': 0.34; 'could': 0.34; "can't": 0.35; 'skip:s 30': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'sequence': 0.36; 'yield': 0.36; 'subject:?': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'generic': 0.38; 'handle': 0.38; 'little': 0.38; 'recent': 0.39; 'sure': 0.39; 'ensure': 0.60; 'ian': 0.60; 'most': 0.60; 'simply': 0.61; "you're": 0.61; 'sum': 0.64; 'different': 0.65; 'between': 0.67; '20,': 0.68; 'special': 0.74; 'complex,': 0.84; 'difference.': 0.84; 'divide': 0.84; 'float,': 0.84; 'type(s)': 0.84; 'absolutely': 0.87; 'cast': 0.91; 'divided': 0.91; 'to:none': 0.92; 'average': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Ic+7uZH8q69/u1iVgBEdGjkGwW3oX49twXEUBD7YYr8=; b=bsblGltFdc8HdtRcUzdIqRXslNUCFfezIhLmSZUZtWqUCSGlnLKJTCg4A2l81P38BZ EJcjHbX2hlVnUPgSOZZ1OUut9YF6/deCfdbgk5++nfBBCnKs8pVtJHj2TgpvReP6qhHj NVGz0E2yeBXbJ6HbF4NBr+afWYGt7HbO6ixROPhDP379mu245f8BQUyXghFtnl1o4khu WYgk0bdlFWOg0JnVEvHMK4ftkxGZK39Fm6lX3JHI059C4ZH4pmpnLRyCxjCfMN+0SDET y38Kv4bv9CHlMeqetRoL2QelW0JllWBqTUZO1YWMduhHb3qfMK1yvrQq4t/WmZqt1ugB PoFQ== MIME-Version: 1.0 X-Received: by 10.58.105.105 with SMTP id gl9mr25093214veb.3.1397940780700; Sat, 19 Apr 2014 13:53:00 -0700 (PDT) In-Reply-To: References: <7x8ur1esa5.fsf@ruckus.brouhaha.com> Date: Sun, 20 Apr 2014 06:53:00 +1000 Subject: Re: Why Python 3? From: Chris Angelico Cc: Python Content-Type: text/plain; charset=UTF-8 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: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1397940784 news.xs4all.nl 2977 [2001:888:2000:d::a6]:39417 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70399 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: >>> "asdf"*3.0 Traceback (most recent call last): File "", line 1, in "asdf"*3.0 TypeError: can't multiply sequence by non-int of type 'float' 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. Why should the language handle that? It's no different from trying to sum a bunch of different numeric types: >>> sum([1.0,decimal.Decimal("1")]) Traceback (most recent call last): File "", line 1, in sum([1.0,decimal.Decimal("1")]) TypeError: unsupported operand type(s) for +: 'float' and 'decimal.Decimal' The language doesn't specify a means of resolving the conflict between float and Decimal, but for some reason the division of two integers is blessed with a language feature. Again, it would make perfect sense if float were a perfect superset of int, so that you could simply declare that 1.0 and 1 behave absolutely identically in all arithmetic (they already hash and compare equally), but that's not the case, so I don't see that division should try to pretend they are. ChrisA