Questions about script modules - global variables, scope, etc

Python and Jython import a module by executing it once and effectively loading the resulting module globals dictionary (including both 'x' and 'changeX' in your example) into RAM under the module name. Note that executing a def statement places the following function code in the module dictionary as a callable object -- it doesn't execute the function during import. Further imports simple reference or retrieve items from that symbol dictionary.

Yes, in current versions of Ignition, the script console is in its own context. Consider testing using actionPerformed events of generic buttons.

No, the global variable 'x' is instantiated when the script is imported the first time. If you didn't have 'global x' in your function, the += assignment would add one to the value of the global variable and save it in a function local variable 'x', leaving the module level 'x' unchanged. The 'global x' statement tells python to divert assignments to 'x' back to the module level.

1 Like