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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:53:56+00:00 2026-06-08T14:53:56+00:00

I think that below code fully describes the problem. Why in the Test2 function

  • 0

I think that below code fully describes the problem. Why in the Test2 function x has not been defined? Why the Test3 function does not return an error?

>>> def Test1():
exec('x=2')
print(str(x))


>>> Test1()
2
>>> def Test2():
global x
exec('x=2')
print(str(x))


>>> Test2()

Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
Test2()
File "<pyshell#38>", line 4, in Test2
print(str(x))
NameError: global name 'x' is not defined

>>> def Test3():
global x
x=2
print(str(x))

>>> Test3()
2
  • 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-08T14:53:59+00:00Added an answer on June 8, 2026 at 2:53 pm

    The answer given by @SanSS is correct, as is the comment by @Colin Dunklau, but I wanted to add a bit more info. One thing that may trip you up is that the global x in Test2 does not carry through to the exec-ed code. So the exec creates a local variable x, while the print x tries to read a global variable. These two examples may be helpful. . .

    Here by passing one dict, you tell exec to use that as globals and locals, so the exec assigns to globals:

    >>> def Test2():
    ...     global x
    ...     exec 'x=2' in globals()
    ...     print(str(x))
    >>> Test2()
    2
    

    Here by including the global declaration in the exec-ed code, you make the exec assign to the global, which the print statement then reads:

    >>> def Test2():
    ...     global x
    ...     exec('global x; x=2')
    ...     print(str(x))
    >>> Test2()
    2
    

    To reiterate, however, it’s usually not a good idea to use exec. It can be good to play around with stuff like this sometimes just to understand how Python works, but there are few cases where this is a good way to actually accomplish something with your code.

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

Sidebar

Related Questions

The code below shows me (I think) that the for each loop is about
I think that I'm probably not writing lazily instantiated methods/attributes in the most ruby
I think that this problem can be sorted using reflection (a technology which I'm
I think that looking at others' code is a good way to learn. I'm
I think that is not easy to understand what I need reading title, so
Could someone please explain function(tx) in the code snippet below, from this page: http://www.webkit.org/demos/sticky-notes/
I think that handlers in android are tools to get different objects that are
just think that when I opened my file then when I want to write
I think that Visual Studio's biggest let down is the Javascript editor. I have
I think that the zipper is a beautiful idea; it elegantly provides a way

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.