gpt4 book ai didi

python - PyLint W0143 警告 : Comparing against a callable

转载 作者:行者123 更新时间:2023-12-04 10:29:08 29 4
gpt4 key购买 nike

我尝试使用默认设置测试 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

您可以将函数包装在对象中,例如在 dict 中,或者创建一个特殊的类。
class Sorter():
def __init__(self, func):
self.sort = func
builtin = Sorter(sortaray_builtin)
bubble = Sorter(sortaray_bubble)
SFUNCTIONS = [ builtin, bubble ]
builtin.sort()

关于python - PyLint W0143 警告 : Comparing against a callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60491475/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com