Why does “if foo:” follow the branch even if the function foo returns False?

后端 未结 3 699
野趣味
野趣味 2021-01-29 00:14
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

3条回答
  •  旧时难觅i
    2021-01-29 01:15

    In this case your code is same as

    if foo != None:
        print("OK")
    else:
        print("KO")
    

    Result is

    "OK"

    because foo actually exists.

提交回复
热议问题