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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:08:24+00:00 2026-05-26T23:08:24+00:00

Possible Duplicate: Python nested functions variable scoping After much trial and error I have

  • 0

Possible Duplicate:
Python nested functions variable scoping

After much trial and error I have eventually discovered that this doesn’t work:

def a():
    def b():
        print x
        x=2
    x = 1
    b()
    print x

You get an exception (x not defined before being referenced). So it looks like b can read from x, but if it tries to assign to it, Python changes its interpretation of ‘x’ to be a local variable, which is now not defined.

Question for my own sick curiosity: is there any way of achieving this? Is there a way of explicitly accessing the scope of the parent function? (x is not global)

  • 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-05-26T23:08:25+00:00Added an answer on May 26, 2026 at 11:08 pm

    The nonlocal statement in Python 3 will do this.


    Edit: In Python 2, there’s not a simple way to do it. I suggest you use some mutable container object if you need this capability. For example:

    def a():
        def b():
            print d["x"]
            d["x"]=2
        d = dict(x=1)
        b()
        print d["x"]
    

    If you absolutely must emulate nonlocal for CPython 2, you can hack it with the Python C API this way:

    import ctypes
    import inspect
    
    locals_to_fast = ctypes.pythonapi.PyFrame_LocalsToFast
    locals_to_fast.restype = None
    locals_to_fast.argtypes = [ctypes.py_object, ctypes.c_int]
    
    def set_in_frame(frame, name, value):
        frame.f_locals[name] = value
        locals_to_fast(frame, 1)
    
    def a():
        def b(frame=inspect.currentframe()):
            print x
            set_in_frame(frame, "x", 2)
        x = 1
        b()
        print x
    

    You could also set the frame local, and instead of calling PyFrame_LocalsToFast(), you could manipulate the bytecode of a so that it uses LOAD_NAME instead of LOAD_FAST. Please don’t do either of these things. There is surely a better solution for your use case.

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

Sidebar

Related Questions

Possible Duplicate: True random number generator I have worked with random functions in python,ruby,
Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
Possible Duplicate: Getting python.exe path at run time I have a python app that
Possible Duplicate: Python rounding error with float numbers I have a rounding Problem in
Possible Duplicate: python: how to encrypt a file? I'm trying to make application that
Possible Duplicate: Run python script without DOS shell appearing I have a python script
Possible Duplicate: What can you use Python generator functions for? I tried to read
Possible Duplicate: how to parse hex or decimal int in Python i have a
Possible Duplicate: Flatten (an irregular) list of lists in Python I have the following
Possible Duplicate: Python “extend” for a dictionary I know that Python list can be

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.