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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:32:06+00:00 2026-06-12T16:32:06+00:00

Code 1 x = 0 class Foo: print(x) x = 1 print(x) print(x) Result

  • 0

Code 1

x = 0

class Foo:
    print(x)
    x = 1
    print(x)

print(x)

Result 1

0
1
0

Code 2

x = 0

def foo():
    print(x)
    x = 1
    print(x)

foo()

Result 2

UnboundLocalError: local variable 'x' referenced before assignment.

Why can x reference objects from two namespaces in class block?
I don’t understand why Code 1 not throw an UnboundLocalError.
Inconsistency between function and class bother me.


Update:

After reading the Python Docs several times, I still cannot understand the scoping rules.

The following are blocks: a module, a function body, and a class definition. …[skip]…

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.

If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations.

  • 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-12T16:32:07+00:00Added an answer on June 12, 2026 at 4:32 pm
    x = 0
    
    class Foo:
        print(x)   # Foo.x isn't defined yet, so this is the global x
        x = 1      # This is referring to Foo.x
        print(x)   # So is this
    
    print(x)
    

    x = 0
    
    def foo():
        print(x)   # Even though x is not defined yet, it's known to be local
                   # because of the assignment
        x = 1      # This assignment means x is local for the whole function
        print(x)
    
    foo()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following (broken) code: import functools class Foo(object): def __init__(self): def f(a,self,b): print
Here is the code, it's pretty straightforward. class Foo { public: void print() {
Why can't I set $_SERVER['DOCUMENT_ROOT'] as attribute? see example code class foo { private
the next is my code: class foo: def __init__(self): self.a = a def __getattr__(self,x,defalut):
I have written some code that uses attributes of an object: class Foo: def
The following code works: class Foo(tuple): def __init__(self, b): super(Foo, self).__init__(tuple(b)) if __name__ ==
I have the following python code: class FooMeta(type): def __setattr__(self, name, value): print name,
I'm having a problem correctly hashing my objects. Consider the following code: class Foo:
I have the following code: class Foo<T> where T : struct { private T
Imagine this code: class foo { foo fff; int m_id; f & work() {

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.