473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reload() Confusion

I'm going to be teaching EEs some basic Python using the first few
chapters of Learning Python, 2nd ed. by Mark Lutz. The discussion on
Reloading Modules starting on page 266 is confusing and I believe
incorrect. On page 266 it says that a reload "changes the existing
module object in place." That's a little vague, but on page 267 it
says "every reference to a module object anywhere in your program is
automatically affected by a reload."

It is my understanding that the reloaded objects become *new* objects
in memory, available only via a fully-qualified reference to the new
module. The old objects remain in memory until the last reference to
them is gone.

I've re-written the section on Reload Basics, and I would like to get
some comments, both on correctness and clarity.

http://ece.arizona.edu/~edatools/Python/Reload.htm

Your help will be appreciated, both by me and by the students
struggling through this the first time.

-- Dave

Jul 18 '05 #1
4 1793
In article <u4********************************@4ax.com>,
David MacQuigg <dm*@gain.com> wrote:
I'm going to be teaching EEs some basic Python using the first few
chapters of Learning Python, 2nd ed. by Mark Lutz. The discussion on
Reloading Modules starting on page 266 is confusing and I believe
incorrect. On page 266 it says that a reload "changes the existing
module object in place." That's a little vague, but on page 267 it
says "every reference to a module object anywhere in your program is
automatically affected by a reload."

It is my understanding that the reloaded objects become *new* objects
in memory, available only via a fully-qualified reference to the new
module. The old objects remain in memory until the last reference to
them is gone.
It seems to me that the module keeps its identity over
the reload:

Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import re
id(re) 9940176 reload(re) <module 're' from 'E:\bin\Python23\lib\re.py'> id(re) 9940176


I've re-written the section on Reload Basics, and I would like to get
some comments, both on correctness and clarity.

http://ece.arizona.edu/~edatools/Python/Reload.htm

Your help will be appreciated, both by me and by the students
struggling through this the first time.

I agree with you. It seems that the module code is
re-executed in the previously-existing modules namespace.
Wherever that code created new objects the first time, it
will create brand-new objects the second time. Small
integers and interned strings will keep their identities (of
course), as will objects referenced through other modules.
Nothing is explicitely deleted, so bindings can be left over
from the modules prior incarnation.
No other namespaces will be affected except when code in the
module explicitely calls for it.

For what it's worth, the following code:

#=======================
"""M1 version 1"""
a=1
b=2
c=[1]
d=[2]

#=======================
"""M1 version 2"""
a=1
b=4
c=[3]
e=[4]

#=======================
"""test_reload.py"""
import os, shutil
def module_contents (M):
print "Doc: ", M.__doc__
for k, v in M.__dict__.items():
if not k.startswith ("__"):
print "%s:\t%d\t%s" % (k, id(v), v)

def grab_contents (M):
d = {}
for k, v in M.__dict__.items():
if not k.startswith ("__"):
d[k] = v
return d

#os.remove ("M1.pyc")
shutil.copyfile ("M1_1.py", "M1.py")
import M1
module_contents (M1)
mc = grab_contents (M1)

print
os.remove ("M1.pyc")
shutil.copyfile ("M1_2.py", "M1.py")
reload (M1)
module_contents (M1)

print '\nMC:'
for k, v in mc.items():
print "%s:\t%d\t%s" % (k, id(v), v)


Which gives

F:\home\mwilson\Projects\python\testscript>python test_reload.py
Doc: M1 version 1
a: 8463376 1
c: 9940432 [1]
b: 8470496 2
d: 9940368 [2]

Doc: M1 version 2
a: 8463376 1
c: 9940272 [3]
b: 8468480 4
e: 9937584 [4]
d: 9940368 [2]

MC:
a: 8463376 1
c: 9940432 [1]
b: 8470496 2
d: 9940368 [2]
The second os.remove seems to be required in case the reloaded
version of M1.py is older than the imported one.

Regards. Mel.
Jul 18 '05 #2
It is my understanding that the reloaded objects become *new* objects
in memory, available only via a fully-qualified reference to the new
module. The old objects remain in memory until the last reference to
them is gone.


