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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:33:44+00:00 2026-06-13T00:33:44+00:00

The local/global/free variable definitions from python doc : If a name is bound in

  • 0

The local/global/free variable definitions from python doc:

If a name is bound in a block, it is a local variable of that block, unless declared as nonlocal. If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a free variable.


Code 1:

>>> x = 0
>>> def foo():
...   print(x)
...   print(locals())
... 
>>> foo()
0
{}

Code 2:

>>> def bar():
...   x = 1
...   def foo():
...     print(x)
...     print(locals())
...   foo()
... 
>>> bar()
1
{'x':1}

Free variables are returned by locals() when it is called in function blocks, but not in class blocks.


In Code 1, x is a global variable, and it’s used but not defined in foo().
However it’s not a free variable, because it’s not returned by locals().
I think it’s not what the doc said. Is there a technical definition for free variable?

  • 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-13T00:33:45+00:00Added an answer on June 13, 2026 at 12:33 am

    Definition of a free variable: Used, but neither global nor bound.

    For example:

    1. x is not free in Code 1, because it’s a global variable.
    2. x is not free in bar() in Code 2, because it’s a bound variable.
    3. x is free in foo().

    Python makes this distinction because of closures. A free variable is not defined in the current environment, i. e. collection of local variables, and is also not a global variable! Therefore it must be defined elsewhere. And this is the concept of closures. In Code 2, foo() closes on x defined in bar(). Python uses lexical scope. This means, the interpreter is able to determine the scope by just looking at the code.

    For example: x is known as a variable in foo(), because foo() is enclosed by bar(), and x is bound in bar().

    Global scope is treated specially by Python. It would be possible to view the global scope as an outermost scope, but this is not done because of performance (I think). Therefore it is not possible that x is both free and global.

    Exemption

    Life is not so simple. There exist free global variables. Python docs (Execution model) says:

    The global statement has the same scope as a name binding operation in the same block. If the nearest enclosing scope for a free variable contains a global statement, the free variable is treated as a global.

    >>> x = 42
    >>> def foo():
    ...   global x
    ...   def baz():
    ...     print(x)
    ...     print(locals())
    ...   baz()
    ... 
    >>> foo()
    42
    {}
    

    I didn’t know that myself. We are all here to learn.

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

Sidebar

Related Questions

I wanted to change a tag from being local to global by deleting and
In python, there is no way to differentiate between arguments, local variables, and global
Why is that in Delphi Boolean variable initialized in global scope is false and
I have a question about local and global variables and global objects in Python.
I have a php file that has some local and global variables (e.g. $foo)
It's known that big local/global variables may cause to a stack overflow. I know
I have a page in SharePoint that contains both a local and global content.
I am stuck at a Local to Global Point issue in Objective C ViewController
Can Dynamic libraries have global, local and static variables in linux? If yes, then
If I call EnqueueNDRange with maximum local group size and some large global group

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.