Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5957

Re: count strangeness

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 <crebert@ucsd.edu>
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<ira8ti$6no$1@dont-em ail.me>|References:=20<ira8ti$6no$1@dont-email.me>|Date: =20Sat,=2021=20May=202011=2023:27:31=20-0700|Message-ID: =20<BANLkTikhFwFjrghD5MzFXT7k+FX=3DG_33fQ@mail.gmail.com> |Subject:=20Re:=20count=20strangeness|From:=20Chris=20Reb ert=20<crebert@ucsd.edu>|To:=20James=20Stroud=20<jstroud@ mbi.ucla.edu>|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 <ira8ti$6no$1@dont-email.me>
References <ira8ti$6no$1@dont-email.me>
Date Sat, 21 May 2011 23:27:31 -0700
Subject Re: count strangeness
From Chris Rebert <crebert@ucsd.edu>
To James Stroud <jstroud@mbi.ucla.edu>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1903.1306045726.9059.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Sat, May 21, 2011 at 11:02 PM, James Stroud <jstroud@mbi.ucla.edu> 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):
> ...   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
> ...
> py> c = 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 = 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 = C()
>>> c.data = [C() for i in xrange(10)]
>>> c.doit()
1024
>>>

Cheers,
Chris
--
http://rebertia.com

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

count strangeness James Stroud <jstroud@mbi.ucla.edu> - 2011-05-21 23:02 -0700
  Re: count strangeness Chris Rebert <crebert@ucsd.edu> - 2011-05-21 23:27 -0700
    Re: count strangeness James Stroud <jstroud@mbi.ucla.edu> - 2011-05-21 23:32 -0700
      Re: count strangeness James Stroud <jstroud@mbi.ucla.edu> - 2011-05-21 23:39 -0700
      Re: count strangeness Chris Rebert <crebert@ucsd.edu> - 2011-05-21 23:55 -0700
        Re: count strangeness James Stroud <jstroud@mbi.ucla.edu> - 2011-05-22 00:03 -0700
  Re: count strangeness Peter Otten <__peter__@web.de> - 2011-05-22 08:31 +0200
    Re: count strangeness James Stroud <jstroud@mbi.ucla.edu> - 2011-05-21 23:34 -0700
      Re: count strangeness Peter Otten <__peter__@web.de> - 2011-05-22 09:13 +0200
    Re: count strangeness James Stroud <jstroud@mbi.ucla.edu> - 2011-05-21 23:56 -0700
  Re: count strangeness Roy Smith <roy@panix.com> - 2011-05-22 09:36 -0400

csiph-web