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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:34:32+00:00 2026-06-10T21:34:32+00:00

While exploring some solutions to my previous question about the inner workings of Python

  • 0

While exploring some solutions to my previous question about the inner workings of Python scope, I learned about the __closure__ attribute. It seems that Python uses this attribute to access variables defined in an outer scope from within a nested function.

We can see this in action by doing the following:

def foo():
    x = 5
    def bar(): 
        print(x)
        print(*(cell.cell_contents for cell in bar.__closure__))
    bar()
foo()

This shows two enclosed values, 5 and the function bar itself.

What I don’t understand is how this works – since, the __closure__ attribute merely contains a tuple of cells which store the enclosed values. But there’s no information about the enclosed variables names – (i.e. the cells are stored in a tuple, not a dict). So how does Python know the names of the variables which have been enclosed?

  • 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-10T21:34:34+00:00Added an answer on June 10, 2026 at 9:34 pm

    The python compiled code uses indices; the variables are tied to an index into the cells structure.

    >>> def foo():
    ...     x = 5
    ...     def bar():
    ...         return x
    ...     return bar
    ... 
    >>> bar = foo()
    >>> import dis
    >>> dis.dis(bar)
      4           0 LOAD_DEREF               0 (x) 
                  3 RETURN_VALUE         
    

    The LOAD_DEREF bytecode refences the first cell value.

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

Sidebar

Related Questions

I did some homework and while I can answer all below questions about exporting
I here by post the code why I came across while exploring one technique.
while looking at some code I stumbled onto: throw /*-->*/new std::exception (//... and I
I've inherited an enormous .NET solution of about 200 projects. There are now some
While recently exploring prezi I realized there have to be better tools than PowerPoint
I'm trying to solve a two-dimensional random walk problem from the book, exploring python.
I recently learned about the DI frameworks Guice and Ninject and wanted to use
Recently I've begun working on exploring ways to convert about 16k Corel Paradox 4.0
I am very new to Oracle exploring things,reading about how index access ,i got
I have been exploring some way in VIM to automatically append closing characters 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.