PyLint W0143 警告:与可调用对象进行比较
PyLint W0143 warning: Comparing against a callable
我尝试使用默认设置测试 pylint 并且(请参阅下面的 python 代码)收到警告:
>pylint pylint_test_01.py
>pylint_test_01.py:24:7: W0143: Comparing against a callable, did you omit the parenthesis? (comparison-with-callable)
如何在不破坏此算法且不禁用 pylint 检查(即使用默认设置)的情况下摆脱此 pylint 警告?
从列表中枚举函数的一般原则应该保留。
'''Test pylint with default settings warnings'''
from random import randint
def sortaray_builtin(arr):
'''This is built in Python sort function'''
return sorted(arr.copy())
def sortaray_bubble(arr):
'''Bubble sorting algorithm -- is a simplest of sorting algorithms. Perfomance: O(N**2)'''
brr = arr.copy()
for i, _ in enumerate(brr[:-1]):
for j in range(i, len(brr)):
if brr[i] > brr[j]:
brr[i], brr[j] = brr[j], brr[i]
return brr
SFUNCTIONS = [
sortaray_builtin,
sortaray_bubble
]
ARSIZE = 20
ARRY = [randint(0, ARSIZE) for i in range(ARSIZE)]
for SrtFunc in SFUNCTIONS:
# Line below cause an W0143: Comparing against a callable, did you omit the parenthesis? (comparison-with-callable)
if SrtFunc == sortaray_builtin:
print("Builtin: ", end='', flush=True)
else:
print("Bubble : ", end='', flush=True)
print(SrtFunc(ARRY))
选项 1
您可以禁用某些地方的检查。
if SrtFunc == sortaray_builtin: # pylint: disable=W0143
更多信息
https://pylint.readthedocs.io/en/latest/user_guide/message-control.html
选项 2
您可以通过 __name__
属性获取函数名称。
SrtFunc.__name__ == builtin_sort.__name__
选项 3
与第二种方法相同,但 .__doc__
SrtFunc.__doc__ == builtin_sort.__doc__
选项 4
您可以将函数包装在对象中,例如在字典中,或者创建一个特殊的 class.
class Sorter():
def __init__(self, func):
self.sort = func
builtin = Sorter(sortaray_builtin)
bubble = Sorter(sortaray_bubble)
SFUNCTIONS = [ builtin, bubble ]
builtin.sort()
您可以使用 is
进行函数比较:
if SrtFunc is sortaray_builtin:
这也是当您使用 ==
比较两个函数时在幕后发生的事情,因为没有其他方法可以确定两个函数是否等价。
另请参阅 this question 了解有关函数比较工作原理的更多详细信息。
我尝试使用默认设置测试 pylint 并且(请参阅下面的 python 代码)收到警告:
>pylint pylint_test_01.py
>pylint_test_01.py:24:7: W0143: Comparing against a callable, did you omit the parenthesis? (comparison-with-callable)
如何在不破坏此算法且不禁用 pylint 检查(即使用默认设置)的情况下摆脱此 pylint 警告? 从列表中枚举函数的一般原则应该保留。
'''Test pylint with default settings warnings'''
from random import randint
def sortaray_builtin(arr):
'''This is built in Python sort function'''
return sorted(arr.copy())
def sortaray_bubble(arr):
'''Bubble sorting algorithm -- is a simplest of sorting algorithms. Perfomance: O(N**2)'''
brr = arr.copy()
for i, _ in enumerate(brr[:-1]):
for j in range(i, len(brr)):
if brr[i] > brr[j]:
brr[i], brr[j] = brr[j], brr[i]
return brr
SFUNCTIONS = [
sortaray_builtin,
sortaray_bubble
]
ARSIZE = 20
ARRY = [randint(0, ARSIZE) for i in range(ARSIZE)]
for SrtFunc in SFUNCTIONS:
# Line below cause an W0143: Comparing against a callable, did you omit the parenthesis? (comparison-with-callable)
if SrtFunc == sortaray_builtin:
print("Builtin: ", end='', flush=True)
else:
print("Bubble : ", end='', flush=True)
print(SrtFunc(ARRY))
选项 1
您可以禁用某些地方的检查。
if SrtFunc == sortaray_builtin: # pylint: disable=W0143
更多信息 https://pylint.readthedocs.io/en/latest/user_guide/message-control.html
选项 2
您可以通过 __name__
属性获取函数名称。
SrtFunc.__name__ == builtin_sort.__name__
选项 3
与第二种方法相同,但 .__doc__
SrtFunc.__doc__ == builtin_sort.__doc__
选项 4
您可以将函数包装在对象中,例如在字典中,或者创建一个特殊的 class.
class Sorter():
def __init__(self, func):
self.sort = func
builtin = Sorter(sortaray_builtin)
bubble = Sorter(sortaray_bubble)
SFUNCTIONS = [ builtin, bubble ]
builtin.sort()
您可以使用 is
进行函数比较:
if SrtFunc is sortaray_builtin:
这也是当您使用 ==
比较两个函数时在幕后发生的事情,因为没有其他方法可以确定两个函数是否等价。
另请参阅 this question 了解有关函数比较工作原理的更多详细信息。