Oct-04-2020, 07:21 AM
That suggests that either something else is clearing the value, or you're not actually reading from the global.
Because globals can be modified anywhere in the code, debugging means looking at every assignment that the variable has throughout your code.
Also, because lists are mutable, it's possible to assign it to something else and unwittingly clear it, thinking you're clearing a copy.
Because globals can be modified anywhere in the code, debugging means looking at every assignment that the variable has throughout your code.
Also, because lists are mutable, it's possible to assign it to something else and unwittingly clear it, thinking you're clearing a copy.
def complete_form(): global scratched_list scratched_list = [] for i, var in enumerate(['a', 'b', 'c']): scratched_list.append(f"{i} {var}") return def something_else(): my_list = scratched_list # incorrectly assumes this is a local copy my_list.clear() # clears the global as well return complete_form() print(scratched_list) something_else() print(scratched_list)
Output:['0 a', '1 b', '2 c']
[]