Skip to content

Use len()

Sourcery refactoring id: use-len

Description:

Replaces sum() with len where appropriate

Before:

if sum(1 for hat in hats) > 0:
    self.shout("I have hats")

After:

if len(hats) > 0:
    self.shout("I have hats")

Explanation:

This simplification can be made where the sum() call is effectively just determining the length of a sized object. It can only be made where Sourcery can determine that hats supports the __len__() method.