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
question from:
https://stackoverflow.com/questions/65861664/local-variable-referenced-before-assignment-list 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…