Skip to content

Simplify-Constant-Sum

Sourcery rule id: simplify-constant-sum

Description

Simplify constant sum() call

Before

sum(1 for book in books if book.author == "Terry Pratchett")

After

sum(bool(book.author == "Terry Pratchett") for book in books)

Explanation

As sum add the values it treats True as 1, and False as 0. We make use of this fact to simplify the generator expression inside the sum call.