0

In this example we don't see a_int, so this code don't be work

a_int=123

def tmp():
  a_int+=2

but why this example work?

a_list=[1,2,3]
def tmp():
  a_list.append(4)

As I understood in example 2, a_list is a link, and therefore we have access to it? I'm not sure if my guesses are correct, can you please explain this to me

Oleg
  • 97
  • 5
  • Does this answer your question? [Python 3: UnboundLocalError: local variable referenced before assignment](https://stackoverflow.com/questions/10851906/python-3-unboundlocalerror-local-variable-referenced-before-assignment) – Nick Sep 10 '22 at 13:14
  • 1
    @Nick: How does that explain `a_list`? – Scott Hunter Sep 10 '22 at 13:16
  • @ScottHunter you're not assigning to `a_list`, you are modifying the object it points to... – Nick Sep 10 '22 at 13:17
  • @ScottHunter if you change the second `tmp` to `a_list = a_list + [4]` (effectively the same thing) you get the unBoundLocalError because you are assigning a new value to `a_list` – Nick Sep 10 '22 at 13:31
  • @Nick: Yes, but my point was that your "duplicate" answer doesn't address those points, and I would argue warrants its own answer (which you have pretty much laid out in comments). – Scott Hunter Sep 10 '22 at 16:02
  • @ScottHunter sorry, it was late and I didn't get your point. You're right, that answer and none of the others it links to mention lists (or dicts for that matter) and how their behaviour is different. – Nick Sep 10 '22 at 22:50

0 Answers0