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

  • Home
  • SEARCH
  • 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 6113353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:50:57+00:00 2026-05-23T14:50:57+00:00

I looked through the myriad ‘Python exec’ threads on SO, but couldn’t find one

  • 0

I looked through the myriad ‘Python exec’ threads on SO, but couldn’t find one that answered my issue. Terribly sorry if this has been asked before. Here’s my problem:

# Python 2.6: prints 'it is working'
# Python 3.1.2: "NameError: global name 'a_func' is not defined"
class Testing(object):
  def __init__(self):
    exec("""def a_func():
      print('it is working')""")
    a_func()

Testing()

# Python 2.6: prints 'it is working'
# Python 3.1.2: prints 'it is working'
class Testing(object):
  def __init__(self):
    def a_func():
      print('it is working')
    a_func()

Testing()

As the standard function definition works in both Python versions, I’m assuming the problem must be a change to the way exec works. I read the API docs for 2.6 and 3 for exec and also read the “What’s New In Python 3.0” page and couldn’t see any reason why the code would break.

  • 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-23T14:50:57+00:00Added an answer on May 23, 2026 at 2:50 pm

    You can see the generated bytecode for each Python version with:

    >>> from dis import dis
    

    And, for each interpreter:

    #Python 3.2
    >>> dis(Testing.__init__)
    ...
      5          10 LOAD_GLOBAL              1 (a_func)
    ...
    
    #Python 2.7
    >>> dis(Testing.__init__)
    ...
      5           8 LOAD_NAME                0 (a_func)
    ...
    

    As you can see, Python 3.2 searches for a global value (LOAD_GLOBAL) named a_func and 2.7 first searches the local scope (LOAD_NAME) before searching the global one.

    If you do print(locals()) after the exec, you’ll see that a_func is created inside the __init__ function.

    I don’t really know why it’s done that way, but seems to be a change on how symbol tables are processed.

    BTW, if want to create a a_func = None on top of your __init__ method to make the interpreter know it’s a local variable, it’ll not work since the bytecode now will be LOAD_FAST and that don’t make a search but directly gets the value from a list.

    The only solution I see is to add globals() as second argument to exec, so that will create a_func as a global function an may be accessed by the LOAD_GLOBAL opcode.

    Edit

    If you remove the exec statement, Python2.7 change the bytecode from LOAD_NAME to LOAD_GLOBAL. So, using exec, your code will always be slower on Python2.x because it has to search the local scope for changes.

    As Python3’s exec is not a keyword, the interpreter can’t be sure if it’s really executing new code or doing something else… So the bytecode don’t change.

    E.g.

    >>> exec = len
    >>> exec([1,2,3])
    3
    

    tl;dr

    exec('...', globals()) may solve the problem if you don’t care the result being added to global namespace

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

Sidebar

Related Questions

I looked through the questions here but didn't find one that suited my case.
Looked through many answers, but couldn't find one solving this. I have four (potentially
I looked through old threads but could not find the answer to my question:
I looked through the related questions and couldn't find anything that addresses exactly what
I've looked through some of the other posts but couldn't find an answer, so
I have looked through dozens and dozens of sliders but can't find the one
I've looked through the similar questions but can't find anything that's relevant to my
I looked through the documentation but couldn't find if there is a way to
I looked through the existing questions of this kind but didn't find what I
I've looked through many of the existing threads about this error, but still no

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.