回答最初的问题:我认为没有更好的方法,但绝对是更有弹性的方法。
在@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
失败的原因如下:
def not_empty_returning_string():
return 'not empty'
isEmptyFunction(just_return_string) # True
以及对于 lambdas:
not_empty_lambda_returning_string = lambda x: 'not empty'
isEmptyFunction(not_empty_lambda_returning_string) # True
我构建了一个扩展版本,它还检查除文档字符串之外的常量:
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."""
pass
empty_lambda = lambda: None
empty_lambda_with_docstring = lambda: None
empty_lambda_with_docstring.__doc__ = """Empty function with docstring."""
def constants(f):
"""Return a tuple containing all the constants of a function without:
* docstring
"""
return tuple(
x
for x in f.__code__.co_consts
if x != f.__doc__
)
return (
f.__code__.co_code == empty_func.__code__.co_code and
constants(f) == constants(empty_func)
) or (
f.__code__.co_code == empty_func_with_docstring.__code__.co_code and
constants(f) == constants(empty_func_with_docstring)
) or (
f.__code__.co_code == empty_lambda.__code__.co_code and
constants(f) == constants(empty_lambda)
) or (
f.__code__.co_code == empty_lambda_with_docstring.__code__.co_code and
constants(f) == constants(empty_lambda_with_docstring)
)
测试:
#
# Empty functions (expect: is_empty_function(f) == True)
#
def empty():
pass
def empty_with_docstring():
"""this is just an example docstring."""
pass
empty_lambda = lambda: None
empty_lambda_with_docstring = lambda: None
empty_lambda_with_docstring.__doc__ = """this is just an example docstring."""
#
# Not empty functions (expect: is_empty_function(f) == False)
#
def not_empty():
print('not empty');
def not_empty_with_docstring():
"""this is just an example docstring."""
print('not empty');
not_empty_lambda = lambda: print('not empty')
not_empty_lambda_with_docstring = lambda: print('not empty')
not_empty_lambda_with_docstring.__doc__ = """this is just an example docstring."""
#
# Not empty functions returning a string (expect: is_empty_function(f) == False)
#
def not_empty_returning_string():
return 'not empty'
def not_empty_returning_string_with_docstring():
return 'not empty'
not_empty_lambda_returning_string = lambda: 'not empty'
not_empty_lambda_returning_string_with_docstring = lambda: 'not empty'
not_empty_lambda_returning_string_with_docstring.__doc__ = """this is just an example docstring."""
all([
is_empty_function(empty) == True,
is_empty_function(empty_with_docstring) == True,
is_empty_function(empty_lambda) == True,
is_empty_function(empty_lambda_with_docstring) == True,
is_empty_function(not_empty) == False,
is_empty_function(not_empty_with_docstring) == False,
is_empty_function(not_empty_lambda) == False,
is_empty_function(not_empty_lambda_with_docstring) == False,
is_empty_function(not_empty_returning_string) == False,
is_empty_function(not_empty_returning_string_with_docstring) == False,
is_empty_function(not_empty_lambda_returning_string) == False,
is_empty_function(not_empty_lambda_returning_string_with_docstring) == False,
]) # == True