normally a function only has read-only access to global variables. in order to update a global variable the code must declare which variable names it will access using the global statement. i am finding that if i initially make these variables be mutable container types such as list or dictionary, i can update the container contents without a global statement. so, instead of doing:
i could do:
is this safe to do? if not, why not?
1 2 3 4 5 6 |
counter = 0 ... def increment(): global counter counter + = 1 return |
1 2 3 4 5 |
counter = [ 0 ] ... def increment(): counter[ 0 ] + = 1 return |
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.