exit to interpreter?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • belinda thom

    #1

    exit to interpreter?

    Hi,

    I'm writing a function that polls the user for keyboard input,
    looping until it has determined that the user has entered a valid
    string of characters, in which case it returns that string so it can
    be processed up the call stack. My problem is this. I'd also like it
    to handle a special string (e.g. 'quit'), in which case control
    should return to the Python command line as opposed to returning the
    string up the call stack.

    sys.exit seemed like a good choice, but it exits the python interpreter.

    I could use an exception for this purpose, but was wondering if
    there's a better way?

    --b
  • kyosohma@gmail.com

    #2
    Re: exit to interpreter?

    On Mar 23, 12:52 pm, belinda thom <b...@cs.hmc.ed uwrote:
    Hi,
    >
    I'm writing a function that polls the user for keyboard input,
    looping until it has determined that the user has entered a valid
    string of characters, in which case it returns that string so it can
    be processed up the call stack. My problem is this. I'd also like it
    to handle a special string (e.g. 'quit'), in which case control
    should return to the Python command line as opposed to returning the
    string up the call stack.
    >
    sys.exit seemed like a good choice, but it exits the python interpreter.
    >
    I could use an exception for this purpose, but was wondering if
    there's a better way?
    >
    --b
    If you're using a function, wouldn't using the keyword "return" work?

    Mike

    Comment

    • belinda thom

      #3
      Re: exit to interpreter?


      On Mar 23, 2007, at 11:04 AM, kyosohma@gmail. com wrote:
      On Mar 23, 12:52 pm, belinda thom <b...@cs.hmc.ed uwrote:
      >Hi,
      >>
      >I'm writing a function that polls the user for keyboard input,
      >looping until it has determined that the user has entered a valid
      >string of characters, in which case it returns that string so it can
      >be processed up the call stack. My problem is this. I'd also like it
      >to handle a special string (e.g. 'quit'), in which case control
      >should return to the Python command line as opposed to returning the
      >string up the call stack.
      >>
      >sys.exit seemed like a good choice, but it exits the python
      >interpreter.
      >>
      >I could use an exception for this purpose, but was wondering if
      >there's a better way?
      >>
      >--b
      >
      If you're using a function, wouldn't using the keyword "return" work?
      >
      Mike
      No, because that just returns to the caller, which is not the Python
      interpreter.

      Comment

      • Mel Wilson

        #4
        Re: exit to interpreter?

        belinda thom wrote:
        On Mar 23, 2007, at 11:04 AM, kyosohma@gmail. com wrote:
        >
        >On Mar 23, 12:52 pm, belinda thom <b...@cs.hmc.ed uwrote:
        >>Hi,
        >>>
        >>I'm writing a function that polls the user for keyboard input,
        >>looping until it has determined that the user has entered a valid
        >>string of characters, in which case it returns that string so it can
        >>be processed up the call stack. My problem is this. I'd also like it
        >>to handle a special string (e.g. 'quit'), in which case control
        >>should return to the Python command line as opposed to returning the
        >>string up the call stack.
        >>>
        >>sys.exit seemed like a good choice, but it exits the python
        >>interpreter .
        >>>
        >>I could use an exception for this purpose, but was wondering if
        >>there's a better way?
        A custom-defined exception is probably the best way to jump out of a
        stack of nested calls.


        Mel.

        Comment

        • kyosohma@gmail.com

          #5
          Re: exit to interpreter?

          On Mar 23, 1:20 pm, belinda thom <b...@cs.hmc.ed uwrote:
          On Mar 23, 2007, at 11:04 AM, kyoso...@gmail. com wrote:
          >
          >
          >
          On Mar 23, 12:52 pm, belinda thom <b...@cs.hmc.ed uwrote:
          Hi,
          >
          I'm writing a function that polls the user for keyboard input,
          looping until it has determined that the user has entered a valid
          string of characters, in which case it returns that string so it can
          be processed up the call stack. My problem is this. I'd also like it
          to handle a special string (e.g. 'quit'), in which case control
          should return to the Python command line as opposed to returning the
          string up the call stack.
          >
          sys.exit seemed like a good choice, but it exits the python
          interpreter.
          >
          I could use an exception for this purpose, but was wondering if
          there's a better way?
          >
          --b
          >
          If you're using a function, wouldn't using the keyword "return" work?
          >
          Mike
          >
          No, because that just returns to the caller, which is not the Python
          interpreter.
          Sorry...I didn't realize you were calling from another function or
          functions. Duh. I agree with the other writer. Use a custom exception.

          Comment

          • belinda thom

            #6
            Re: exit to interpreter?

            Thanks to all. I had suspected this was the best way to go, but as
            I'm fairly new to Python, it seemed worth a check.

            --b

            On Mar 23, 2007, at 12:48 PM, kyosohma@gmail. com wrote:
            On Mar 23, 1:20 pm, belinda thom <b...@cs.hmc.ed uwrote:
            >On Mar 23, 2007, at 11:04 AM, kyoso...@gmail. com wrote:
            >>
            >>
            >>
            >>On Mar 23, 12:52 pm, belinda thom <b...@cs.hmc.ed uwrote:
            >>>Hi,
            >>
            >>>I'm writing a function that polls the user for keyboard input,
            >>>looping until it has determined that the user has entered a valid
            >>>string of characters, in which case it returns that string so it
            >>>can
            >>>be processed up the call stack. My problem is this. I'd also
            >>>like it
            >>>to handle a special string (e.g. 'quit'), in which case control
            >>>should return to the Python command line as opposed to returning
            >>>the
            >>>string up the call stack.
            >>
            >>>sys.exit seemed like a good choice, but it exits the python
            >>>interprete r.
            >>
            >>>I could use an exception for this purpose, but was wondering if
            >>>there's a better way?
            >>
            >>>--b
            >>
            >>If you're using a function, wouldn't using the keyword "return"
            >>work?
            >>
            >>Mike
            >>
            >No, because that just returns to the caller, which is not the Python
            >interpreter.
            >
            Sorry...I didn't realize you were calling from another function or
            functions. Duh. I agree with the other writer. Use a custom exception.
            >
            --
            http://mail.python.org/mailman/listinfo/python-list

            Comment

            • jay graves

              #7
              Re: exit to interpreter?

              On Mar 23, 12:52 pm, belinda thom <b...@cs.hmc.ed uwrote:
              be processed up the call stack. My problem is this. I'd also like it
              to handle a special string (e.g. 'quit'), in which case control
              should return to the Python command line as opposed to returning the
              string up the call stack.
              Maybe you are looking for the 'code' module.

              Source code: Lib/code.py The code module provides facilities to implement read-eval-print loops in Python. Two classes and convenience functions are included which can be used to build applications...


              ....
              Jay Graves

              Comment

              • belinda thom

                #8
                Re: exit to interpreter?


                On Mar 24, 2007, at 4:30 AM, Dennis Lee Bieber wrote:
                On Fri, 23 Mar 2007 10:52:09 -0700, belinda thom <bthom@cs.hmc.e du>
                declaimed the following in comp.lang.pytho n:
                >
                >Hi,
                >>
                >I'm writing a function that polls the user for keyboard input,
                >looping until it has determined that the user has entered a valid
                >string of characters, in which case it returns that string so it can
                >be processed up the call stack. My problem is this. I'd also like it
                >to handle a special string (e.g. 'quit'), in which case control
                >should return to the Python command line as opposed to returning the
                >string up the call stack.
                >>
                To the Python command line? That seems to imply that you started
                Python first, then imported the module with the function and
                invoked it.
                Yup
                Surely you don't expect this to be the normal operational mode for
                the program?
                Its more for pedagogical purposes. I'm using Python in an undergrad
                prog class and I'd like students to be able to either enter one of a
                set of simple menu choices or quit to the interpreter. It should be
                command-based as opposed to GUI based. Hence the request.
                About the only way I know of to short circuit the calling stack
                requires using an exception -- and an exception handler at the top-
                level
                (which, for your stated goal, probably has a "pass" for its body,
                so the
                top-level will "exit" to the interpreter prompt).
                This is similar to Mel's advice, which was what I'd expected but
                wanted to verify.

                Thanks. For those who've been following this thread, this is the
                approach I took.

                --b

                Comment

                Working...