Solved by mariosasko in post #2 
              
              
                I see two options. 
When registering a hook, the handle is returned that can be used as a context-manager. When this context-manager exits, the hook gets deactivated. This is not probably documented in the offical docs but you can check the source code for that. 
The snippet below shows how this wor…