Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder1.xlned.com!news2.euro.net!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'say,': 0.05; 'builtin': 0.07; 'type,': 0.07; "'w',": 0.09; 'method:': 0.09; 'subject:while': 0.09; 'throws': 0.09; 'sat,': 0.15; "'e',": 0.16; "'o',": 0.16; "'r',": 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happily': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'feb': 0.19; 'holds': 0.20; 'bit': 0.21; 'together.': 0.21; 'errors': 0.23; 'header:In-Reply-To:1': 0.25; 'skip:[ 10': 0.26; 'am,': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'types.': 0.29; 'definition': 0.29; 'objects': 0.29; 'probably': 0.29; "i'm": 0.29; 'sense': 0.31; 'johnson': 0.32; 'not.': 0.32; 'function.': 0.33; 'mixed': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'sequence': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'method': 0.36; 'too': 0.36; 'bad': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'nothing': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'your': 0.60; 'most': 0.61; 'you.': 0.61; 'decided': 0.65; 'sum': 0.66; 'special': 0.73; '/only/': 0.84; '2013': 0.84; 'encounters': 0.84; 'rick': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type:content-transfer-encoding; bh=ruyj7eCWoZA+dDEQKPni7YaU3g0xKa+HCuq8x80pZWQ=; b=LKSLUUhUyOOW9bgqJ3D6P2YW6A8zFnHx4KZ6i9sEWxJKZ/FwzBdM71StBxMwW8VaEw Pte/SJ1T08fOp72QRvFmt6ct4YDD386gq5U1rS/HOnPVrflTFfC8bqrn6MAhSevlTHIM 7pNUgw+Cjm7Cnv60BApFfdDxvBFX80UvicQiYURSviX7MEq/Mzd/v6DEo4CQoC7z3eCC DrN8crKJBi9Zx8AOdWhRUiYt4fciwM8scC0uYbzeFxL/H3R1ap/u4xsAzHVaKz6TqDXU OD2imL1b/v0dIB0IzKwqSKOkSgxP9HIM0J8r4c9/09pTO4J0BI55fNCIx1Fr562VK/uY Cq1Q== MIME-Version: 1.0 X-Received: by 10.58.253.161 with SMTP id ab1mr9082935ved.55.1360368355027; Fri, 08 Feb 2013 16:05:55 -0800 (PST) In-Reply-To: <62c3e7bb-d023-43b4-b759-f424707fd346@googlegroups.com> References: <5002a1f9$0$29995$c3e8da3$5496439d@news.astraweb.com> <7b027612-a07e-40f9-8ad2-3e95c5440482@googlegroups.com> <86872ad2-fda0-403b-9f18-d1cb18e41860@t32g2000yqd.googlegroups.com> <50039290$0$29978$c3e8da3$5496439d@news.astraweb.com> <9309333c-13a0-464c-bd94-9c682363b8c9@googlegroups.com> <511516db$0$29969$c3e8da3$5496439d@news.astraweb.com> <62c3e7bb-d023-43b4-b759-f424707fd346@googlegroups.com> Date: Sat, 9 Feb 2013 11:05:54 +1100 Subject: Re: Implicit conversion to boolean in if and while statements From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360368357 news.xs4all.nl 6889 [2001:888:2000:d::a6]:40837 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38481 On Sat, Feb 9, 2013 at 6:58 AM, Rick Johnson wrote: > I'm a bit unnerved by the sum function. Summing a sequence only makes sen= se if the sequence in question contains /only/ numeric types. For that reas= on i decided to create a special type for holding Numerics. This will proba= bly result in many complaints from lazy people who want to use only one Seq= uence type, which holds mixed types, THEN jamb nothing but numeric types in= to it, THEN have a sum method that throws errors when it encounters a non-n= umeric type!!! I say, too bad for you. Most assuredly not. The sum builtin works happily on any sequence of objects that can be added together. It works as an excellent flatten() method: >>> nested_list =3D [["q"], ["w","e"], ["r","t","u"], ["i","o","p"]] >>> sum(nested_list,[]) ['q', 'w', 'e', 'r', 't', 'u', 'i', 'o', 'p'] >>> nested_list [['q'], ['w', 'e'], ['r', 't', 'u'], ['i', 'o', 'p']] I'm not sure what your definition of a numeric type is, but I suspect that list(str) isn't part of it. ChrisA