0

I want to write code that will allow me to check condition only if we want to use that condition i.e

cond1 = True
cond2 = False
cond3 = True


use_cond1 = False
use_cond2 = True
use_cond3 = True

and now current code is

if cond1 and cond2 and cond3:
    dosomething

I want to add check if we should use that condition for cond1 would be use_cond1 but if I write is as

if (cond1 and use_cond1) and cond2 and cond3:
    dosomething

entire condition will be False because we don't want to use cond1, is there an effective way to remove cond1 from if statement without writing each if statement manually? thank you

rpanai
  • 12,515
  • 2
  • 42
  • 64
pandas
  • 25
  • 10

5 Answers5

2

If you are not using the condition you want it to be True to don't break the other conditions. That translates to:

(not use_cond1 or cond1)

To be more dynamic, you can use zip and all:

conds = [cond1, cond2, ...]
use_conds = [use_cond1, use_cond2, ...]

if all(not use_cond or cond for cond, use_cond in zip(conds, use_conds)):
    ...
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
1

You could default to True for that check if you don't want to use it.

if (cond1 if use_cond1 else True) and cond2 and cond3:
    do_something()
LukasNeugebauer
  • 1,331
  • 7
  • 10
1
# create a list where you pair each condition with its corresponding use-flag
conditions_list = [
    (cond1, use_cond1),
    (cond2, use_cond2),
    (cond2, use_cond_3)
]

# use list-comprehension to create a new list,
# where only the conditions are appended, whose use-flag is True
used_conditions = [cond for (cond, use_cond) in conditions_list if use_cond]

# check if all conditions from that new list are true
if all(used_conditions):
    do_something()

If this type of if-statement appears multiple times in your code, you might want to write a function, as Wakerboy135 described

py_coffee
  • 85
  • 10
  • 2
    Note that your use of `all` might not give the expected output if all `use_cond...` are `False`. In this case, `used_conditions` will be empty, and `all([])` is `True`. We can't know if that is the behaviour the OP expects in this case, as he didn't specify it, but it's worth noting what will happen in this specific case, as this code doesn't make it explicit. – Thierry Lathuille Mar 11 '22 at 12:36
  • 2
    If there are no conditions to be used, I guess using True is reasonable, but thanks for the input! It's good to know that all([]) is in fact True. You can also check my answer which produces `all([True, True, True])` even when all the use_conditions are False, because the condition will be skipped and True will be used instead. – c8999c 3f964f64 Mar 11 '22 at 13:10
  • @ThierryLathuille, didn't think about that, thanks – py_coffee Mar 11 '22 at 14:46
  • but I do see @c8999c3f964f64 's point – py_coffee Mar 11 '22 at 14:47
  • It could also be turned into a generator expression inside the `all` to save the creation of the list – Tomerikoo Mar 11 '22 at 20:10
  • @Tomerikoo Yes, but that could probably compromise readability, I'm afraid – py_coffee Mar 14 '22 at 09:29
0

Personally dont like this if condition.

I'm not a python developer, however why dont u write a function like this

def my_conditional(enable):
    if enable: 
        return condition
        
    return True

Condition is your condition that u want to apply and must return a Boolean.

Then u can call your if:

if my_conditional(False) and cond2 and cond3:
    do_something

or

if my_conditional(True) and cond2 and cond3:
    do_something
Wakerboy135
  • 91
  • 1
  • 7
0
# create all your conditions and use_conditions as lists
conditions = [cond1, cond2, cond3]
use_conditions = [use_cond1, use_cond2, use_cond3]

# use a single check which only does something if all the checks pass
if all(condition if use_condition else True for condition, use_condition in zip(conditions, use_conditions)):
    print("dosomething")

Using the ternary check if use_condition else True is slightly faster than using an or operator, because the check will be skipped if use_condition is False. (The value of "condition" will be ignored, and True will be used without evaluating "condition").

Does Python have a ternary conditional operator?

This might not be important for this use-case (because you're using True / False anyway), but if the condition has to be evaluated with some computational resources, this is worth considering. Because otherwise, all the conditions will be evaluated - even if they end up being irrelevant because the use_condition operator is False for that condition.

c8999c 3f964f64
  • 1,430
  • 1
  • 12
  • 25