Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'operator': 0.03; 'compiler': 0.05; 'python': 0.09; '[1,': 0.09; 'sub': 0.09; 'subtract': 0.09; 'xor': 0.09; 'yeah,': 0.09; 'zero.': 0.09; 'language,': 0.11; 'assume': 0.11; 'language': 0.14; 'cases': 0.15; '*other*': 0.16; 'arrays,': 0.16; 'arrays.': 0.16; 'decimal.': 0.16; 'enlighten': 0.16; 'filter:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'inf': 0.16; 'integers,': 0.16; 'least,': 0.16; 'makes,': 0.16; 'mov': 0.16; 'nan': 0.16; 'opcode': 0.16; 'result:': 0.16; 'set,': 0.16; 'spotted': 0.16; 'subtraction': 0.16; 'whatever,': 0.16; 'wrote:': 0.17; 'bytes': 0.17; 'instance': 0.17; 'string,': 0.17; '>>>': 0.18; '(or': 0.18; 'do.': 0.21; 'regardless': 0.21; 'subject:skip:i 10': 0.22; 'example': 0.23; 'elements': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'skip:( 20': 0.28; 'subject:list': 0.28; 'chris': 0.28; 'cases.': 0.29; 'cpu': 0.29; "d'aprano": 0.29; 'sets.': 0.29; 'steven': 0.29; 'array': 0.29; 'case,': 0.29; 'probably': 0.29; "i'm": 0.29; 'fri,': 0.30; 'sense': 0.31; 'code': 0.31; '(2)': 0.32; 'running': 0.32; 'could': 0.32; 'to:addr:python-list': 0.33; 'another': 0.33; 'received:google.com': 0.34; '(1)': 0.34; 'data,': 0.35; 'desirable': 0.35; 'nov': 0.35; 'doing': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; "didn't": 0.36; 'should': 0.36; 'itself': 0.37; 'being': 0.37; 'rather': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'some': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'release': 0.39; 'takes': 0.39; 'where': 0.40; 'subject:-': 0.40; 'header:Received:5': 0.40; 'first': 0.61; 'more': 0.63; 'benefit': 0.70; 'hoping': 0.72; 'us,': 0.74; 'differently:': 0.84; 'float,': 0.84; 'gospel': 0.84; 'once).': 0.84; 'pike': 0.84; 'sets,': 0.84; 'comment.': 0.91; 'either:': 0.91 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:to :content-type; bh=y1RI+H8IDsTW/gh4FhtqsqHgEcmg5Pd1K+s27CYCR5Y=; b=VCxwYorDXJPpB9tQUyKdpQjIfHhXA6NHIYcevyUeC0VXJFBD/shlRhSpWIHoLk844U W/PfcQMRxoSjJSizpmjSqysl0ffoiUKleLCUX8i5Dpd3Xc11nRVpvkWmJe5u4CBbBRIk g3nLjDJW9qdZRmQaYQZaVrbsDBScj+isG0y23TZpRdetx4AS2bI4RIQZlP9CKbGpds2d i3qa6ekBXtN2CTcBQtPXcm9sDCJ7Y4TngbbfXYCUHRCZwqsm13YtyEcB79HAOp0ofbRk NST67B4gQU0PX3tNrA7Z7u2Tmn27wUGjThlEVdmsvvaj17s2G8txEvs7icOIsDi9BomN c/8w== MIME-Version: 1.0 In-Reply-To: <509ca4b5$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <509ca4b5$0$29980$c3e8da3$5496439d@news.astraweb.com> Date: Fri, 9 Nov 2012 17:59:36 +1100 Subject: Re: Multi-dimensional list initialization From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352444379 news.xs4all.nl 6865 [2001:888:2000:d::a6]:48248 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32999 On Fri, Nov 9, 2012 at 5:37 PM, Steven D'Aprano wrote: > On Fri, 09 Nov 2012 17:07:09 +1100, Chris Angelico wrote: >> Can you enlighten us as to how this is better than either: >> x := -x >> or >> x := 0 - x >> ? I'm not seeing it. > > I'm hoping that Mark intended it as an example of crappy code he has > spotted in some other language rather than a counter-example of something > you would do. Ohh. Yeah, that figures. Huh. > To be pedantic... there may very well be some (rare) cases where you > actually do want x -= x rather than just x = 0. Consider the case where x > could be an INF or NAN. Then x -= x should give x = NAN rather than zero. > That may be desirable in some cases. > > At the very least, the compiler should NOT optimize away x = x - x to > x = 0 if x could be a float, complex or Decimal. Yep. In the specific case of integers, though, and in the specific instance of CPU registers in assembly language, it's reasonable to optimize it the *other* way - MOV reg,0 is a one-byte opcode and 1, 2, or 4 bytes of immediate data, while SUB reg,reg (or XOR reg,reg) is a two-byte operation regardless of data size. But that's microoptimization that makes, uhh, itself-subtracted-from-itself sense in Python. >> And I'm not seeing any nonnumeric that would >> benefit from being subtracted from itself twice (strings, arrays, sets, >> you can subtract them from one another but not usefully more than once). > > How do you subtract strings? The same way you subtract sets. Same with arrays. Python doesn't do either, but Python also doesn't do the ":=" operator that the example code demonstrated, so I didn't assume Python. Pike v7.8 release 700 running Hilfe v3.5 (Incremental Pike Frontend) > "Hello, world!"-"l"; (1) Result: "Heo, word!" > ({1,2,3,3,2,3,1,2,1})-({2}); (2) Result: ({ /* 6 elements */ 1, 3, 3, 3, 1, 1 }) Python spells it differently: >>> "Hello, world!".replace("l","") 'Heo, word!' Not sure how to do array subtraction other than with filter: >>> list(filter(lambda x: x!=2,[1,2,3,3,2,3,1,2,1])) [1, 3, 3, 3, 1, 1] But there's probably a way (list.remove only takes out the first occurrence, so it's not equivalent). In any case, subtracting something from _itself_ is only going to give you an empty string, array, set, or whatever, and doing so a second time is going to achieve nothing. Hence my comment. But poor code we will always have with us, to paraphrase the Gospel of Matthew. ChrisA