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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:15:29+00:00 2026-05-16T08:15:29+00:00

Given the following code: a = 0 def foo(): # global a a +=

  • 0

Given the following code:

a = 0
def foo():
  # global a
  a += 1
foo()

When run, Python complains: UnboundLocalError: local variable ‘a’ referenced before assignment

However, when it’s a dictionary…

a = {}
def foo():
  a['bar'] = 0
foo()

The thing runs just fine…

Anyone know why we can reference a in the 2nd chunk of code, but not the 1st?

  • 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-05-16T08:15:29+00:00Added an answer on May 16, 2026 at 8:15 am

    The difference is that in the first example you are assigning to a which creates a new local name a that hides the global a.

    In the second example you are not making an assignment to a so the global a is used.

    This is covered in the documentation.

    A special quirk of Python is that – if no global statement is in effect – assignments to names always go into the innermost scope.

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

Sidebar

Related Questions

Given the following controller code def update params[:image][:contributor_ids] ||= [] params[:image][:category_ids] ||= [] @image
The following code works: class Foo(tuple): def __init__(self, b): super(Foo, self).__init__(tuple(b)) if __name__ ==
Given the following example code: def myfunc(item): if item == 2: item = 1
Given the following code: def slope(x1, y1, x2, y2): >>> slope(5, 3, 4, 2)
Consider the following Scala code (e.g., in REPL) object A{def foo:Unit = {}} object
Given the following code: def create @something = Something.new(params[:something]) thing = @something.thing # another
Given the following code: class MagicList def items=(array) @items = array.map{|x| x*2} end def
Given the following code: abstract class Field { type T val data: List[T] def
Consider the following (broken) code: import functools class Foo(object): def __init__(self): def f(a,self,b): print
Given the following code: class A extends Actor { def act() { loop {

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.