← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #209132]: var across module

 

Question #209132 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/209132

    Status: Open => Answered

RaiMan proposed the following answer:
principally this should access myVar in myModule:

# in main
import myModule
myModule.myVar = 0

But the problem you have:
the code in myModule is executed once at the time of the import.

so your code in myModule:
if (myVar=="0"):print myVar
else:print myVar

would be executed before something else is done in main and never again.
... and this would give a name error, since, myVar is not defined at the time of the if.

Modules usually do only contain code for some initialization. The rest
is classes and/or functions.

If you want to execute code from other .py's in the context of main,
then use execfile() instead of import.

With a module you should wrap the features you want to use in main into
def()'s:

main.py
-------
import myModule

if (arg=="foo"):myVar=1
else:myVar=0

myModule.doSomething(myVar)

myModule.py
-----------
def doSomething(myVar):
    if (myVar=="0"):print myVar
    else:print myVar

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.