This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: |= set update scoping
Type: Stage: resolved
Components: Parser Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: jabowery2, lys.nikolaou, pablogsal
Priority: normal Keywords:

Created on 2021-11-03 20:24 by jabowery2, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg405643 - (view) Author: James Bowery (jabowery2) Date: 2021-11-03 20:24
Comment out the |= line and it prints "{'b':2}" as expected.

$ cat t.py
scoped_dict = {'b':2}
def scoped_def():
    print(scoped_dict)
    scoped_dict |= {'a',1}
scoped_def()

$ p t.py 
Traceback (most recent call last):
  File "/home/jabowery/dev/t.py", line 5, in <module>
    scoped_def()
  File "/home/jabowery/dev/t.py", line 3, in scoped_def
    print(scoped_dict)
UnboundLocalError: local variable 'scoped_dict' referenced before assignment

$ python --version
Python 3.10.0
msg405645 - (view) Author: James Bowery (jabowery2) Date: 2021-11-03 20:33
My mistake.  Disregard.
msg405646 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-11-03 20:34
This is not a bug. You are getting an UnboundLocalError because in the scope of scoped_def function, scoped_dict is marked as local (because you assign to it in the function) but you are reading it before that. 

Please, take a look at:

https://docs.python.org/3/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value

and possibly at

https://docs.python.org/3.3/tutorial/classes.html#python-scopes-and-namespaces
https://docs.python.org/3/library/exceptions.html#UnboundLocalError
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 89868
2021-11-03 23:07:27steven.dapranosetstage: resolved
2021-11-03 20:34:29pablogsalsetresolution: not a bug
messages: + msg405646
stage: resolved -> (no value)
2021-11-03 20:33:04jabowery2setstatus: open -> closed

messages: + msg405645
stage: resolved
2021-11-03 20:24:40jabowery2create