def play_callback() :
# say that the song stopped
music_stopped.set()
Python would lookup the value of bare names like music_stopped
using the LEGB rule (Locals, Extended, Globals, Builtins). Since music_stopped
is not a local variable, Python would next look in the enclosing (lexical) scope. Indeed, it would find music_stopped
defined in the enclosing scope of the main
function. The value is looked up dynamically -- in other words, the value returned would be the value of music_stopped
at the time when the bare name is looked up, i.e. when play_callback
is being executed. That value of music_stopped
need not be the value at the time when play_callback
was defined.
However, to bind music_stopped
to the value when play_callback
was defined, you could make music_stopped
a local variable with a default value:
def play_callback(music_stopped=music_stopped):
# say that the song stopped
music_stopped.set()
The default value is evaluated and bound to the local variable at the time when play_callback
is defined.