in comparisons, variables must be left

This page is not yet available in Spanish. We are working on its translation.
If you have any questions or feedback about our current translation project, feel free to reach out to us!

Metadata

ID: python-best-practices/comparison-constant-left

Language: Python

Severity: Notice

Category: Best Practices

Description

In a comparison that compare a variable with a value, put the variable on the left side of the comparison expression.

Non-Compliant Code Examples

if 1 <= i:  # use i >= 1
  pass
if 1.0 <= i:  # use i >= 1.0
  pass

Compliant Code Examples

if i >= 1:
  pass

if 0 < nextSx <= len(subject):
    px = nextPx
    sx = nextSx

if 1 in ctx:
  print("foo")
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis