saber
is None
in b()
because you have defined it as a global variable with a value of None
and have not defined a local variable of the same name in that function. In a()
you defined saber
with a value of "ex"
and when you printed that variable, that's what you got. But that value of saber
went away when the function finished, because it is local to the function, and that's what local variables do.
Now you might ask why rider
was changed in a()
when saber
was not. But in fact, rider
was not changed. You did not assign to the name rider
in a()
, so the global variable rider
was used. The contents of rider
were changed (you added a new key/value pair) but rider
itself is still the same dictionary object it was when you first defined it. And that's why when you print it in b()
you get the changed dictionary. Both functions are using the global variable rider
because there is no local variable of the same name "hiding" it.