I encountered an interesting error. When defining a list outside the scope of a function, like below, it causes an error: UnboundLocalError: local variable 'a' referenced before assignment
def main():
a = []
def sub():
a += ["hello"]
return a
sub()
main()
However, with the same logic, if used a.append
no error is raised
def main():
a = []
def sub():
a.append("hello")
return a
sub()
main()
Is there any reason for this?
Thanks