Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Function call every other time

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.

like image 768
user3063850 Avatar asked Aug 03 '26 21:08

user3063850


1 Answers

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.)

like image 66
user2357112 supports Monica Avatar answered Aug 06 '26 11:08

user2357112 supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!