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

后端 未结 3 705
野趣味
野趣味 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条回答
  •  庸人自扰
    2021-01-29 01:09

    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
    
    

    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
    

提交回复
热议问题