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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:46:57+00:00 2026-06-10T10:46:57+00:00

I have a question about local and global variables and global objects in Python.

  • 0

I have a question about local and global variables and global objects in Python. Look at this code:

var = 0

class Test():
    var = 0

test = Test()

def sum():
    Test.var += 1
    test.var += 1
    var += 1

sum()

If I run that code, the exception is triggered in the line “var += 1” only. The two previous lines work. I read this question from the Python FAQ. I think that there is no exception in the first two lines of the function because the “Test” and “test” are referenced. The member “var” is assigned, but “Test” and “test” are global because are referenced to get the member. The FAQ said: “In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be a local.”

So, the question is… is my assumption true?

  • 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-10T10:46:58+00:00Added an answer on June 10, 2026 at 10:46 am

    I think that there is no exception in the first two lines of the
    function because the “Test” and “test” are referenced.

    Correct. And they refer to the class attribute var, not the global one you defined.

    The member “var” is assigned, but “Test” and “test” are global because are referenced to get the member.

    Or to put it another way, Test and test are available in the global namespace so Test.var and test.var work.

    If the value of var was not changed in sum(), you would get 0 since the lines above it have changed the Test class attribute not the global. Adding some prints in sum and removing the var += 1

    def sum():
        Test.var += 1
        print Test.var
        test.var += 1
        print test.var
        print var
    
    sum()
    

    …gives:

    1
    2
    0
    

    But the moment I try to assign a value to var within the sum function, I get an error even before that line:

    >>> def sum():
    ...     Test.var += 1
    ...     print Test.var
    ...     test.var += 1
    ...     print test.var
    ...     print var
    ...     var += 1
    ...     print var
    ... 
    >>> sum()
    1
    2
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 6, in sum
    UnboundLocalError: local variable 'var' referenced before assignment
    

    Because var is now being assigned a value in sum(), it’s considered local but has not been defined prior to that line. (Which implies that python is doing some ‘looking ahead’ or checking variable scope in sum() since it raised the error for the first print var before var was re-assinged. Putting var = 50 instead of var += 1 raises the same error.)

    To work with the global var:

    def sum():
        Test.var += 1
        print Test.var
        test.var += 1
        print test.var
        global var #added the global keyword
        print var
        var += 1
        print var
    

    output:

    1
    2
    0    # notice that global var is still 0 because the above var+=1 are for Test.var
    1
    

    Edit: Regarding the ‘look ahead’ behaviour I mentioned. Was going to post a question about it but it’s been explained well in this answer: https://stackoverflow.com/a/370380/1431750 (to Python variable scope error)

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

Sidebar

Related Questions

I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass
I have question about interpreting strings as packed binary data in C++. In python,
I have a question about this formula from a book: EFW (cm,kg)= 10^(-1,7492+(0,166*BPD)+(0,046*AC)-(2,646*AC*BPD/1000)) The
I have a question about how GC works in Java. Consider the following code:
I have question about this plugin system: .NET 4.0 ASP.NET MVC 3 plug-in architecture
I have a question about local classes in Java (classes that declares in the
I have a question about variable scope. For example: class A { private static
I have a question about how closures are implemented. Say this is in a
I have question about parsing in Html helper : I have sth like: @foreach
I have question about XSLT1.0. The task is to write out in HTML all

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.