← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #200874]: Exiting a function, in an imported module.

 

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

    Status: Open => Answered

RaiMan proposed the following answer:
to clarify: you say: calling the modules in turn.
this is misunderstandable:
- a module is imported (if it contains code outside of def(s) or class(), this code is executed once at import time)
- you then call the functions or use the classes contained in the module for execution
- depending on the import form all defined names are added to the calling global namespace (from x import *)

--- signaling termination (e.g. for breaking a loop) from inside a module.function
you have to return an appropriate return value, that is checked by the caller
e.g.
def myFunction():
    # some code here
    if all_is_ok: return True # or some suitable value
    else: return false

# only side effects
while True:
    if not myFunction(): break

or

# needing the return value:
while True:
    ret = myFunction()
    if not ret: break
    print ret

--- But I get an error in the reload line
reload works all over the place - so what error do you get?

--- declare a global variable across modules
- 1. If you make Sikuli scripts, the option to use Sikuli's Settings class is the easiest solution.
In the Python sense, these are not global variables, but attributes in a globally available Sikuli class.
recommended: Settings.myGlobal = True (hence use a unique prefix)

-2. If you do not like that:
- make module myGlobals
- containing
myGlobal1 = True
...
- and use "from myGlobals import *" in every module including main, that needs access to these global variables.
- take care: inside of def()'s and classes, global variables are only readable. If they are to be assigned something, you have to say
global myGlobal1
myGlobal1 = False
otherwise myGlobal1 is an unknown local variable (error).

So solution 1 is easier and more flexible, since it is always clear what
is meant: Settings.myGlobal1

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