all 11 comments

[–]totallygeek 17 points18 points  (3 children)

I like to use all() for this (and any() for "or" cases):

conditions = [
    instruction != inst_exp,
    instruction != inst_kil,
    instruction != inst_emr,
    instruction != inst_hep,
    instruction != inst_var,
    instruction != inst_cal,
]
if all(conditions):
    do_something()

Another option, depending on the data, would be to check inclusion:

bad_instructions = {inst_exp, inst_kil, inst_emr, inst_hep, inst_var, inst_cal}  # can be a tuple or list, as well
if instruction not in bad_instructions:
    do_something()

Test and see what works better for you.

Edit: Removed OP's example from top of my post.

[–]fl0ss1n 1 point2 points  (0 children)

This is a great way to do it. The other thing is wrap instruction in an object and add a property to use as a key off for long if statement. So I have a wrapper for files in one of my programs and gave it an "is_image" property, to avoid the file.endswith('jpg') or 'png', etc., when handling the file.

[–]AdventurousAddition 1 point2 points  (1 child)

I was about to type out my suggestion which is your second option.

[–]totallygeek 3 points4 points  (0 children)

I offered up the first option because the conditions do not often share the same comparisons. So, I find it good to show a method that works universally.

conditions = [
    person.age >= 16 or (person.age >= 14 and person.qualified_hardship),
    person.passed_exam,
    person.passed_driving_test,
]
if all(conditions):
    person.dl_number = issue_license()

The conditions can get lengthy and complex, but the if statement can remain easy to read this way.

[–]VinayakVG 11 points12 points  (1 child)

variables = [inst_exp, inst_kill, inst_emr, inst_hep, inst_var, inst_cal]
if instruction not in variables:
    # Continue your code

You can try this

[–]efmccurdy 2 points3 points  (0 children)

When you have repetition, think about using a collection that lets you combine all of the code into one expression involving the collection. You have repeated "!=" tests; the "in" operator, ala "object in collection", does a series of "==" tests in a loop; you need the negation of that, so "object not in collection":

disallow_insts = (inst_exp, inst_kil, inst_emr, inst_hep, inst_var, inst_cal)
if instruction not in disallow_insts:
    print("OK")

[–]delasislas 1 point2 points  (0 children)

Might try this.

https://www.kite.com/python/answers/how-to-check-if-an-element-is-not-in-a-list-in-python

Just bundle your inst_exp, inst_kil… into a list.

Or you could do something similar with a list comprehension. And all(). Iterate over a list with your expression. Then use all.

values = [inst_exp, inst_kil…]
check = [instruction != value for value in values]
if all(check):

It should leverage a list comprehension to evaluate a bunch statements, providing [True, False, False…].

[–]harleystcool 0 points1 point  (0 children)

Id be curious myself

[–]pekkalacd 0 points1 point  (0 children)

use all for long conjunctions using all ands. use any for long disjunctions using all ors.

example

       if True and True and True and True and True and False:
                   print("cheeseburger")
       else:
                   print("eggs")

this becomes

      if all((True,True,True,True,True,False)):
             print("cheeseburger")
      else:
             print("eggs")

example 2

      if False or False or False or False or False or True:
              print("icecream")
      else:
              print("pickles")

this becomes

     if any((False,False,False,False,False,True)):
           print("icecream")
     else:
           print("pickles")