Domanda

I'm not sure how to phrase this properly, but there's a series of functions I'm calling, with another function being called inbetween where a certain condition is met. Like so:

functionOne()
if condition: conditionalFunction()
functionTwo()
if condition: conditionalFunction()
functionThree()
if condition: conditionalFunction()
functionFour()
#etc etc etc...

I feel its somewhat 'dirty' to repetitiously have the conditional function in between. There must be a more eloquent way of achieving this; I just can't seem to come up with it though :/ Much thanks.

È stato utile?

Soluzione

for step in [functionOne, functionTwo, ...]:
    step()
    if condition:
        conditionalFunction()

Depending on the context, there may be better ways to refactor your code, but this works. (If you don't need to do the conditional thing after the last function, add a check for that.)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top