Is there any way to tell if a function object was a lambda or a def?

后端 未结 3 1310
感动是毒
感动是毒 2021-01-12 10:56

Consider the two functions below:

def f1():
    return \"potato\"

f2 = lambda: \"potato\"
f2.__name__ = f2.__qualname__ = \"f2\"

Short of

3条回答
  •  -上瘾入骨i
    2021-01-12 11:26

    Here

    def f1():
        return "potato"
    
    
    f2 = lambda: "potato"
    
    
    def is_lambda(f):
        return '' in str(f.__code__)
    
    
    print(is_lambda(f1))
    print(is_lambda(f2))
    

    output

    False
    True
    

提交回复
热议问题