On Sun, Oct 2, 2011 at 01:47, Steven D'Aprano <st...@pearwood.info> wrote:
> Richard D. Moores wrote:
>>
>> Python 3.2.2, Win 7
>>
>> When using the Python 3 interactive prompt, is there a way to quickly
>> "refresh" the prompt? By "refresh" I mean get a new interactive prompt
>> with nothing imported and all things like  a = "qwerty",   n = 123,
>> etc. no longer in effect. Not sure what the wording should be for
>> that. "imports cancelled and variables deleted"?
>
> That would be "start a fresh interpreter session".

Thanks.

> And the easiest way to start a fresh interpreter session is, indeed, to exit
> the current session and start a fresh one, just as you describe.
>
>
>> I know I can do that by entering a Ctrl+z, followed by entering
>> "python". But could print() be used to do the same thing? I can get a
>> beep with print("\a"), or clear the screen with   import os;
>> print(os.system("CLS"),chr(13)," ",chr(13)), but how to print a ^Z?
>
> Printing a ^Z is easy, you just have to know what character code ^Z
> corresponds to. That's chr(26) or 1A in hexadecimal, so this works:
>
>>>> print('\x1A')
> ▒
>
>
> But as you can see, that just prints a character.
>
> You need to get Ctrl-Z into the *input* stream, not the *output* stream, to
> exit the interpreter. If you are doing it manually, the easiest way is just
> to type Ctrl-Z. If doing it programmatically, use either of these:
>
> sys.exit()
> raise SystemExit
>
>
> There is no easy way to reset the current interpreter session to "as new". I
> suppose you could try something like this:
>
>
> sys.modules[:] = []
> globals().clear()
>
> but that won't necessarily reset everything to a fresh state.

Actually, my goal was to create an Active Word
(<http://www.activewords.com/>) that would do the job, and I needed to
know how to close the interactive prompt as the first step in that AW.
I should have thought of sys.exit(), but I didn't.

This is OT, but here's the Active Word script that  what I want  -- to
close the current interpreter session and start a fresh one: import
sys<ENTER>sys.exit()<Enter>python<Enter>. The Active Word I chose is
"fresh". Here it is in action:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Windows\System32>python
Python 3.2.2 (default, Sep  4 2011, 09:07:29) [MSC v.1500 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1234
>>> from math import sqrt
>>> sqrt(a)
35.12833614050059  (right her is where I called my AW script, "fresh")
>>> import sys
>>> sys.exit()

C:\Windows\System32>python
Python 3.2.2 (default, Sep  4 2011, 09:07:29) [MSC v.1500 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> sqrt(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sqrt' is not defined
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>>

Thanks, Steven!

Dick
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to