Function a creates a new local variable names and assigns it a list ['Fred', 'George', 'Bill'] . So now this is another variable from the global first_names , as you already learned.
You can read about changing the list inside a function here .
One way to make function a behave the same as function b is to make function a a modifier :
def a(names): names += ['Bill']
Or you can make a pure function:
def c(names): new_list = names + ['Bill'] return new_list
And call him:
first_names = c(first_names) print first_names # ['Fred', 'George', 'Bill']
A pure function means that it does not change the state of the program, that is, it does not have any side effects, such as changing global variables.
source share