count strangeness

James Stroud jstroud at mbi.ucla.edu
Sun May 22 02:34:56 EDT 2011


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





More information about the Python-list mailing list