Lately, I've had to write functions that look like this:
fruits = []
def foo():
global fruits
...
What exactly does the line global fruits
do? I apologize if this question has been asked before, but I could not find one like this.
Lately, I've had to write functions that look like this:
fruits = []
def foo():
global fruits
...
What exactly does the line global fruits
do? I apologize if this question has been asked before, but I could not find one like this.
It tells the compiler that binding the name should be performed in the module scope rather than the local scope. It has no use if you're simply mutating the object (e.g. fruits.append('apple')
).