Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 69261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:32:44+00:00 2026-05-10T19:32:44+00:00

I believe I’m getting bitten by some combination of nested scoping rules and list

  • 0

I believe I’m getting bitten by some combination of nested scoping rules and list comprehensions. Jeremy Hylton’s blog post is suggestive about the causes, but I don’t really understand CPython’s implementation well-enough to figure out how to get around this.

Here is an (overcomplicated?) example. If people have a simpler one that demos it, I’d like to hear it. The issue: the list comprehensions using next() are filled with the result from the last iteration.

edit: The Problem:

What exactly is going on with this, and how do I fix this? Do I have to use a standard for loop? Clearly the function is running the correct number of times, but the list comprehensions end up with the final value instead of the result of each loop.

Some hypotheses:

  • generators?
  • lazy filling of list comprehensions?

code

import itertools def digit(n):     digit_list = [ (x,False) for x in xrange(1,n+1)]     digit_list[0] = (1,True)     return itertools.cycle ( digit_list) 
 >>> D = digit(5) >>> [D.next() for x in range(5)] ## This list comprehension works as expected [(1, True), (2, False), (3, False), (4, False), (5, False)] 
class counter(object):     def __init__(self):         self.counter = [ digit(4) for ii in range(2) ]          self.totalcount=0         self.display = [0,] * 2     def next(self):         self.totalcount += 1         self.display[-1] = self.counter[-1].next()[0]         print self.totalcount, self.display         return self.display      def next2(self,*args):         self._cycle(1)         self.totalcount += 1         print self.totalcount, self.display         return self.display      def _cycle(self,digit):         d,first = self.counter[digit].next()         #print digit, d, first         #print self._display         self.display[digit] = d         if first and digit > 0:             self._cycle(digit-1)   C = counter() [C.next() for x in range(5)] [C.next2() for x in range(5)] 

OUTPUT

 In [44]: [C.next() for x in range(6)] 1 [0, 1] 2 [0, 2] 3 [0, 3] 4 [0, 4] 5 [0, 1] 6 [0, 2] Out[44]: [[0, 2], [0, 2], [0, 2], [0, 2], [0, 2], [0, 2]]  In [45]: [C.next2() for x in range(6)] 7 [0, 3] 8 [0, 4] 9 [1, 1] 10 [1, 2] 11 [1, 3] 12 [1, 4] Out[45]: [[1, 4], [1, 4], [1, 4], [1, 4], [1, 4], [1, 4]]  # this should be:  [[0,3],[0,4]....[1,4]] or similar 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:32:45+00:00Added an answer on May 10, 2026 at 7:32 pm

    The problem is that with return self.display you return a reference to this list (not a copy). So what you end up with is a list where each element is a reference to self.display. To illustrate, look at the following:

    >>> a = [1,2] >>> b = [a,a] >>> b [[1, 2], [1, 2]] >>> a.append(3) >>> b [[1, 2, 3], [1, 2, 3]] 

    You probably want to use something like return self.display[:].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I believe that I am having a bad database design and need some help
I believe the problem I am facing is a variant of the nested-monitor lockout.
I believe this post is the resolution to my trouble Flickering when navigating between
I believe I've set this up correctly. Can somebody see anything wrong with this
I believe I am using the correct queryString params for jdbc connection jdbc:mysql://localhost:3306/databasename?useUnicode=true&characterEncoding=utf8&noAccessToProcedureBodies=true My
I believe what I am trying to accomplish should be a fairly common task,
I believe that the rallytagpicker is definitely a useful component. However, I do not
I believe I am having a security problem related to using the embed tag
I believe I've looked everywhere possible and already feel to silly because I spent
I believe Wordpress stores multiple entries of posts as revisions but I think thats

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.