回答最初的问题:我不认为有更好的方法,但肯定是一个更具弹性的方法。在
建立在this之上,@kcon回答:def isEmptyFunction(func):
def empty_func():
pass
def empty_func_with_doc():
"""Empty function with docstring."""
pass
return func.__code__.co_code == empty_func.__code__.co_code or \
func.__code__.co_code == empty_func_with_doc.__code__.co_code
在以下情况下失败:
^{pr2}$
对于lambdas:not_empty_lambda_returning_string = lambda x: 'not empty'
isEmptyFunction(not_empty_lambda_returning_string) # True
我构建了一个扩展版本,除了docstring之外,它还检查常量:def is_empty_function(f):
"""Returns true if f is an empty function."""
def empty_func():
pass
def empty_func_with_docstring():
"""Empty function with docstring.""&#