Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20756
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ian.g.kelly@gmail.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; 'own.': 0.03; 'behavior,': 0.07; 'ignored': 0.07; 'necessary,': 0.09; 'received:mail- lpp01m010-f46.google.com': 0.09; 'though:': 0.09; 'def': 0.13; 'argument': 0.15; 'ideally': 0.15; 'result,': 0.15; '*args)': 0.16; '*args):': 0.16; '2:38': 0.16; 'iterable,': 0.16; 'subject:() ': 0.16; 'subject:sum': 0.16; 'ways:': 0.16; 'cc:addr :python-list': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'subject:not': 0.19; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'feb': 0.22; 'produces': 0.23; 'though.': 0.23; 'expect': 0.25; 'cc:2**0': 0.26; 'all,': 0.27; "i'm": 0.28; 'second': 0.28; 'message-id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'subject:number': 0.30; 'zero.': 0.30; 'received:209.85.215.46': 0.32; 'thu,': 0.32; "isn't": 0.33; 'this.': 0.33; 'operations': 0.35; 'starting': 0.36; 'two': 0.36; 'passed': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'replace': 0.38; 'should': 0.38; 'received:209.85.215': 0.39; 'received:209': 0.39; 'header:Received:6': 0.61; 'your': 0.61; 'proposal': 0.68; 'superior': 0.74; 'sum': 0.74; '12)': 0.84; 'buck': 0.84; 'summing': 0.84 |
| Received-SPF | pass (google.com: domain of ian.g.kelly@gmail.com designates 10.112.29.34 as permitted sender) client-ip=10.112.29.34; |
| Authentication-Results | mr.google.com; spf=pass (google.com: domain of ian.g.kelly@gmail.com designates 10.112.29.34 as permitted sender) smtp.mail=ian.g.kelly@gmail.com; dkim=pass header.i=ian.g.kelly@gmail.com |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=kfSt7MPg99jR4N5OMkSECeAAghz0Sf0OOG3zgSRGzbs=; b=tpV5F6ahkHLRWymDoaameQQgcsOc/IaY6Juommj33ITQDFquHE/Ol4nArGuld39oUN rw/O8ZovQs5itIMuTIW3frRCBId8CyaS6w1U44UMcLKDjOPXneriZE+tXjo+AXbIyu6V i3pq4BI2LIJ71GBTX5q1tCh6M7J1GKwXBVe9M= |
| MIME-Version | 1.0 |
| In-Reply-To | <afd029f0-1047-496c-a734-6e4be6ed7d8f@i10g2000pbc.googlegroups.com> |
| References | <91c71e41-b98c-46f6-b3af-bed894cf9d92@kh11g2000pbb.googlegroups.com> <mailman.89.1330032768.3037.python-list@python.org> <afd029f0-1047-496c-a734-6e4be6ed7d8f@i10g2000pbc.googlegroups.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Thu, 23 Feb 2012 14:54:22 -0700 |
| Subject | Re: sum() requires number, not simply __add__ |
| To | Buck Golemon <buck@yelp.com> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| 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.93.1330034094.3037.python-list@python.org> (permalink) |
| Lines | 31 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1330034094 news.xs4all.nl 6947 [2001:888:2000:d::a6]:58791 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:20756 |
Show key headers only | View raw
On Thu, Feb 23, 2012 at 2:38 PM, Buck Golemon <buck@yelp.com> wrote:
> My proposal is still *slightly* superior in two ways:
>
> 1) It reduces the number of __add__ operations by one
> 2) The second argument isn't strictly necessary, if you don't mind
> that the 'null sum' will produce zero.
It produces the wrong result, though:
>>> sum([3,4], base=12)
7
If I'm starting with 12 and summing 3 and 4, I expect to get 19.
Ideally the second argument should be ignored only if it isn't passed
in at all, and I don't know off-hand why the built-in sum doesn't do
this. We really don't need to replace it, though. If you want a
different sum behavior, just write your own.
def sum(iterable, *args):
return reduce(operator.add, iterable, *args)
>>> sum([3,4])
7
>>> sum([3,4], 12)
19
>>> sum(['hello', 'world'])
'helloworld'
Cheers,
Ian
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
sum() requires number, not simply __add__ Buck Golemon <buck@yelp.com> - 2012-02-23 13:19 -0800
Re: sum() requires number, not simply __add__ Buck Golemon <buck@yelp.com> - 2012-02-23 13:23 -0800
Re: sum() requires number, not simply __add__ Arnaud Delobelle <arnodel@gmail.com> - 2012-02-23 21:41 +0000
Re: sum() requires number, not simply __add__ Chris Angelico <rosuav@gmail.com> - 2012-02-24 08:53 +1100
Re: sum() requires number, not simply __add__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-23 23:33 +0000
Re: sum() requires number, not simply __add__ Chris Angelico <rosuav@gmail.com> - 2012-02-24 10:39 +1100
Re: sum() requires number, not simply __add__ Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2012-02-24 12:41 +0100
Re: sum() requires number, not simply __add__ Roy Smith <roy@panix.com> - 2012-02-24 08:23 -0500
Re: sum() requires number, not simply __add__ Terry Reedy <tjreedy@udel.edu> - 2012-02-24 16:58 -0500
Re: sum() requires number, not simply __add__ Arnaud Delobelle <arnodel@gmail.com> - 2012-02-23 21:59 +0000
Re: sum() requires number, not simply __add__ Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-23 15:00 -0700
Re: sum() requires number, not simply __add__ Chris Angelico <rosuav@gmail.com> - 2012-02-24 09:04 +1100
Re: sum() requires number, not simply __add__ Arnaud Delobelle <arnodel@gmail.com> - 2012-02-23 22:09 +0000
Re: sum() requires number, not simply __add__ Arnaud Delobelle <arnodel@gmail.com> - 2012-02-23 21:32 +0000
Re: sum() requires number, not simply __add__ Chris Rebert <clp2@rebertia.com> - 2012-02-23 13:32 -0800
Re: sum() requires number, not simply __add__ Buck Golemon <buck@yelp.com> - 2012-02-23 13:38 -0800
Re: sum() requires number, not simply __add__ Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-23 14:54 -0700
Re: sum() requires number, not simply __add__ Stefan Behnel <stefan_ml@behnel.de> - 2012-02-23 22:42 +0100
Re: sum() requires number, not simply __add__ Duncan Booth <duncan.booth@invalid.invalid> - 2012-02-24 11:40 +0000
Re: sum() requires number, not simply __add__ Peter Otten <__peter__@web.de> - 2012-02-24 09:29 +0100
csiph-web