From 93a56f9aed89adb178b5049fea1b674a8aabaaec Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Tue, 5 Nov 2024 18:56:48 +0000 Subject: [PATCH] refactor: replace multiple `==` checks with `in` 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. --- python/demo_code.py | 4 ++-- python/utils.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/python/demo_code.py b/python/demo_code.py index 5e502b20..32fcf043 100644 --- a/python/demo_code.py +++ b/python/demo_code.py @@ -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") @@ -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?") diff --git a/python/utils.py b/python/utils.py index af8508ee..4d08046b 100644 --- a/python/utils.py +++ b/python/utils.py @@ -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):