[Tutor] can i run the last saved input again
Steven D'Aprano
steve at pearwood.info
Sat Jul 24 10:37:30 CEST 2010
On Sat, 24 Jul 2010 06:00:21 pm Alan Gauld wrote:
> "ANKUR AGGARWAL" <coolankur2006 at gmail.com> wrote
>
> > hey this is a crazy question but i want to know it......
> > suppose i have this code
> >
> > a=raw_input("enter the string :")
> > print a
> >
> > now i want to ask is there's any way that python remembers the
> > input i gave
> > it to last time and it just give me the output when i again run
> > python
>
> No that is impossible, python is an interpreter and if it remembered
> its input you would only ever be able to run one program. What you
> want to do is get your program to remember its input, and that is
> possible. It is very important you are clear in your mind what is
> python and what is your code.
You shouldn't have trimmed the rest of Ankur's post, because he clearly
states he's talking about his program, not Python specifically:
"now i want to ask is there's any way that python remembers the input i
gave it to last time and it just give me the output when i again run
python prog.py?????? i mean i dont need to give input again. I just
need to give input only first time and when i run the prog again it
will pick up the previous one output is it possible???"
Well, for some definition of "clearly" :)
> You cannot change much in Python(*), you can
> change anything in your code.
Actually, you can change a lot in Python, but often you shouldn't.
Any module you import, including builtins, can be monkey-patched
(modified on the fly), although that's rightfully considered to be
dangerous and best avoided except under very special circumstances.
You can't change syntax, or literals, or a handful of fixed objects like
None, but otherwise most things in Python can be modified, if you know
what you're doing and/or silly enough to modify them.
And of course when you start up Python, it reads a number of environment
variables and config files, which give you an opportunity to have
Python remember your input from last session. For example, I have a
very minimal PYTHONSTARTUP file which automatically imports os and sys.
I had also experimented with having it back-port functionality from
versions 2.6 to 2.5. Some people use it to customize things like the
prompt in the interactive interpreter, or install a tab-completion
module, provide history which survives shutting down the interpreter
(like most Linux shells already do), or otherwise provide added
functionality such as that provided by (e.g.) IPython.
http://ipython.scipy.org/moin
So there are plenty of reasons to want Python to remember your input,
and plenty of ways to do so.
--
Steven D'Aprano
More information about the Tutor
mailing list