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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:53:37+00:00 2026-06-14T21:53:37+00:00

>>> x = [] >>> y = [1,2,3] >>> def func1(L): … x+=L …

  • 0
>>> x = []
>>> y = [1,2,3]
>>> def func1(L):
...     x+=L
... 
>>> def func2(L):
...     x.extend(L)
... 
>>> func1(y)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in func1
UnboundLocalError: local variable 'x' referenced before assignment
>>> func2(y)
>>> x
[1, 2, 3]

Why can the list extend() method alter the non-global variable, but the += operator cannot? From what I understand, as long as the function doesn’t assign the variable, it can read it without the global designation. but in this case the function does set the value.

  • 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-14T21:53:38+00:00Added an answer on June 14, 2026 at 9:53 pm

    It doesn’t set the value, it modifies the existing value. In Python binding a name to a value is one thing, and modifying (or mutating) a value is another thing.

    The real question is why += doesn’t work, since for lists this operation is defined to also modify the list in-place. The answer is just that, because that operator looks like assignment, it was decided to make it act like assignment for scoping and binding purposes. The documentation says (emphasis added):

    An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target.

    In other words, augmented assignment includes an assignment as part of the operation, so the normal scoping/binding rules apply. Since the target is evaluated as a target, if it’s a function it’s treated as a local variable. So when x is a list, x += newList is effectively like:

    x.extend(newList)
    x = x
    

    More generally, augmented assignment can modify the operand in-place, but it still then attempts to rebind the resulting value to the original variable. So x += other works essentially like:

    y = x.__iadd__(other)
    x = y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: class SomeClass: @classmethod def func1(cls,arg1): #---Do Something--- @classmethod def func2(cls,arg1):
I have the following class; class myStringMethod(): def __init__(self): self.func_list= [('func1','print_func1()'),('func2','print_func2()')] def print_func1(self, name):
I'm trying to do something like this: import threading def func1(a,b): def func2(): t=threading.Thread(target=func3)
I've got a piece of code similar to this: import sys def func1(): func2()
Having a class class A(object): z = 0 def Func1(self): return self.z def Func2(self):
can I call a list of functions and use list comprehension? def func1(): return
I'm trying to run 2 functions at the same time. def func1(): print('Working') def
given two classes: abstract class ClassA(val argA:Int = 1) { def func1 { ...
I want to call multiple modules using for loop in paintEvent(), like def fun1(self,paint,a,b):
The following script produces a local variable 'var' referenced before assignment error: def func1():

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.