Skip to content

Ternary to If Expression

Sourcery refactoring id: ternary-to-if-expression

Description

Replace boolean ternary with inline if expression.

Before

protocol = is_ssl and "https" or "http"

After

protocol = "https" if is_ssl else "http"

Explanation

Prior to Python version 2.5 the best way to create a ternary operator was to use and & or together in the before example.

The if expression syntax shows the intent of the code much clearer and so is preferred.

Back to top