Max/min Default
Documentation Index
Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
Use this file to discover all available pages before exploring further.
Sourcery refactoring id: max-min-default
Section titled “Sourcery refactoring id: max-min-default”Description
Section titled “Description”Use max/min default argument instead of if statement
Before
Section titled “Before”a = [1, 2, 3]if a: b = max(a)else: b = 0a = [1, 2, 3]b = max(a, default=0)Explanation
Section titled “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.