问题
Considering this code snippet:
from os import walk
files = []
for (dirpath, _, filenames) in walk(mydir):
# more code that modifies files
if len(files) == 0: # <-- C1801
return None
I was alarmed by Pylint with this message regarding the line with the if statement:
[pylint] C1801:Do not use
len(SEQUENCE)as condition value
The rule C1801, at first glance, did not sound very reasonable to me, and the definition on the reference guide does not explain why this is a problem. In fact, it downright calls it an incorrect use.
len-as-condition (C1801): Do not use
len(SEQUENCE)as condition value Used when Pylint detects incorrect use of len(sequence) inside conditions.
My search attempts have also failed to provide me a deeper explanation. I do understand that a sequence's length property may be lazily evaluated, and that __len__ can be programmed to have side effects, but it is questionable whether that alone is problematic enough for Pylint to call such a use incorrect. Hence, before I simply configure my project to ignore the rule, I would like to know whether I am missing something in my reasoning.
When is the use of len(SEQ) as a condition value problematic? What major situations is Pylint attempting to avoid with C1801?
回答1:
When is the use of
len(SEQ)as a condition value problematic? What major situations is Pylint attempting to avoid with C1801?
It’s not really problematic to use len(SEQUENCE) – though it may not be as efficient (see chepner’s comment). Regardless, Pylint checks code for compliance with the PEP 8 style guide which states that
For sequences, (strings, lists, tuples), use the fact that empty sequences are false.
Yes: if not seq: if seq: No: if len(seq): if not len(seq):
As an occasional Python programmer, who flits between languages, I’d consider the len(SEQUENCE) construct to be more readable and explicit (“Explicit is better then implicit”). However, using the fact that an empty sequence evaluates to False in a Boolean context is considered more “Pythonic”.
回答2:
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.
回答3:
Next release of Pylint should no longer unnecessarily complain after fixing https://github.com/PyCQA/pylint/issues/2684 and https://github.com/PyCQA/pylint/issues/1405
Thanks to PaulRenvoise, PCManticore and adhearn for their work on fixing this!
For example if len(files) == 0 will no longer cause pylint to complain.
回答4:
Pylint was failing for my code and research led me to this post:
../filename.py:49:11: C1801: Do not use `len(SEQUENCE)` to determine if a sequence is empty (len-as-condition)
../filename.py:49:34: C1801: Do not use `len(SEQUENCE)` to determine if a sequence is empty (len-as-condition)
This was my code before:
def list_empty_folders(directory):
"""The Module Has Been Build to list empty Mac Folders."""
for (fullpath, dirnames, filenames) in os.walk(directory):
if len(dirnames) == 0 and len(filenames) == 0:
print("Exists: {} : Absolute Path: {}".format(
os.path.exists(fullpath), os.path.abspath(fullpath)))
This was after my code fix. By using the int() attribute, I seem to have satisfied the Pep8/Pylint and doesn't seem to have a negative impact on my code:
def list_empty_folders(directory):
"""The Module Has Been Build to list empty Mac Folders."""
for (fullpath, dirnames, filenames) in os.walk(directory):
if len(dirnames).__trunc__() == 0 and len(filenames).__trunc__() == 0:
print("Exists: {} : Absolute Path: {}".format(
os.path.exists(fullpath), os.path.abspath(fullpath)))
My Fix
By adding .__trunc__() to the sequence it seems to have settled the need.
I do not see a difference in the behaviour, but if anyone knows specifics that I am missing, please let me know.
来源:https://stackoverflow.com/questions/43121340/why-is-the-use-of-lensequence-in-condition-values-considered-incorrect-by-pyli