Skip to content

Commit

Permalink
refactor: replace multiple == checks with in
Browse files Browse the repository at this point in the history
To check if a variable is equal to one of many values, combine the values into a tuple and check if the variable is contained `in` it instead of checking for equality against each of the values.
This is faster, less verbose, and more readable.
  • Loading branch information
deepsource-autofix[bot] authored Nov 5, 2024
1 parent 6c791bf commit 93a56f9
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions python/demo_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def bad_isinstance(initial_condition, object, other_obj, foo, bar, baz):


def check(x):
if x == 1 or x == 2 or x == 3:
if x in (1, 2, 3):
print("Yes")
elif x != 2 or x != 3:
print("also true")
Expand All @@ -140,7 +140,7 @@ def check(x):
elif x == 10 or x == 20 or x == 30 and x == 40:
print("Sweet!")

elif x == 10 or x == 20 or x == 30:
elif x in (10, 20, 30):
print("Why even?")


Expand Down
2 changes: 1 addition & 1 deletion python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def is_even(x):
return x % 2 == 0

def is_prime(x):
if x == 0 or x == 1:
if x in (0, 1):
return False

for i in range(2, x):
Expand Down

0 comments on commit 93a56f9

Please sign in to comment.