2

After I defined some functions I would like two call them in an IF loop, just if some conditions are fulfilled:

if __name__== "__main__":
if args['param'] == None:

    fun_1(x, y, z)
    fun_4(x, y, z) 
    fun_2(x, y, z)
    #SAME
    fun_a(x, y, z)
    fun_b(x, y, z)
    fun_c(x, y, z)

elif args['param'] == 'yes' or args['param'] == 'y' and param == True:
    fun_1(x, y, z)
    fun_3(x, y, z) 
    fun_6(x, y, z)
    #SAME
    fun_a(x, y, z)
    fun_b(x, y, z)
    fun_c(x, y, z)

elif args['param'] == 'no' or args['param'] == 'n' and param == True:

    fun_a(x, y, z)
    fun_b(x, y, z)
    fun_c(x, y, z)

However, it is redundant because I am calling the same functions:

    fun_a(x, y, z)
    fun_b(x, y, z)
    fun_c(x, y, z)

In the first and in the second IF statements. How can I call fun_a, fun_b, and func_c once here:

if __name__== "__main__":
if args['param'] == None:

    fun_1(x, y, z)
    fun_4(x, y, z) 
    fun_2(x, y, z)
    #SAME
    fun_a(x, y, z)
    fun_b(x, y, z)
    fun_c(x, y, z)

elif args['param'] == 'yes' or args['param'] == 'y' and param == True:
    fun_1(x, y, z)
    fun_3(x, y, z) 
    fun_6(x, y, z)
    #SAME
    fun_a(x, y, z)
    fun_b(x, y, z)
    fun_c(x, y, z)
anon
  • 836
  • 2
  • 9
  • 25
  • 1
    You have a syntax error, an `else` cannot have a condition – Olivier Melançon May 08 '19 at 18:43
  • I know @OlivierMelançon, in which other way can I reformat the statements without repetition? – anon May 08 '19 at 18:44
  • please fix it first, it makes what you want to do ambiguous – Olivier Melançon May 08 '19 at 18:46
  • Please clarify what you're trying to do. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. StackOverflow is not a coding, review, or tutorial resource. – Prune May 08 '19 at 18:46
  • You've posted two examples, one of which doesn't compile, so there's no output. You haven't shown us the actual effect you want. Why do you require 8 different functions to show your problem? It appears that you're having a minor problem in factoring Boolean logic, but it's hard to tell from this code. If so, then your first action should be to review a Python tutorial on this topic. – Prune May 08 '19 at 18:48
  • 1
    ... what is an **`IF loop`** ? `IF` is a conditional statement, loops are `for` or `while` statements ... – Patrick Artner May 08 '19 at 19:04

3 Answers3

1

You can rewrite it as follows:

if __name__== "__main__":
    if args['parm'] is None:
        fun_1(x, y, z)
        fun_4(x, y, z) 
        fun_2(x, y, z)   
    elif args['parm'] == 'yes' and param:
        fun_1(x, y, z)
        fun_3(x, y, z) 
        fun_6(x, y, z)

    if args['parm'] is None or (param and args['parm'] in ['yes', 'no']):
        fun_a(x, y, z)
        fun_b(x, y, z)
        fun_c(x, y, z)

Note that you should use foo is None as opposed to foo == None, and foo == True is the same as foo.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
1

You can use a Boolean flag that's initialized as True and is only set False if none of the desired conditions is met, and run the common functions only if the flag is True:

postprocessing = True
if args['param'] == None:
    fun_1(x, y, z)
    fun_4(x, y, z) 
    fun_2(x, y, z)
elif args['param'] == 'yes' or args['param'] == 'y' and param == True:
    fun_1(x, y, z)
    fun_3(x, y, z) 
    fun_6(x, y, z)
elif not (args['param'] == 'no' or args['param'] == 'n' and param == True):
    postprocessing = False
if postprocessing:
    fun_a(x, y, z)
    fun_b(x, y, z)
    fun_c(x, y, z)
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • what if I add another value?... check my updated version THANKS for the help! – anon May 08 '19 at 19:14
  • Glad to be of help. Adding another value won't matter to what's being done in this answer. The point is to set the flag according to the conditions, so what the conditions are does not really matter here. – blhsing May 08 '19 at 19:18
  • 1
    this is a good anser more intuitive! – anon May 08 '19 at 19:18
1

Isn't the if-elif-else statement like the format below:

if args['parm'] == None:
    #do something
elif args['parm'] == 'yes' and param == True:
    #do something else
else ##args['param'] == 'no' and param == True:
    #do something left

I have never seen conditions following the keyword else.

How can I call fun_a, fun_b, and func_c once here:

In the if-else branch, you are actually calling each of the functions once.

But as the example shown by Ami Tavory, you can refactor your code with if-elif and no else.

if args['parm'] is None:
    # do something 
elif args['parm'] == 'yes' and param:
    # do something else
fun_a(x, y, z)
fun_b(x, y, z)
fun_c(x, y, z)
Peipei
  • 136
  • 1
  • 9