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
[pylint] C1801:Do not useas condition value
Do not useas condition value Used when Pylint detects incorrect use of len(sequence) inside conditions.
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”.