len
used as condition PYL-C1802len(SEQUENCE)
without comparison to determine if a sequence is empty892 with p.group(len(name) + 1, name + "(", ")"):
893 if cycle:
894 p.text("...")
895 elif len(obj):896 p.pretty(dict(obj))
len(SEQUENCE)
without comparison to determine if a sequence is empty874 with p.group(len(name) + 1, name + "(", ")"):
875 if cycle:
876 p.text("...")
877 elif len(obj):878 p.pretty(list(obj.items()))
879
880
len(SEQUENCE)
without comparison to determine if a sequence is empty1552 with p.group(0, name + "(", ")"):
1553 if cycle:
1554 p.text("...")
1555 elif len(self):1556 p.break_()
1557 p.pretty(dict(self))
1558
len(SEQUENCE)
without comparison to determine if a sequence is empty 354 with p.group(0, name + "(", ")"):
355 if cycle:
356 p.text("...")
357 elif len(self): 358 p.break_()
359 p.pretty(dict(self))
360
len(SEQUENCE)
without comparison to determine if a sequence is empty162 with p.group(0, name + "(", ")"):
163 if cycle:
164 p.text("...")
165 elif len(self):166 p.break_()
167 p.pretty(dict(self))
168
Using the len
function to check if a sequence is empty is not idiomatic and can be less performant than checking the truthiness of the object.
len
doesn't know the context in which it is called, so if computing the length means traversing the entire sequence, it must; it doesn't know that the result is just being compared to 0.
Computing the boolean value can stop after it sees the first element, regardless of how long the sequence actually is.
Not preferred:
if not len(my_sequence):
print("Empty sequence.")
else:
print("Sequence is not empty.")
Preferred:
if not my_sequence:
print("Empty sequence.")
else:
print("Sequence is not empty.")