← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #248294]: exists doesen't seem to work inside def!!

 

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

    Status: Open => Answered

Eugene S proposed the following answer:
Hi,

This is not a Sikuli but rather a Python question.
You cannot directly access a variable defined inside the function. All the variables you initiate inside the function are in scope of that function only and are invisible outside the function.

So if you want to make a variable available outside function you
generally have 2 options:

1. Return the variable. THIS IS THE RELEVANT IN YOUR CASE.
You should make the function return the variable, which is a standard procedure.
To do that, just add a line:

"return stuff"

at the end of your function.
Then you will be able to call you function in this manner:

print foo()

or:

result = foo()
print result

2. Make the variable global (This is just for general knowledge. Not the way to do that in this case)
You can define a variable as global in your function like that:

stuff = ''

def foo():
    global stuff
    stuff = 'hello'

foo()

print stuff


Hope that helps.

Regards,
Eugene

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