Skip to content

Ternary to If Expression

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: ternary-to-if-expression

Section titled “Sourcery refactoring id: ternary-to-if-expression”

Replace boolean ternary with inline if expression.

protocol = is_ssl and "https" or "http"
protocol = "https" if is_ssl else "http"

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.