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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:18:56+00:00 2026-05-10T22:18:56+00:00

What exactly are the Python scoping rules? If I have some code: code1 class

  • 0

What exactly are the Python scoping rules?

If I have some code:

code1 class Foo:    code2    def spam.....       code3       for code4..:        code5        x() 

Where is x found? Some possible choices include the list below:

  1. In the enclosing source file
  2. In the class namespace
  3. In the function definition
  4. In the for loop index variable
  5. Inside the for loop

Also there is the context during execution, when the function spam is passed somewhere else. And maybe lambda functions pass a bit differently?

There must be a simple reference or algorithm somewhere. It’s a confusing world for intermediate Python programmers.

  • 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. 2026-05-10T22:18:57+00:00Added an answer on May 10, 2026 at 10:18 pm

    Actually, a concise rule for Python Scope resolution, from Learning Python, 3rd. Ed.. (These rules are specific to variable names, not attributes. If you reference it without a period, these rules apply.)

    LEGB Rule

    • Local — Names assigned in any way within a function (def or lambda), and not declared global in that function

    • Enclosing-function — Names assigned in the local scope of any and all statically enclosing functions (def or lambda), from inner to outer

    • Global (module) — Names assigned at the top-level of a module file, or by executing a global statement in a def within the file

    • Built-in (Python) — Names preassigned in the built-in names module: open, range, SyntaxError, etc

    So, in the case of

    code1 class Foo:     code2     def spam():         code3         for code4:             code5             x() 

    The for loop does not have its own namespace. In LEGB order, the scopes would be

    • L: Local in def spam (in code3, code4, and code5)
    • E: Any enclosing functions (if the whole example were in another def)
    • G: Were there any x declared globally in the module (in code1)?
    • B: Any builtin x in Python.

    x will never be found in code2 (even in cases where you might expect it would, see Antti’s answer or here).

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would suggest using ADAM (Acitve Directory Application Mode). This… May 11, 2026 at 11:42 pm
  • Editorial Team
    Editorial Team added an answer Using SetCursorPos with a timer to move the cursor between… May 11, 2026 at 11:42 pm
  • Editorial Team
    Editorial Team added an answer Unless there's a Project Template file included with the cocos2d… May 11, 2026 at 11:42 pm

Related Questions

One of my favorite features about python is that you can write configuration files
In Python, the following statements do not work: f = open("ftmp", "rw") print >>
Is the LAMP (Linux, Apache, MySQL, PHP / Ruby / Python) stack appropriate for
I suppose I could take a text and remove high frequency English words from

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.