Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'anyway.': 0.04; 'say,': 0.05; 'semantic': 0.07; 'sequences.': 0.07; 'type,': 0.07; '(1,': 0.09; '3),': 0.09; 'subclass': 0.09; 'subject:while': 0.09; 'substitution': 0.09; 'throws': 0.09; 'cc:addr:python-list': 0.10; '"things': 0.16; 'advocating': 0.16; 'design?': 0.16; 'hierarchy': 0.16; 'operator.': 0.16; 'serve.': 0.16; 'violated': 0.16; 'wrote:': 0.17; 'input': 0.18; 'feb': 0.19; 'equivalent': 0.20; 'holds': 0.20; 'proposed': 0.20; 'trying': 0.21; 'bit': 0.21; 'meant': 0.21; 'fine,': 0.22; 'cc:2**0': 0.23; 'errors': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'guess': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'oop': 0.29; 'types.': 0.29; 'probably': 0.29; 'class': 0.29; "i'm": 0.29; 'fri,': 0.30; 'function': 0.30; 'sense': 0.31; 'johnson': 0.32; 'could': 0.32; 'function.': 0.33; 'mixed': 0.33; 'received:google.com': 0.34; 'sequence': 0.35; 'so,': 0.35; 'pm,': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'really': 0.36; 'but': 0.36; 'method': 0.36; 'too': 0.36; 'bad': 0.37; 'does': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'nothing': 0.38; 'apply': 0.39; 'your': 0.60; 'you.': 0.61; "you've": 0.61; 'share': 0.61; 'skip:n 10': 0.63; 'here': 0.65; 'decided': 0.65; 'sum': 0.66; 'special': 0.73; 'goal': 0.74; 'yourself': 0.77; '/only/': 0.84; '2013': 0.84; 'answer:': 0.84; 'encounters': 0.84; 'rick': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type:content-transfer-encoding; bh=OEgnuPiml3O7wLM7PlDX4RZ8CD+egaQmwswNo7CoVZ8=; b=GDZZ12k/pOmIu/NH7dJt9C/xiM3Nuz3lBhOXliK/tUgm8JgmaIOwV/jBvvAR3+HKdZ hWRu/r3Vs15HSp+5BEubWGErzC+yT5a/4z9X41ZV41a+BF13P4+3A/Y/2rKS6pQw1fTp giYCqyXeBqWusJBavX+oEFqixUwOxoDk37NiUcqVHdCBSYup+fFHYU7CgeVhxvmFHPrO KWX7h+Ifi7OQa2wdUVzknXHeACn7cS5qjuqya3kCKtFzQ1pMWl2wuOlIkZhjFNQFYgUR d/QugdNJ8BDf2Def0WTciB9q43waQfOIBHpvVSYovxehWs326lFWBD13nYDQJ1V2ltgx msgQ== X-Received: by 10.66.87.67 with SMTP id v3mr22753362paz.63.1360372034199; Fri, 08 Feb 2013 17:07:14 -0800 (PST) MIME-Version: 1.0 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> From: Ian Kelly Date: Fri, 8 Feb 2013 18:06:34 -0700 Subject: Re: Implicit conversion to boolean in if and while statements To: Rick Johnson Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: Python 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360372044 news.xs4all.nl 6889 [2001:888:2000:d::a6]:57240 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38486 On Fri, Feb 8, 2013 at 12:58 PM, 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. > > Stop obfuscating your code! Of course someone could probably find a legit= imate reason to apply a sum method to non-numeric values; if so, then inher= it from NumericSequence and create your custom type! Are you aware that the approach you're advocating here is bad OOP design? If you declare a class NumericSequence with the property that it contains only numeric types, and then you declare a subclass NonnumericSequence that does not share that property, then guess what? You've just violated the Liskov Substitution Principle. The goal you're trying to achieve here is nonsensical anyway. Ask yourself what the semantic meaning of the sum() function is, what purpose it is meant to serve. My answer: it is the reduction of the addition operator. The implication of this is that the input type of the sum() function is not "numbers", but rather "things that can be added". That includes numbers, but since I see from your proposed class hierarchy that you are retaining the __add__ method on sequences, it also includes sequences. Are you really going to tell the user that (1, 2, 3) + (4, 5, 6) is perfectly fine, but that the semantic equivalent sum([(1, 2, 3), (4, 5, 6)]) is nonsense?