[英]Detecting empty function definitions in python
我需要檢測一個函數是否是一個空定義。 它可以是這樣的:
def foo():
pass
或喜歡:
def foo(i, *arg, **kwargs):
pass
或喜歡:
foo = lambda x: None
使用“檢查”模塊檢測它們的最優雅的方法是什么? 有沒有比這更好的方法:
def isEmptyFunction(func):
e = lambda: None
return func.__code__.co_code == e.__code__.co_code
您提出的方法不太有效,因為具有文檔字符串的空函數的字節碼略有不同。
沒有文檔字符串的空函數的func.__code__.co_code
值是'd\\x00\\x00S'
,而帶有文檔字符串的函數的值是'd\\x01\\x00S'
。
為了我的目的,它只是添加額外的案例來測試:
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
回答最初的問題:我認為沒有更好的方法,但絕對是一種更有彈性的方法。
建立在@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
您使用的方式有效。 一個可能更“優雅”的解決方案是擁有一個函數列表,並在所有空(或所有非空)函數中將其添加到列表中,然后檢查該函數是否在列表中.
為什么要這么做? 它看起來像糟糕的設計。 我敢打賭你不會做得更快。
python -m timeit -s'def a(): pass' -s'def b(): pass' 'if a.__code__.co_code == b.__code__.co_code: pass'
1000000 loops, best of 3: 0.293 usec per loop
python -m timeit -s 'def a(): pass' -s 'def b(): pass' 'a()'
10000000 loops, best of 3: 0.0941 usec per loop
與僅進行調用相比,它的比較似乎要慢得多,因為后者的循環次數是后者的 10 倍。 等於運算符實際上肯定是調用 a。 代碼.co_code。 等式所以你只是讓事情變慢了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.