# How can I check the returned boolean of a function?

I am attempting the check whether a function returns True or False in order to procede to the next step of a separate function.

For example, here is a stripped down version of what I have:

def target(modifier):
if modifier == 'MIRROR':
return True
elif modifier == 'ARRAY':
return False

def execute(self, context):
if target is True:
print("True")
else:
print("False")


This does not work, though. How can I check what the return boolean of target() is?

• Okay I believe I've got it with: "if target(modifier) is True:" Is there a better way to do this? – Jonathan Williamson Jul 3 '13 at 20:47
• Yes, that is the way to do it. Unless target() is run and assigned to a variable, there is nothing to pass in to execute(). – CharlesL Jul 3 '13 at 21:13
• Pure Python questions would be better placed over at Stack Overflow. – Aldrik Jul 4 '13 at 1:29
• Good call @Aldrik – Jonathan Williamson Jul 4 '13 at 15:15

The way you are doing it in the second block is not what you think you are doing. The line

if target is True:


will ask if a function reference is the same as a literal True...which it is not. You want to invoke the function, so use the parenthesis and pass in the string you want.
If the parameter "context" is the one you are interested in comparing (if you are using BPY, it isn't, heads up), it will instead be:

if target(context):


Notice I drop the is True", if is doing that for you already, you don't need to do it again.

• Is doing "if target(context):" the same as doing "if target(context) is True:"? – Jonathan Williamson Jul 3 '13 at 21:27
• Yes, without redundancy. "if" will only execute true statements, so just returning true is enough. Checking if True = True is a tautology. – Kirbinator Jul 3 '13 at 21:43
• @JonathanWilliamson No, if obj: checks truthiness. if obj is True: checks identity. – Aldrik Jul 4 '13 at 1:05
• @Aldrik: Good point, you would filter out returned strings or numbers this way. – Kirbinator Jul 7 '13 at 20:08

With True/False/None you can check identity.

if target(obj) is True:
# do_something


Though in most cases there is no need to be so explicit and you can do...

if target(obj):
# do_something


In some cases you may want to test against True though.

How about something like this:

import bpy

# Input String the name of modifier
def exists(modifiername):
if bpy.context.active_object.modifiers.get(modifiername):
print("yes, true")
return True
else:
print("not true")
return False

exists("Mirror")