[New-bugs-announce] [issue4109] Wrong UnboundLocalError with += operator

Eric Atienza report at bugs.python.org
Sun Oct 12 15:56:42 CEST 2008


New submission from Eric Atienza <eric at ericaro.net>:

the following code :

def test():
    code=''
    def sub(n):
        for i in range(n):
            code+=str(i)
    sub(5)
    sub(10)
    return code

>>> test()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "<console>", line 6, in test
  File "<console>", line 5, in sub
UnboundLocalError: local variable 'code' referenced before assignment

error came from the += operator.
Tested for code initialized to '', to 0
I guess it's the same for all inline operators.

I agree that global variables CANNOT be assigned, it's ok.

But += (and I guess *= etc) operators are not assignements, and are not 
different from .append(), or .extend() methods.

I was expecting += to work the same as append() method

----------
components: Interpreter Core
messages: 74666
nosy: atienza
severity: normal
status: open
title: Wrong UnboundLocalError with += operator
type: behavior
versions: Python 2.5.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4109>
_______________________________________


More information about the New-bugs-announce mailing list