3

I trying to test pylint with default settings and (see python code below) get a warning:

>pylint pylint_test_01.py
>pylint_test_01.py:24:7: W0143: Comparing against a callable, did you omit the parenthesis? (comparison-with-callable)

How can I get rid of this pylint warning without breaking this algorithm and without disable pylint checks (i.e. with default settings)? General principle of enumerating functions from list should remain.

'''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))
An0ther0ne
  • 324
  • 3
  • 8

2 Answers2

5

You could use is for function comparisons:

if SrtFunc is sortaray_builtin:

This is also what happens under the hood when you use == to compare two functions, as there is no way to otherwise determine if two functions are equivalent.

Also see this question for more details on how comparisons on functions work.

Zecong Hu
  • 2,584
  • 18
  • 33
  • I'm not compare equivalence of two functions, I want to check if some function in list is exactly the same as I describe in comparison. In example at your link there two actually function definition, they looks like the same, but not. In addition, after 8 years in the version of Python 3x, this does not work as described. For example: fun1=lambda :x; fun2=lambda :x; fun1 == fun2 ==> False – An0ther0ne Mar 31 '20 at 09:06
  • 2
    @An0ther0ne I know you're not, I'm merely stating that it's impossible to check whether functions are equivalent. In this example you've given, you're still defining two *different* lambda functions, although they have the same function body. Using `==` to compare functions is exactly the same as using `is` which checks whether the values are the same object, which is why you get `False` here. – Zecong Hu Mar 31 '20 at 13:32
  • Specifics of using `is` keyword are described [here](https://stackoverflow.com/a/3258748/12446146) – An0ther0ne Apr 01 '20 at 15:36
  • 2
    @An0ther0ne I understand what `is` does, and I know that it's different from `==`. What I'm saying is, in the case of comparing functions, `==` works the same as `is`. This is because there is no way to determine equivalence of function, so no `__eq__` is defined for functions, thus `==` falls back to identity comparison, which is what `is` does. See [here](https://docs.python.org/3/reference/expressions.html#value-comparisons). – Zecong Hu Apr 01 '20 at 17:41
2

Option 1

You can disable checks for some places.

if SrtFunc == sortaray_builtin:  # pylint: disable=W0143

More info https://pylint.readthedocs.io/en/latest/user_guide/message-control.html

Option 2

You can get the name of a function with __name__ attribute.

SrtFunc.__name__ == builtin_sort.__name__

Option 3

Same as the second method, but with .__doc__

SrtFunc.__doc__ == builtin_sort.__doc__

Option 4

You can wrap your functions in objects, e.g. in dict, or create a special class.

class Sorter():
    def __init__(self, func):
        self.sort = func
builtin = Sorter(sortaray_builtin)
bubble = Sorter(sortaray_bubble)
SFUNCTIONS = [ builtin, bubble ]
builtin.sort()
Maxim Krabov
  • 685
  • 5
  • 17