1
def check():
    if (cond):
        how to return back to explore()
    else:
        return 1 # here return to f2

def cannot_be_modified():
    try:
        check() # inserted
        print("hello from cannot_be_modified")
        return 2
    except Exception as e:
        print(e)

def explore():
    cannot_be_modified()
    print("hello from explore")
    return 0

explore()

Call stack is: explore() -> cannot_be_modified() --> check()

If I meet some conditions in check, I want to quit check and cannot_be_modified and come back to explore. So how can I achieve this?

I thought about raising a specific type of exception in check function and catch it in explore but that exception can be caught in function cannot_be_modified

Does anyone have ideas?

Thanks

  • Could you just return early given some condition in each function? – Tom May 22 '18 at 08:27
  • 1
    This thread may help you: https://softwareengineering.stackexchange.com/questions/18454/should-i-return-from-a-function-early-or-use-an-if-statement?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Tom May 22 '18 at 08:28
  • Do you mean that you cannot modify the code inside `cannot_be_modified` ? – PM 2Ring May 22 '18 at 08:30
  • Have you thought of using exceptions to do this? https://stackoverflow.com/questions/2052390/manually-raising-throwing-an-exception-in-python#24065533 read this first to be aware of the pitfalls in this approach but I think it will work. – Paula Thomas May 22 '18 at 08:33
  • @TCouch: we can only add `check` to the `cannot_be_modified` function, cannot modify any other thing. @ Paula Thomas: I can raise exception in `check` but it can be caught and handled by `cannot_be_modified` and cannot reach `explore` – Xuan Linh Ha May 22 '18 at 08:38

1 Answers1

1

Even if it's not the most elegant solution, you may decide to raise an exception (built-in or custom) in your check function and catch that in the explorer function. Be sure that you're catching only your exception.

def check():
  if True:
      raise ValueError("MyError")
  else:
      return 1 # here return to f2

def cannot_be_modified():
    check() # inserted
    print("hello from cannot_be_modified")
    return 2

def explore():
  try:
    cannot_be_modified()
  except ValueError as e:
    print(e)

  print("hello from explore")
  return 0

explore()

output:

MyError
hello from explore
Gsk
  • 2,929
  • 5
  • 22
  • 29
  • Hi @Gsk: `cannot_be_modified` here does not have `try` but `cannot_be_modified` is implemented by another party, it may have `try` and can catch exception from `check()`. Then `explore()` cannot catch that exception. I modified the `cannot_be_modified` for that case. – Xuan Linh Ha May 22 '18 at 09:02
  • have a look at how exceptions works, and try my code! you do not have to do anything with `cannot_be_modified`: if Exception are handled properly in this third-party function (i.e. there are no `try/except` catching everything) the exception will raise till your `explore` function – Gsk May 22 '18 at 09:09
  • if there is not `try/except` in `cannot_be_modified` function, your solution is correct but if `cannot_be_modified` has `try/except`, then `explore` cannot catch the exception from `check`. Because `cannot_be_modified` cannot be modified and implemented by a third-party, so they can arbitrarily write anything they want. – Xuan Linh Ha May 22 '18 at 15:04