472,364 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Re: Another (perhaps similar) import question

Dan Yamins wrote:
I also have noticed another (to me) strange thing about module
imports. If anyone could explain this to me, that would be great (I
apologize if it's too elementary for this list.)

Suppose I have a module

#file: testmodule.py
a = 1
When importing this module, obviously 'a' becomes an attribute of
>>import testmodule
['__builtins__', '__doc__', '__file__', '__name__', 'a']
Now, supposed I modify the file to:

#file: testmodule.py
A = 1

and then reload:
<module 'testmodule' from 'testmodule.py'>

Now, the reported attributes still include the old 'a':
['A', '__builtins__', '__doc__', '__file__', '__name__', 'a']

Why does this happen?
Because loading (and reloading) assigns values to variables (called
binding a value in Python), but does not go on a hunt to find variables
to *unbind*. Once a variable is bound to a value, it stays bound until
something unbinds or rebinds it, and module loading does no such thing.

Moreover, even if I delete the module from memory and then reload, I
_still_ get the old attribute 'a':
But in fact you did *not* delete the module from memory. A module
import goes through two phases, only one of which you have access to...

Phase 1. Read/parse/execute each statement, and keep the resulting
namespace internally adn hidden from the user. (This is only done once.)

Phase 2. Bind the module to a local name. (This may be done many times)

import xyz
may or may not cause module xyz to be read/parsed/run (depending of
whether it's the first time imported or not), and then it binds the
resulting module namespace to the variable xyz. Your del of the module
just removed the one reference to it, but that's all.
>>del testmodule
>>import testmodule
['A', '__builtins__', '__doc__', '__file__', '__name__', 'a']

What is the principle behind this? And, is there some simple way
(other than restarting the interpreter) of "reloading" that wipes out
the old attributes associated with a given name so that spurious
attributes do not remain?

Conclusion: Don't use reload (ever). A dozen years of Python
programming, and I've never used it even once. If there is a good use
case for reload, you are probably years from being there.
Thanks again (and apologies of this is a stupid question)
Not stupid. It will all start making sense soon.

Gary Herron


Jun 27 '08 #1
0 905

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

Similar topics

by: Stian Søiland | last post by:
all examples performed with: Python 2.3+ (#2, Aug 10 2003, 11:09:33) on linux2 (2, 3, 0, 'final', 1) This is a recursive import:
by: Ryan Spencer | last post by:
Hello Everyone, I want to have a row of periods, separated by small, say, .5 second intervals between each other. Thus, for example, making it have the appearance of a progress "bar". ...
by: Doug Baroter | last post by:
Hi, One of my clients has the following situation. They use Access DB for data update etc. some business functions while they also want to view the Access data quickly and more efficiently in...
by: MLH | last post by:
I created a database and imported a report from another mdb file. In the first database, I had created a custom toolbar & invoked it with the following line... DoCmd ShowToolbar "Custom...
by: Mark Reed | last post by:
Hi all, I have been asked to look at an existing Access 2002 DB tasked with adding some more features and improving the speed at which it runs. It is a split fe/be application with around 30,000...
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
by: Lee Harr | last post by:
I understand how to create a property like this: class RC(object): def _set_pwm(self, v): self._pwm01 = v % 256 def _get_pwm(self): return self._pwm01 pwm01 = property(_get_pwm, _set_pwm)
by: beginner | last post by:
Hi All, This is just a very simple question about a python trick. In perl, I can write __END__ in a file and the perl interpreter will ignore everything below that line. This is very handy...
by: Gary Herron | last post by:
Dan Yamins wrote: Please keep responses to python-list discussion on python-list, not my personal mail box. -- Thanks. I'd suggest using modules for your system's code, and exec (or...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...

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.