Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70420
| 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 | <python@mrabarnett.plus.com> |
| 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 <python@mrabarnett.plus.com> |
| 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 | <mailman.9344.1397878113.18130.python-list@python.org> <7x8ur1esa5.fsf@ruckus.brouhaha.com> <CAPTjJmqAeuFnOmW890ykPkk4L7JpgYFtFpd6uk1xAXtt2MgBMw@mail.gmail.com> <CALwzidmk9ogErqsF9ghM4_Bi=+n2qwCTpqJ48exOgg+L-2Gd_Q@mail.gmail.com> <CAPTjJmqcr+ZhtRRxGtOVX6ppLd=1RyQ2yU9X=fFy4zH8oLrHyA@mail.gmail.com> <CALwzid=HrxL7kCwM5LOKEgwQT+YzrCH0xV57LXEjqU6jtsti=g@mail.gmail.com> <CAPTjJmrRm7hwb3pn8cj3_5qUKXj5urA2PK4raH2V_dtPWCacUg@mail.gmail.com> <CALwzidnRcrQA9qc=qJpROR106F2EJFr7KzLN9XeyyNXfJW-GSA@mail.gmail.com> <CAPTjJmr89nw1J-Z11=09YexSA74mq0ATt_pxHCqUcJBJZhVEFg@mail.gmail.com> <CALwzidkMqYkEt9XBfKt00SVdVy+jbSzsZNzMfyDhYGNZJNzNXQ@mail.gmail.com> |
| In-Reply-To | <CALwzidkMqYkEt9XBfKt00SVdVy+jbSzsZNzMfyDhYGNZJNzNXQ@mail.gmail.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.9381.1398012258.18130.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On 2014-04-20 17:22, Ian Kelly wrote: > > On Apr 19, 2014 2:54 PM, "Chris Angelico" <rosuav@gmail.com > <mailto:rosuav@gmail.com>> wrote: > > > > On Sun, Apr 20, 2014 at 6:38 AM, Ian Kelly <ian.g.kelly@gmail.com > <mailto:ian.g.kelly@gmail.com>> 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 "<stdin>", line 1, in <module> File "<stdin>", 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. >
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Why Python 3? Anthony Papillion <papillion@gmail.com> - 2014-04-18 22:28 -0500
Re: Why Python 3? Paul Rubin <no.email@nospam.invalid> - 2014-04-18 23:40 -0700
Re: Why Python 3? Chris Angelico <rosuav@gmail.com> - 2014-04-19 17:34 +1000
Re: Why Python 3? Roy Smith <roy@panix.com> - 2014-04-19 09:26 -0400
Re: Why Python 3? Chris Angelico <rosuav@gmail.com> - 2014-04-19 23:42 +1000
Re: Why Python 3? Albert-Jan Roskam <fomcl@yahoo.com> - 2014-04-19 10:57 -0700
Re: Why Python 3? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-20 10:07 +0000
Re: Why Python 3? Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-19 03:25 -0600
Re: Why Python 3? Marko Rauhamaa <marko@pacujo.net> - 2014-04-19 12:59 +0300
Re: Why Python 3? Chris Angelico <rosuav@gmail.com> - 2014-04-19 19:37 +1000
Integer and float division [was Re: Why Python 3?] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-20 11:02 +0000
Re: Integer and float division [was Re: Why Python 3?] Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-04-20 15:38 +0300
Re: Integer and float division [was Re: Why Python 3?] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-20 15:09 +0000
Re: Integer and float division [was Re: Why Python 3?] Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-21 11:44 +1200
Re: Why Python 3? Terry Reedy <tjreedy@udel.edu> - 2014-04-19 13:23 -0400
Re: Why Python 3? Paul Rubin <no.email@nospam.invalid> - 2014-04-19 20:25 -0700
Re: Why Python 3? Ben Finney <ben+python@benfinney.id.au> - 2014-04-20 19:15 +1000
Re: Why Python 3? Walter Hurry <walterhurry@lavabit.com> - 2014-04-20 23:50 +0000
Re: Why Python 3? Chris Angelico <rosuav@gmail.com> - 2014-04-21 10:00 +1000
Re: Why Python 3? HoneyMonster <nobody@someplace.invalid> - 2014-04-21 04:08 +0000
Re: Why Python 3? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-04-21 01:11 +0100
Re: Why Python 3? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-04-19 18:31 +0100
Re: Why Python 3? Chris Angelico <rosuav@gmail.com> - 2014-04-20 03:53 +1000
Re: Why Python 3? Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-19 13:58 -0600
Re: Why Python 3? Chris Angelico <rosuav@gmail.com> - 2014-04-20 06:31 +1000
Re: Why Python 3? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-20 13:06 +1200
Re: Why Python 3? Chris Angelico <rosuav@gmail.com> - 2014-04-20 11:28 +1000
Re: Why Python 3? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-21 10:52 +1200
Re: Why Python 3? Chris Angelico <rosuav@gmail.com> - 2014-04-21 09:24 +1000
Re: Why Python 3? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-21 03:43 +0000
Re: Why Python 3? Chris Angelico <rosuav@gmail.com> - 2014-04-21 14:43 +1000
Re: Why Python 3? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-22 09:58 +1200
Re: Why Python 3? Chris Angelico <rosuav@gmail.com> - 2014-04-21 14:48 +1000
Re: Why Python 3? wxjmfauth@gmail.com - 2014-04-21 02:42 -0700
Re: Why Python 3? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-22 10:28 +1200
Re: Why Python 3? Chris Angelico <rosuav@gmail.com> - 2014-04-22 08:43 +1000
Re: Why Python 3? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-22 18:03 +1200
Re: Why Python 3? Terry Reedy <tjreedy@udel.edu> - 2014-04-20 00:17 -0400
Re: Why Python 3? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-21 11:13 +1200
Re: Why Python 3? Terry Reedy <tjreedy@udel.edu> - 2014-04-20 20:09 -0400
Re: Why Python 3? Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-19 14:38 -0600
Re: Why Python 3? Chris Angelico <rosuav@gmail.com> - 2014-04-20 06:53 +1000
Re: Why Python 3? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-20 13:35 +1200
Re: Why Python 3? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-20 09:59 +0000
Unicode in Python Rustom Mody <rustompmody@gmail.com> - 2014-04-21 20:57 -0700
Re: Unicode in Python Terry Reedy <tjreedy@udel.edu> - 2014-04-22 01:44 -0400
Re: Unicode in Python Rustom Mody <rustompmody@gmail.com> - 2014-04-21 23:18 -0700
Re: Unicode in Python Chris Angelico <rosuav@gmail.com> - 2014-04-22 16:32 +1000
Re: Unicode in Python Steven D'Aprano <steve@pearwood.info> - 2014-04-22 06:11 +0000
Re: Unicode in Python Rustom Mody <rustompmody@gmail.com> - 2014-04-21 23:30 -0700
Re: Unicode in Python Chris Angelico <rosuav@gmail.com> - 2014-04-22 16:44 +1000
Re: Unicode in Python wxjmfauth@gmail.com - 2014-04-22 02:07 -0700
Re: Unicode in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-22 12:21 +0000
Re: Unicode in Python wxjmfauth@gmail.com - 2014-04-22 08:28 -0700
Re: Unicode in Python Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-22 00:31 -0600
Re: Unicode in Python Rustom Mody <rustompmody@gmail.com> - 2014-04-22 02:23 -0700
Re: Unicode in Python Rustom Mody <rustompmody@gmail.com> - 2014-04-22 11:09 -0700
Re: Why Python 3? Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-20 10:22 -0600
Re: Why Python 3? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-21 11:56 +1200
Re: Why Python 3? Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-20 18:29 -0600
Re: Why Python 3? MRAB <python@mrabarnett.plus.com> - 2014-04-20 17:41 +0100
Re: Why Python 3? Chris Angelico <rosuav@gmail.com> - 2014-04-21 02:46 +1000
Re: Why Python 3? Roy Smith <roy@panix.com> - 2014-04-20 14:40 -0700
Re: Why Python 3? Terry Reedy <tjreedy@udel.edu> - 2014-04-20 17:58 -0400
Re: Why Python 3? Richard Damon <Richard@Damon-Family.org> - 2014-04-20 18:02 -0400
Re: Why Python 3? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-21 12:22 +1200
Re: Why Python 3? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-21 02:13 +0000
Re: Why Python 3? Steve Hayes <hayesstw@telkomsa.net> - 2014-04-19 13:53 +0200
Re: Why Python 3? Chris Angelico <rosuav@gmail.com> - 2014-04-19 22:46 +1000
Re: Why Python 3? Rustom Mody <rustompmody@gmail.com> - 2014-04-19 08:59 -0700
Re: Why Python 3? Rick Johnson <rantingrickjohnson@gmail.com> - 2014-04-19 07:41 -0700
Re: Why Python 3? Thomas Lehmann <thomas.lehmann.private@googlemail.com> - 2014-05-06 09:28 -0700
csiph-web