To my knowledge, it is not possible to generically capture the assignment of a global symbol in Python (At least in CPython where globals are stored in a dict in the module object, both are C types that cannot be monkey patched).
Here's a simple workaround that's a bit of a compromise. Use a wrapper object to store your monitored variables, and define __setattr__ to do whatever you want to do before (or after) setting an attribute.
class CaptureOnSetAttribute:
def __setattr__(self, attr, value):
# our hook to do something
print(f'set value of {attr} to {value}')
# actually set the attribute the normal way after
super().__setattr__(attr, value)
wrapper_object = CaptureOnSetAttribute()
The compromise of course is that now instead of writing something like:
monitored_global = value
You must now write:
wrapper_object.monitored_attribute = value
tkinteruse classes for this - ie.IntVar()usesget()/set()to work with data but it has alsotracer(callback)to assign function(s) which will be executed when value is changed. Similar classes -Properties- usesKivy. So you could create own classes with similar functionality.ayou have a globalvariable, and then in moduleb,import a; a.variable = 123. This specific case is more doable as you can override the module object thatimport aresults in, and then override the__setitem__of that object. At that point though you are really going out of your way to do something that maybe is not meant to be done