interactive shell -- reload definitions?

M

Metalone

I have a question about the interactive Python shell. Is it possible
to reload a file and get the new definitions.

For example, if I do
import xyz

Then I find a bug in some function in xyz.
So, I edit xyz.py

I would like to reload the definitions in xyz.py without leaving my
current shell session.
Is this possible?


Also, is there any way to save definitions created in the interactive
shell to a file?
Sometimes I write a simple function in the shell, and this wish I had
that function in an external file.

Thanks.
 
J

James Stroud

Metalone said:
I have a question about the interactive Python shell. Is it possible
to reload a file and get the new definitions.

For example, if I do
import xyz

Then I find a bug in some function in xyz.
So, I edit xyz.py

I would like to reload the definitions in xyz.py without leaving my
current shell session.
Is this possible?


Also, is there any way to save definitions created in the interactive
shell to a file?
Sometimes I write a simple function in the shell, and this wish I had
that function in an external file.

Thanks.

Quoting my python interpreter:

"""
Help on built-in function reload:

reload(...)
reload(module) -> module

Reload the module. The module must have been successfully imported
before.
"""

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
B

bruno at modulix

Metalone said:
I have a question about the interactive Python shell. Is it possible
to reload a file and get the new definitions.

For example, if I do
import xyz

Then I find a bug in some function in xyz.
So, I edit xyz.py

I would like to reload the definitions in xyz.py without leaving my
current shell session.
Is this possible?

you want reload().
Also, is there any way to save definitions created in the interactive
shell to a file?
Sometimes I write a simple function in the shell, and this wish I had
that function in an external file.

AFAIK, Idle and IPython might do this.

I personnaly uses emacs + python-mode, and it's quite powerful (let you
eval a whole buffer or part of it in an running embedded python shell,
and of course copy/paste from the shell to a buffer).
 
M

malv

This is a question that comes up almost continuously for at least six
years now.
For Python users having to deal with major real-life applications, this
may make them think twice about the future suitability of Python as a
competitive development tool.
Ruby is featuring a software modify and go feature. Lisp is, even VB
does. In the design of Smalltalk this used to be one of the major
considerations.

Plenty of posts will turn up doing a search on "reload". The following
references summarize some of these problems:
http://groups.google.com/group/comp...q=Hung+Jung+Lu+reload&rnum=2#41f57f366affd057
http://groups.google.com/group/comp...q=Hung+Jung+Lu+reload&rnum=2#41f57f366affd057

In fact, doing a reload usually will not accomplish what one is looking
for. Class instances should also be upgraded on reload(), preferably
automatically. This can be accomplished as shown by Michael Hudson in:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/160164
Variants on this theme exist which seem to be broken.

Given the persistent push of Ruby, I would strongly recommend that a
workable integrated solution will be found for Reload & Go in Python,
taking priority on many way out features of rather low practicality for
many programmers.
 
J

Jack Diederich

This is a question that comes up almost continuously for at least six
years now.
For Python users having to deal with major real-life applications, this
may make them think twice about the future suitability of Python as a
competitive development tool.
Ruby is featuring a software modify and go feature. Lisp is, even VB
does. In the design of Smalltalk this used to be one of the major
considerations.

Plenty of posts will turn up doing a search on "reload". The following
references summarize some of these problems:
http://groups.google.com/group/comp...q=Hung+Jung+Lu+reload&rnum=2#41f57f366affd057
http://groups.google.com/group/comp...q=Hung+Jung+Lu+reload&rnum=2#41f57f366affd057

In fact, doing a reload usually will not accomplish what one is looking
for. Class instances should also be upgraded on reload(), preferably
automatically. This can be accomplished as shown by Michael Hudson in:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/160164
Variants on this theme exist which seem to be broken.

reload() cannot always do exactly what you want it to do because what
you want it to do isn't always the same. Upgrading instances to the
new definition of the class may be handy sometimes but what is the
'right' thing to do here?

# test.py version 1
class A:
def __init__(self, val):
self.hello_wolrd = val
def __str__(self):
return self.hello_world

# test.py version 2
class A:
def __init__(self, val):
self.himom = val
def __str__(self):
return self.himom

reload() can never know that 'hello_world' was renamed to 'himom'
so if you upgrade the class of existing instances str(ob) will explode.
reload() is only handy for interactive use. If you try to use it in any
non-trivial production program you will get burned. It can never be
made magic enough.
Given the persistent push of Ruby, I would strongly recommend that a
workable integrated solution will be found for Reload & Go in Python,
taking priority on many way out features of rather low practicality for
many programmers.

Link? Google finds lots of unrelated stuff because reload is a common
word.

-Jack
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top