Reputation: 55
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
Upvotes: 0
Views: 15061
Reputation: 1627
# 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.
Upvotes: 0
Reputation: 19414
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)):
...
Upvotes: 2
Reputation: 95
# 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
Upvotes: 1
Reputation: 129
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
Upvotes: 0
Reputation: 1337
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()
Upvotes: 1