Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'interpreter': 0.07; 'python': 0.08; '128': 0.09; '21,': 0.09; 'pm,': 0.10; '>>>': 0.12; 'def': 0.12; 'wrote:': 0.14; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; '1024': 0.16; '[gcc': 0.16; 'expression:': 0.16; 'outputs': 0.16; 'cc:addr:python-list': 0.17; 'once,': 0.19; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'received:209.85.213.46': 0.23; 'received:mail- yw0-f46.google.com': 0.23; 'function': 0.25; 'statement': 0.26; 'example': 0.27; 'message-id:@mail.gmail.com': 0.28; 'sat,': 0.29; 'explicitly': 0.29; 'class': 0.29; 'instead': 0.29; 'cc:addr:python.org': 0.30; 'second': 0.30; '"why': 0.30; 'implicitly': 0.30; 'print': 0.31; 'expression': 0.32; 'comment': 0.33; 'initial': 0.33; '...': 0.34; 'question': 0.34; 'chris': 0.34; 'there': 0.35; 'received:132': 0.35; 'received:google.com': 0.37; 'received:209.85': 0.37; 'assuming': 0.37; 'received:209.85.213': 0.37; 'run': 0.38; 'subject:: ': 0.38; 'received:209': 0.39; 'final': 0.60; 'more': 0.60; 'your': 0.60; '2.7.1': 0.84; '256': 0.84; 'python2.7': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=ucsd.edu; i=crebert@ucsd.edu; q=dns/txt; s=041709-iport; t=1306045725; x=1337581725; h=mime-version:in-reply-to:references:date:message-id: subject:from:to:cc:content-transfer-encoding; z=MIME-Version:=201.0|In-Reply-To:=20|References:=20|Date: =20Sat,=2021=20May=202011=2023:27:31=20-0700|Message-ID: =20 |Subject:=20Re:=20count=20strangeness|From:=20Chris=20Reb ert=20|To:=20James=20Stroud=20|Cc:=20python-list@python.org |Content-Transfer-Encoding:=20quoted-printable; bh=aXFkWo1+6vmSey8bKW1ljZ69AwlNActGdB883Djq8mo=; b=QZRNxbbOjRFhXC2aivIZDp+IvN5nYtBGD7WUzbwJj9+Soj2RD5Q9Ylnt mlx1GQoDzoZAOmKFf41u2TIR/LN+9Fi1+3DkObODRxP0n9NE2MIEJ1q1p kExlTaDLLGV6xKze22hVIJJWJ3WEU8p1pYK4rzrNobbk/NrxP6plZ8INs M=; X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao0FAFOs2E2E7/kU/2dsb2JhbACEXKE8CHioPotfhkuIdIErggOBZIEHBIZQiUGEIYZkO4MAVQ X-IronPort-AV: E=Sophos;i="4.65,251,1304319600"; d="scan'208";a="516741531" X-Spam-Status: No X-Spam-Level: MIME-Version: 1.0 In-Reply-To: References: Date: Sat, 21 May 2011 23:27:31 -0700 Subject: Re: count strangeness From: Chris Rebert To: James Stroud Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 73 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306045726 news.xs4all.nl 49038 [::ffff:82.94.164.166]:46325 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5957 On Sat, May 21, 2011 at 11:02 PM, James Stroud wrote= : > tal 65% python2.7 > Python 2.7.1 (r271:86832, May 21 2011, 22:52:14) > [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > py> class C(object): > ... =C2=A0 def __init__(self): > ... =C2=A0 =C2=A0 self.data =3D [] > ... =C2=A0 def doit(self, count=3D0): > ... =C2=A0 =C2=A0 for c in self.data: > ... =C2=A0 =C2=A0 =C2=A0 count +=3D c.doit(count) > ... =C2=A0 =C2=A0 count +=3D 1 > ... =C2=A0 =C2=A0 print count > ... =C2=A0 =C2=A0 return count > ... > py> c =3D C() > py> c.data.extend([C() for i in xrange(10)]) > py> c.doit() > 1 > 2 > 4 > 8 > 16 > 32 > 64 > 128 > 256 > 512 > 1024 > 1024 > > WTF? Assuming your question is "Why is 1024 there twice?", the answer is that it was the return value of the initial c.doit() call, and the interactive interpreter outputs all non-None expression results; for example: [In the Python REPL] >>> 5 + 5 10 >>> So, you explicitly print the 1024 in your function once, and the interpreter implicitly outputs it the second time. Notice what happens when we use a statement instead of an expression: >>> final =3D c.doit() 1 2 4 8 16 32 64 128 256 512 1024 >>> final 1024 >>> And if we comment out the `print` in C.doit() and run your example again: >>> c =3D C() >>> c.data =3D [C() for i in xrange(10)] >>> c.doit() 1024 >>> Cheers, Chris -- http://rebertia.com