Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong UnboundLocalError with += operator #48359

Closed
eric mannequin opened this issue Oct 12, 2008 · 3 comments
Closed

Wrong UnboundLocalError with += operator #48359

eric mannequin opened this issue Oct 12, 2008 · 3 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@eric
Copy link
Mannequin

eric mannequin commented Oct 12, 2008

BPO 4109
Nosy @benjaminp

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2008-10-12.14:08:36.680>
created_at = <Date 2008-10-12.13:56:41.695>
labels = ['interpreter-core', 'type-bug', 'invalid']
title = 'Wrong UnboundLocalError with += operator'
updated_at = <Date 2008-10-12.14:08:36.678>
user = 'https://bugs.python.org/eric'

bugs.python.org fields:

activity = <Date 2008-10-12.14:08:36.678>
actor = 'benjamin.peterson'
assignee = 'none'
closed = True
closed_date = <Date 2008-10-12.14:08:36.680>
closer = 'benjamin.peterson'
components = ['Interpreter Core']
creation = <Date 2008-10-12.13:56:41.695>
creator = 'eric'
dependencies = []
files = []
hgrepos = []
issue_num = 4109
keywords = []
message_count = 3.0
messages = ['74666', '74667', '74668']
nosy_count = 2.0
nosy_names = ['benjamin.peterson', 'eric']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue4109'
versions = ['Python 2.5.3']

@eric
Copy link
Mannequin Author

eric mannequin commented Oct 12, 2008

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

@eric eric mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Oct 12, 2008
@benjaminp
Copy link
Contributor

Is code a global variable?

@benjaminp
Copy link
Contributor

Inplace operators ("+=", "-=" etc) are assignments since in the case of
immutable types like str

code += "some string"

is equivalent to

code = code + "some string"

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant