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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:15:20+00:00 2026-06-17T12:15:20+00:00

Beazley pg 100 mentions: >>>python.__closure__ (<cell at 0x67f50: str object at 0x69230>,) >>>python.__closure__[0].cell_contents my

  • 0

Beazley pg 100 mentions:

>>>python.__closure__
(<cell at 0x67f50: str object at 0x69230>,)
>>>python.__closure__[0].cell_contents

my understanding is that __closure__ is a list but what’s all this
cell stuff and str object?? That looks like a 1-ary tuple?

  • 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-17T12:15:21+00:00Added an answer on June 17, 2026 at 12:15 pm

    Closure cells refer to values needed by the function but are taken from the surrounding scope.

    When Python compiles a nested function, it notes any variables that it references but are only defined in a parent function (not globals) in the code objects for both the nested function and the parent scope. These are the co_freevars and co_cellvars attributes on the __code__ objects of these functions, respectively.

    Then, when you actually create the nested function (which happens when the parent function is executed), those references are then used to attach a closure to the nested function.

    A function closure holds a tuple of cells, one each for each free variable (named in co_freevars); cells are special references to local variables of a parent scope, that follow the values those local variables point to. This is best illustrated with an example:

    def foo():
        def bar():
            print(spam)
    
        spam = 'ham'
        bar()
        spam = 'eggs'
        bar()
        return bar
    
    b = foo()
    b()
    

    In the above example, the function bar has one closure cell, which points to spam in the function foo. The cell follows the value of spam. More importantly, once foo() completes and bar is returned, the cell continues to reference the value (the string eggs) even though the variable spam inside foo no longer exists.

    Thus, the above code outputs:

    >>> b=foo()
    ham
    eggs
    >>> b()
    eggs
    

    and b.__closure__[0].cell_contents is 'eggs'.

    Note that the closure is dereferenced when bar() is called; the closure doesn’t capture the value here. That makes a difference when you produce nested functions (with lambda expressions or def statements) that reference the loop variable:

    def foo():
        bar = []
        for spam in ('ham', 'eggs', 'salad'):
            bar.append(lambda: spam)
        return bar
    
    for bar in foo():
        print bar()
    

    The above will print salad three times in a row, because all three lambda functions reference the spam variable, not the value it was bound to when the function object was created. By the time the for loop finishes, spam was bound to 'salad', so all three closures will resolve to that value.

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

Sidebar

Related Questions

This example is taken from Beazley, Python Essential Reference 4e, pg:101. How is he
Ever since I read Dave Beazley's post on binary I/O handling (http://dabeaz.blogspot.com/2009/08/python-binary-io-handling.html) I've wanted
I've read all the documentation on the subject, but it seems I can't grasp
While reading up the documentation for dict.copy() , it says that it makes a
Defining a function, MyFunction(argument, *args): [do something to argument[arg] for arg in *args] if
I have been coding against a Delphi EXE (win32 desktop app) to access twitter

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.