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 [EMAIL PROTECTED] declaimed the following in comp.lang.python: 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: exit to interpreter?
On Friday, Mar 23rd 2007 at 10:52 -0700, quoth belinda thom: =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? I was reading other people's responses. Why not simply use the python debugger? http://docs.python.org/lib/module-pdb.html Yes? No? -- Time flies like the wind. Fruit flies like a banana. Stranger things have .0. happened but none stranger than this. Does your driver's license say Organ ..0 Donor?Black holes are where God divided by zero. Listen to me! We are all- 000 individuals! What if this weren't a hypothetical question? steveo at syslang.net -- http://mail.python.org/mailman/listinfo/python-list
Re: exit to interpreter?
On Mar 23, 12:52 pm, belinda thom [EMAIL PROTECTED] wrote: 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. http://docs.python.org/lib/module-code.html ... Jay Graves -- http://mail.python.org/mailman/listinfo/python-list
Re: exit to interpreter?
On Mar 23, 12:52 pm, belinda thom [EMAIL PROTECTED] wrote: 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: exit to interpreter?
On Mar 23, 2007, at 11:04 AM, [EMAIL PROTECTED] wrote: On Mar 23, 12:52 pm, belinda thom [EMAIL PROTECTED] wrote: 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: exit to interpreter?
belinda thom wrote: On Mar 23, 2007, at 11:04 AM, [EMAIL PROTECTED] wrote: On Mar 23, 12:52 pm, belinda thom [EMAIL PROTECTED] wrote: 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: exit to interpreter?
On Mar 23, 1:20 pm, belinda thom [EMAIL PROTECTED] wrote: On Mar 23, 2007, at 11:04 AM, [EMAIL PROTECTED] wrote: On Mar 23, 12:52 pm, belinda thom [EMAIL PROTECTED] wrote: 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. -- http://mail.python.org/mailman/listinfo/python-list
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, [EMAIL PROTECTED] wrote: On Mar 23, 1:20 pm, belinda thom [EMAIL PROTECTED] wrote: On Mar 23, 2007, at 11:04 AM, [EMAIL PROTECTED] wrote: On Mar 23, 12:52 pm, belinda thom [EMAIL PROTECTED] wrote: 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. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list