Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python': 0.08; '128': 0.09; 'assumed': 0.09; 'description.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'rewritten': 0.09; '>>>': 0.12; 'def': 0.12; 'wrote:': 0.14; '/usr/bin/env': 0.16; '1024': 0.16; 'missing?': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'twice.': 0.16; 'file,': 0.22; 'loop': 0.22; 'wondered': 0.23; 'code': 0.24; 'problem': 0.28; 'class': 0.29; 'from:addr:web.de': 0.30; 'print': 0.31; 'adds': 0.32; 'header:X-Complaints-To:1': 0.32; 'to:addr:python-list': 0.33; 'chris': 0.34; 'idea': 0.36; 'case': 0.37; 'another': 0.37; 'put': 0.37; 'received:org': 0.38; 'run': 0.38; 'subject:: ': 0.38; 'header:Mime-Version:1': 0.39; 'got': 0.39; 'add': 0.39; 'to:addr:python.org': 0.39; 'your': 0.60; '(3)': 0.63; '(4)': 0.65; 'special': 0.66; '(5)': 0.77; '256': 0.84; '72%': 0.84; 'python2.7': 0.84; 'appeared': 0.91; 'enlightened': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: count strangeness Date: Sun, 22 May 2011 09:13:44 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849a1d.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 68 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306048424 news.xs4all.nl 49045 [::ffff:82.94.164.166]:53633 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5966 James Stroud wrote: > Peter Otten wrote: >> James Stroud wrote: >>> WTF? >> >> Put the code into a file, run it -- and be enlightened ;) > > > tal 72% python2.7 eraseme.py > 1 > 2 > 4 > 8tal 73% cat eraseme.py > #! /usr/bin/env python > > class C: > def __init__(self): > self.data = [] > def doit(self, count=0): > for c in self.data: > count += c.doit(count) > count += 1 > print count > return count > > c = C() > c.data.extend([C() for i in xrange(10)]) > c.doit() > tal 74% python2.7 eraseme.py > 1 > 2 > 4 > 8 > 16 > 32 > 64 > 128 > 256 > 512 > 1024 > > > Hmmm. It's still 1024. > > What am I missing? > > James Like Chris I assumed that you wondered why 1024 appeared twice. It turns out WTF is not a got a good problem description. Now for a complete run-through, the inner c.doit() has len(c.data) == 0, so count += c.doit(count) just adds count + 1 and you get 10 iterations: (1) count=0, print 1, add 1 (2) count=1, print 2, add 2 (3) count=3, print 4, add 4 (4) count=7, print 8, add 8 (5) you get the idea Another way to look at it: in your special case the inner loop can be rewritten as for c in self.data: print count + 1 count += count + 1