Mel> It seems to me that the module keeps its identity over the reload:

Yup. The module remains the same, but its __dict__ is repopulated.

Skip

Jul 18 '05 #3
I like this code example because it gets the student to think about
how modules are kept in memory. Showing the module's dictionary
before and after a reload is great. I'll try to integrate it into my
presentation, perhaps in place of the second example, which can now
become an exercise. Thanks.

-- Dave

On Sat, 20 Mar 2004 17:55:17 -0500, mw*****@the-wire.com (Mel Wilson)
wrote:
For what it's worth, the following code:

#=======================
"""M1 version 1"""
a=1
b=2
c=[1]
d=[2]

#=======================
"""M1 version 2"""
a=1
b=4
c=[3]
e=[4]

#=======================
"""test_reload.py"""
import os, shutil
def module_contents (M):
print "Doc: ", M.__doc__
for k, v in M.__dict__.items():
if not k.startswith ("__"):
print "%s:\t%d\t%s" % (k, id(v), v)

def grab_contents (M):
d = {}
for k, v in M.__dict__.items():
if not k.startswith ("__"):
d[k] = v
return d

#os.remove ("M1.pyc")
shutil.copyfile ("M1_1.py", "M1.py")
import M1
module_contents (M1)
mc = grab_contents (M1)

print
os.remove ("M1.pyc")
shutil.copyfile ("M1_2.py", "M1.py")
reload (M1)
module_contents (M1)

print '\nMC:'
for k, v in mc.items():
print "%s:\t%d\t%s" % (k, id(v), v)


Which gives

F:\home\mwilson\Projects\python\testscript>pyth on test_reload.py
Doc: M1 version 1
a: 8463376 1
c: 9940432 [1]
b: 8470496 2
d: 9940368 [2]

Doc: M1 version 2
a: 8463376 1
c: 9940272 [3]
b: 8468480 4
e: 9937584 [4]
d: 9940368 [2]

MC:
a: 8463376 1
c: 9940432 [1]
b: 8470496 2
d: 9940368 [2]
The second os.remove seems to be required in case the reloaded
version of M1.py is older than the imported one.

Regards. Mel.


Jul 18 '05 #4
In article <g2********************************@4ax.com>,
David MacQuigg <dm*@gain.com> wrote:
I like this code example because it gets the student to think about
how modules are kept in memory. Showing the module's dictionary
before and after a reload is great. I'll try to integrate it into my
presentation, perhaps in place of the second example, which can now
become an exercise. Thanks.


Use it in good health.

Mel.
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
by: Ellinghaus, Lance | last post by:
> > Other surprises: Deprecating reload() >Reload doesn't work the way most people think >it does: if you've got any references to the old module, >they stay around. They aren't replaced. ...
1
by: Emmanuel | last post by:
Hi, I use a 'reload all' feature in my app, that allow to reload every module. I first try this version : import sys def Reload():
19
by: Darren | last post by:
I have a page that opens a popup window and within the window, some databse info is submitted and the window closes. It then refreshes the original window using window.opener.location.reload(). ...
4
by: Lonnie Princehouse | last post by:
So, it turns out that reload() fails if the module being reloaded isn't in sys.path. Maybe it could fall back to module.__file__ if the module isn't found in sys.path?? .... or reload could...
3
by: John Salerno | last post by:
I understand that after you import something once, you can reload it to pick up new changes. But does reload work with from statements? I tried this: from X import * and then did my testing....
8
by: T. Wintershoven | last post by:
Hi all, Is there a simple way in php to reload a page coded within an if statement.(see code below) It's very important that the session stays intact. The filename is RCStudent.php ...
2
by: aurekha | last post by:
Hi all, I am in confusion that, What is the difference between postgres server restarting and postgres server reloading Any Explanation will be helpful for me...
0
by: Rafe | last post by:
Hi, This seems to be an old question, and I've read back a bit, but rather than assume the answer is "you can't do that", I'd thought I'd post my version of the question along with a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.