I've just discovered this weird thing where if I create a global variable called for example numbers
and then call a function with a default variable with the same name it accesses that global variable even though it's not specifically passed in or referenced with the global
operator. This seems like it shouldn't be possible. Is there a reason why this happens and is this explained anywhere in the Python documentation?
This example shows what I mean:
def append(number, numbers=[]):
print(numbers)
numbers.append(number)
return numbers
numbers = append(1)
different_numbers = append(2)
print(numbers, 'end')
Output:
[]
[1]
[1, 2] end