Python Forum
exiting the outer function from the inner function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
exiting the outer function from the inner function
#3
You mentioned that this was a simplified example. If you really need to do this, the common way is using Sentinel values to signal something. It's a value that simply cannot be generated any other way. Here's an example:

>>> def outer():
...   class Sentinel:
...     pass
...   def inner():
...     question = input("Answer here: ")
...     if question == "q":
...       return Sentinel()
...     return question
...   x = inner()
...   if isinstance(x, Sentinel):
...     return
...   while True:
...     print(f"do something with {x}")
...
...
>>> def menu():
...   print("hi i'm a menu")
...   running = True
...   while running:
...     choice = input("Choose: ")
...     if choice == "1":
...       outer()
...     else:
...       running = False
...
>>> menu()
hi i'm a menu
Choose: 1
Answer here: q
Choose: 1
Answer here: q
Choose: 1
Answer here: q
Choose: asdf
The other option is to include state with the function return. Something like:
>>> def outer():
...     def inner():
...         question = input("Answer: ")
...         state = {"response": question}
...         state["next"] = "return" if "q" == question else "work"
...         return state
...     x = inner()
...     if "return" == x["next"]:
...         return
...     while True:
...         print(f"do something with {x}")
...
That version can quickly get very messy if you're not careful, though, and make it very difficult to debug. But the state method is how things like level transitions work in video games.
banidjamali likes this post
Reply


Messages In This Thread
RE: exiting the outer function from the inner function - by nilamo - Feb-26-2021, 10:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas Outer merge skollu826 2 381 Apr-20-2024, 06:28 PM
Last Post: deanhystad
  The function of double underscore back and front in a class function name? Pedroski55 9 1,001 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  can Inner Class reference the Outer Class's static variable? raykuan 6 6,373 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  Exit function from nested function based on user input Turtle 5 3,163 Oct-10-2021, 12:55 AM
Last Post: Turtle
Question Stopping a parent function from a nested function? wallgraffiti 1 3,832 May-02-2021, 12:21 PM
Last Post: Gribouillis
  Return not exiting function?? rudihammad 3 5,553 Dec-01-2020, 07:11 PM
Last Post: bowlofred
  Passing argument from top-level function to embedded function JaneTan 2 2,382 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  use NULL as function parameter which is a pointer function? oyster 0 2,612 Jul-11-2020, 09:02 AM
Last Post: oyster
  Unindent does not match any outer intendation level -error MaartenRo 3 2,402 Jun-28-2020, 11:46 AM
Last Post: MaartenRo
  How to use function result in another function Ayckinn 4 3,009 Jun-16-2020, 04:50 PM
Last Post: Ayckinn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020