def foo(a):
print(\"I\'m foo\")
return False
if foo:
print(\"OK\")
else:
print(\"KO\")
I run it and it returns OK. I know, I should h
python if
condition statisfies if the value is not equal to any of these
0, None, "", [], {}, False, ()
Here
def foo(a):
print("I'm foo")
return False
>>>foo
<function __main__.foo>
That means variable foo
pointing to the function.If you call your function foo(arg)
then it will return False
as you expecting.So
>>>foo("arg")
False
When you write
if foo:
print("OK")
else:
print("KO")
you are actually testing if the function pointer foo
. It is defined, so it prints "OK" and it does not call the function. With the parenthesis, you call the foo
function and runs its code.
In this case your code is same as
if foo != None:
print("OK")
else:
print("KO")
Result is
"OK"
because foo
actually exists.