Why is the use of len(SEQUENCE) in condition values considered incorrect by Pylint?

后端 未结 4 1895
独厮守ぢ
独厮守ぢ 2021-01-30 08:08

Considering this code snippet:

from os import walk

files = []
for (dirpath, _, filenames) in walk(mydir):
    # More code that modifies files
if len(files) == 0:         


        
4条回答
  •  故里飘歌
    2021-01-30 08:20

    Note that the use of len(seq) is in fact required (instead of just checking the bool value of seq) when using NumPy arrays.

    a = numpy.array(range(10))
    if a:
        print "a is not empty"
    

    results in an exception: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

    And hence for code that uses both Python lists and NumPy arrays, the C1801 message is less than helpful.

提交回复
热议问题