Skip to content

Max/min Default

Sourcery refactoring id: max-min-default

Description

Use max/min default argument instead of if statement

Before

a = [1, 2, 3]
if a:
    b = max(a)
else:
    b = 0

After

a = [1, 2, 3]
b = max(a, default=0)

Explanation

When using max/min over an iterable we often want to provide a default value.

Instead of setting this using an if statement we can use the default keyword argument of max/min directly. This makes the intent of the code much clearer and makes it much easier to read.

Back to top