Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #5956 > unrolled thread
| Started by | James Stroud <jstroud@mbi.ucla.edu> |
|---|---|
| First post | 2011-05-21 23:02 -0700 |
| Last post | 2011-05-22 09:36 -0400 |
| Articles | 11 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | James Stroud <jstroud@mbi.ucla.edu> |
|---|---|
| Date | 2011-05-21 23:02 -0700 |
| Subject | count strangeness |
| Message-ID | <ira8ti$6no$1@dont-email.me> |
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? James
[toc] | [next] | [standalone]
| From | Chris Rebert <crebert@ucsd.edu> |
|---|---|
| Date | 2011-05-21 23:27 -0700 |
| Message-ID | <mailman.1903.1306045726.9059.python-list@python.org> |
| In reply to | #5956 |
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
[toc] | [prev] | [next] | [standalone]
| From | James Stroud <jstroud@mbi.ucla.edu> |
|---|---|
| Date | 2011-05-21 23:32 -0700 |
| Message-ID | <iraam0$dfi$1@dont-email.me> |
| In reply to | #5957 |
Chris Rebert wrote: >> WTF? > > Assuming your question is "Why is 1024 there twice?", the answer is The question is "Why is 1024 there at all?" It should be 10. James
[toc] | [prev] | [next] | [standalone]
| From | James Stroud <jstroud@mbi.ucla.edu> |
|---|---|
| Date | 2011-05-21 23:39 -0700 |
| Message-ID | <irab3d$dfi$3@dont-email.me> |
| In reply to | #5959 |
James Stroud wrote: > Chris Rebert wrote: >>> WTF? >> >> Assuming your question is "Why is 1024 there twice?", the answer is > > The question is "Why is 1024 there at all?" It should be 10. > > James I mean 11, not 10--but you get the point. James
[toc] | [prev] | [next] | [standalone]
| From | Chris Rebert <crebert@ucsd.edu> |
|---|---|
| Date | 2011-05-21 23:55 -0700 |
| Message-ID | <mailman.1907.1306047359.9059.python-list@python.org> |
| In reply to | #5959 |
On Sat, May 21, 2011 at 11:32 PM, James Stroud <jstroud@mbi.ucla.edu> wrote:
> Chris Rebert wrote:
>>>
>>> WTF?
>>
>> Assuming your question is "Why is 1024 there twice?", the answer is
>
> The question is "Why is 1024 there at all?" It should be 10.
Ah. This is why it's better to be more explicit about what your
question is than a mere "WTF?".
In that case, I believe you meant to write:
count += c.doit()
rather than:
count += c.doit(count)
Recall that count is continually increasing as you go through your loop.
Although you'll then get 11, not 10, but the cause of that is obvious
(`count += 1`).
Cheers,
Chris
--
http://rebertia.com
[toc] | [prev] | [next] | [standalone]
| From | James Stroud <jstroud@mbi.ucla.edu> |
|---|---|
| Date | 2011-05-22 00:03 -0700 |
| Message-ID | <iracf9$lq3$1@dont-email.me> |
| In reply to | #5963 |
Chris Rebert wrote: > On Sat, May 21, 2011 at 11:32 PM, James Stroud <jstroud@mbi.ucla.edu> wrote: >> Chris Rebert wrote: >>>> WTF? >>> Assuming your question is "Why is 1024 there twice?", the answer is >> The question is "Why is 1024 there at all?" It should be 10. > > Ah. This is why it's better to be more explicit about what your > question is than a mere "WTF?". > > In that case, I believe you meant to write: > count += c.doit() > rather than: > count += c.doit(count) I get it. Thank you. James
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2011-05-22 08:31 +0200 |
| Message-ID | <mailman.1905.1306045890.9059.python-list@python.org> |
| In reply to | #5956 |
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): > ... 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? Put the code into a file, run it -- and be enlightened ;)
[toc] | [prev] | [next] | [standalone]
| From | James Stroud <jstroud@mbi.ucla.edu> |
|---|---|
| Date | 2011-05-21 23:34 -0700 |
| Message-ID | <iraaqg$dfi$2@dont-email.me> |
| In reply to | #5958 |
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
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2011-05-22 09:13 +0200 |
| Message-ID | <mailman.1908.1306048424.9059.python-list@python.org> |
| In reply to | #5960 |
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
[toc] | [prev] | [next] | [standalone]
| From | James Stroud <jstroud@mbi.ucla.edu> |
|---|---|
| Date | 2011-05-21 23:56 -0700 |
| Message-ID | <irac35$k6a$1@dont-email.me> |
| In reply to | #5958 |
Peter Otten wrote:
> James Stroud wrote:
>> WTF?
>
> Put the code into a file, run it -- and be enlightened ;)
Compare the follower to the last.
tal 77% cat eraseme.py
#! /usr/bin/env python
class C:
def __init__(self):
self.data = []
def doit(self, count=[0]):
for c in self.data:
c.doit()
count[0] += 1
print count[0]
c = C()
c.data.extend([C() for i in xrange(10)])
c.doit()
tal 78% python2.7 eraseme.py
1
2
3
4
5
6
7
8
9
10
11
James
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2011-05-22 09:36 -0400 |
| Message-ID | <roy-3A556B.09365022052011@news.panix.com> |
| In reply to | #5956 |
In article <ira8ti$6no$1@dont-email.me>, 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() I have no idea what this is *supposed* to be doing, so let me toss out some general debugging ideas; 1) What do you expect [C() for i in xrange(10)] to return? Does it? Run that part in isolation and see if it does. 2) What do you expect c.data.extend([...]) to return? Does it? 3) What do you expect c.doit() to return for trivial inputs? Does it? Try calling it with a count of 0, 1, and 2, and see if you get the result you expect. In other words, break the problem down into smaller pieces and make sure each piece works in isolation. When you find some little piece which isn't doing what you expect, it'll be much easier to debug than trying to debug the whole thing at once.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web