0

When function1 is successfully finished then run function2. But this need to be in function2, not as if statement outside of functions. How to call success finish of function1?

EDIT

import time

def function1():
    print ('This is function1!')
    time.sleep(5)
    print ('Function1 is successful')

function1()

def function2():
    if function1 is success #this part I don't get (How to write this line?)
        print ('This is something about function1')
    else:
        pass
function2()
project.py
  • 107
  • 1
  • 4
  • 19
  • 1
    Can you show some code, what you tried, what failed... ? Otherwise, your question is quite difficult to understand – qmeeus Aug 25 '21 at 13:51
  • just call it like how you would call a function outside of the function? – Karina Aug 25 '21 at 13:52
  • 1
    https://stackoverflow.com/questions/1904351/python-observer-pattern-examples-tips – Riccardo Bucco Aug 25 '21 at 14:03
  • do either function return a value? – JonSG Aug 25 '21 at 14:31
  • I have updated question, I created dummy functions, I hope it is clear now. – project.py Aug 25 '21 at 15:29
  • In your code, `function2()` won't be called until after `function1()` finishes; that's how Python functions behave by default, you'd have to use `threading` or `multiprocessing` for any other behavior to be possible. – jasonharper Aug 25 '21 at 15:31
  • Your question is confusing. Are you saying that function1 can possibly succeed or fail, and you want some code inside function2 to deal with that, or are you saying that you just want function2 to run when function1 is _finished_? – John Gordon Aug 25 '21 at 15:33
  • First function can succeed or fail, and in other function I want... if first function succeed do something more, if it fails, to do nothing. Both functions are called (function1 first, and function2 second) – project.py Aug 25 '21 at 15:37

1 Answers1

1

Use a global variable. Set it in function1, and check it in function2.

function1_success = False

def function1():
    global function1_success
    # do stuff ...
    if success:
        function1_success = True

def function2():
    global function1_success
    if function1_success:
        # do stuff ...
John Gordon
  • 29,573
  • 7
  • 33
  • 58