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
#1
Question 
Hello everyone.

In the code below I have simplified a result I'm trying to achieve.
I want to know how I can exit the outer function when the inner function returns a specific choice:

def outer_function():

    def inner_function():

        question = input('Answer here: ')
        if question == 'q':
            return menu()
        else:
            return question

    # if the return from the inner_function is 'menu()',
    # then I want the outer function to terminate as well
    # and not continue

    x = inner_function()
    while True:

        print(f'Here we evaluate a condition based on the {x} that is returned')

        return f'something related to {x}'


def menu():
    print('This is menu.')

    choice = input('Choose: ')
    if choice == '1':
        return outer_function()
    else:
        exit()
Thanks in advance.
Reply
#2
You have a few things going on that are unusual.

menu calls outer, outer calls inner, and inner calls menu. That's a recipe for an infinite recursion. I don't think it's needed. Perhaps if you had more specifics about what the functions should do, we could have some suggestions. Right now it seems to be a control flow example, but the control flow looks odd.

Also, there's no reason to have the inner function defined inside the outer. It doesn't reference any variables in the outer function, so better would be to have them independent. That doesn't prevent them from calling each other.
buran, ndc85430, banidjamali like this post
Reply
#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
#4
(Feb-26-2021, 10:14 PM)nilamo Wrote: x = inner()...     if "return" == x["next"]:...         return
Thank you. This helped.
I also did some tweaking to deal with the recursion issue.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 623 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  can Inner Class reference the Outer Class's static variable? raykuan 6 5,881 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  Exit function from nested function based on user input Turtle 5 2,895 Oct-10-2021, 12:55 AM
Last Post: Turtle
Question Stopping a parent function from a nested function? wallgraffiti 1 3,667 May-02-2021, 12:21 PM
Last Post: Gribouillis
  Return not exiting function?? rudihammad 3 5,277 Dec-01-2020, 07:11 PM
Last Post: bowlofred
  Passing argument from top-level function to embedded function JaneTan 2 2,239 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  use NULL as function parameter which is a pointer function? oyster 0 2,502 Jul-11-2020, 09:02 AM
Last Post: oyster
  Unindent does not match any outer intendation level -error MaartenRo 3 2,272 Jun-28-2020, 11:46 AM
Last Post: MaartenRo
  How to use function result in another function Ayckinn 4 2,815 Jun-16-2020, 04:50 PM
Last Post: Ayckinn
  HELP! unindent does not match any outer indentation level blackjesus24 2 1,941 Jan-29-2020, 08:00 AM
Last Post: blackjesus24

Forum Jump:

User Panel Messages

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