Namespace for Python actions and callbacks

mhisted's Avatar


13 Feb, 2018 06:03 PM

What namespace do callbacks (mw.register_event_callback) execute in? And what namespace does run_python_string() execute in? From some testing that I could have gotten wrong, it seems like run_python_string('a=1'), and then accessing 'a' in a callback doesn't seem to work. Am I missing something?


  1. Support Staff 1 Posted by Christopher Sta... on 13 Feb, 2018 06:28 PM

    Christopher Stawarz's Avatar

    Hi Mark,

    The run_python_file and run_python_string actions, along with Python file resources, execute their Python code in the __main__ module, just like a Python script run from the command line.

    Callback functions (like all Python functions) follow Python's scoping rules for name resolution. Unqualified names (like a) are searched for first in the function's local scope, then in the local scopes of any enclosing functions, then in the namespace of the module in which the function is defined.

    My guess is that you're using an import statement to import your callback from a module, e.g.

    from my_callbacks import a_callback

    If you want the callback to have access to a global variable a, then you'll need to define that name in the callback's module, e.g.:

    my_callbacks.a = 3


  2. 2 Posted by mhisted on 13 Feb, 2018 08:51 PM

    mhisted's Avatar

    Thanks, Chris.

    I was wondering if the callback functions got hoisted out to run in the __main__ namespace, but it appears not.
    This is all I need.


  3. Christopher Stawarz closed this discussion on 13 Feb, 2018 09:00 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac