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 8430849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:41:52+00:00 2026-06-10T05:41:52+00:00

I am using yield to create a generator that returns chunks of a string

  • 0

I am using yield to create a generator that returns chunks of a string that are being extracted using a regex and re.sub(). While I found an approach that worked, I am a bit confused about why it works one way but not another, as shown below:

This doesn’t work (processchunk() is not assigning to the chunk declared in splitmsg):

def splitmsg(msg):
    chunk = None
    def processchunk(match):
        chunk = match.group(1)
        return ""
    while True:
        chunk = None
        msg = re.sub(reCHUNK,processchunk,msg,1)
        if chunk:
            yield chunk
        else:
            break     

This does work (note the only difference being chunk is now a list chunks):

def splitmsg(msg):
    chunks = [ None, ]
    def processchunk(match):
        chunks[0] = match.group(1)
        return ""
    while True:
        chunks[0] = None
        msg = re.sub(reCHUNK,processchunk,msg,1)
        if chunks[0]:
            yield chunks[0]
        else:
            break

My question is basically why does it appear that the scoping of the chunk/chunks variable seem to depend on whether it is a plain variable or a list?

  • 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. Editorial Team
    Editorial Team
    2026-06-10T05:41:53+00:00Added an answer on June 10, 2026 at 5:41 am

    In python, variables can be ‘pulled’ from the surrounding scope if read from. So the following will work:

    def foo():
        spam = 'eggs'
        def bar():
            print spam
    foo()
    

    because the variable ‘spam’ is being looked up in the surrounding scope, the foo function.

    However, you cannot change the value of a surrounding scope. You can change global variables (if you declare them as global in your function), but you cannot do that for the variable spam in the above function.

    (Python 3 changes this, it adds a new keyword nonlocal. If you define spam as nonlocal inside of bar you can assign to that variable a new value inside of bar.)

    Now to your list. What happens there is that you are not altering the variable chunks at all. Throughout your code, chunks points to one list, and only to that list. As far as python is concerned, chunks the variable is not altered within the processchunk function.

    What does happen is that you alter the contents of the list. You can freely assign a new value to chunks[0], because that’s not the variable chunks, it is the list referred to by chunks, first index. Python allows this because it is not a variable assignment, but a list manipulation instead.

    So, your ‘workaround’ is correct, if somewhat obscure. If you use Python 3, you can declare chunks as nonlocal within processchunk and then things will work without lists too.

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

Sidebar

Related Questions

Preface: I'm know that in most cases using a volatile field won't yield any
All of the examples I've seen of using yield return x; inside a C#
When you write an iterator function in C#, using yield syntax, the compiler internally
Using Google + Bing didn't yield an answer to what should be a simple
i'm using ECLiPSe programming logic system. i want to implement the yield method for
I am using a module that is part of a commercial software API. The
Besides just using yield for iterators in Ruby, I also use it to pass
I'm trying to create a topbar using twitter bootstrap, but the black bar won't
I'm running into an issue while unit-testing a Python project that I'm working on
Using module_eval, my code allows me to dynamically create and add new methods to

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.