473,503 Members | 11,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to clean a module?

ai
It assumes that there is a module A which have two global variables X
and Y. If I run "import A" in the IDLE shell, then I can use A.X and
A.Y correctly. But if I want to change the module A and then delete
the variable Y, I find I can use A.Y just the same as before!
In fact, I have tried all the following methods but can't remove the
A.Y:
execute "import A" again
"reload(A)"
"del A; import A"
Yes, if you use "del A.Y", it works. But it is stupid since there are
probably many names. In my thought, if no one references objects in A,
"del A" will release all memory about A. But it seems that the fact is
not. So I can not refresh the namespace to follow changes of a module
easily and I will worry about the memory if I del a module.
I want to know if there is a way to clear a module entirely.

May 31 '07 #1
6 1713
ai schrieb:
It assumes that there is a module A which have two global variables X
and Y. If I run "import A" in the IDLE shell, then I can use A.X and
A.Y correctly. But if I want to change the module A and then delete
the variable Y, I find I can use A.Y just the same as before!
In fact, I have tried all the following methods but can't remove the
A.Y:
execute "import A" again
"reload(A)"
"del A; import A"
Yes, if you use "del A.Y", it works. But it is stupid since there are
probably many names. In my thought, if no one references objects in A,
"del A" will release all memory about A. But it seems that the fact is
not. So I can not refresh the namespace to follow changes of a module
easily and I will worry about the memory if I del a module.
I want to know if there is a way to clear a module entirely.
There might be other answers - but the easiest and IMHO best is to
simply restart the interpreter. Because whatever you type in there, you
could or should even (if it reaches some complexity) put in a small test
script - and execute that from the interpreter at a shell prompt. The
advantage is that you don't suffer from any side-effects e.g. IDLE has
(no Tk mainloop for example) and avoid the problems you describe
entirely. Together with a bunch of others.

If you want/have to, you can drop into interpreter mode after script
execution with

python -i myscript.py
Diez
May 31 '07 #2
ai a écrit :
It assumes that there is a module A which have two global variables X
and Y. If I run "import A" in the IDLE shell, then I can use A.X and
A.Y correctly. But if I want to change the module A and then delete
the variable Y, I find I can use A.Y just the same as before!
It's unlikely to be true, see below.
In fact, I have tried all the following methods but can't remove the
A.Y:
execute "import A" again
"reload(A)"
"del A; import A"
Yes, if you use "del A.Y", it works. But it is stupid since there are
probably many names. In my thought, if no one references objects in A,
"del A" will release all memory about A. But it seems that the fact is
not. So I can not refresh the namespace to follow changes of a module
easily and I will worry about the memory if I del a module.
I want to know if there is a way to clear a module entirely.
Actually I do not see your problem and your exact need, when I type the
following in python prompt I just see expected behavior, what is a
problem to you ? Maybe you could post a code explaining it.
In [64]: import a

In [65]: a.X

Out[65]: 0

In [66]: a.X = 2

In [67]: del a

In [68]: import a as b

In [69]: b.X

Out[69]: 2

In [71]: for name in [ n for n in b.__dict__ if not n.startswith('__') ]
:
....: b.__dict__.__delitem__(name)

....:

....:

In [72]: b.X

---------------------------------------------------------------------------

<type 'exceptions.AttributeError' Traceback (most recent call
last)
C:\Documents and Settings\maric\Bureau\<ipython consolein <module>()

<type 'exceptions.AttributeError'>: 'module' object has no attribute 'X'

In [73]: reload(b)

Out[73]: <module 'a' from 'a.pyc'>

In [74]: b.X

Out[74]: 0
In [75]: del b.X

In [76]: del b

In [77]: import a

In [78]: a.b

---------------------------------------------------------------------------

<type 'exceptions.AttributeError' Traceback (most recent call
last)
C:\Documents and Settings\maric\Bureau\<ipython consolein <module>()

<type 'exceptions.AttributeError'>: 'module' object has no attribute 'b'

May 31 '07 #3
ai
Yes, you are right.
But from this problem, could I infer that the statement "del xxx"
doesn't release the memory which xxx used?
On May 31, 11:21 pm, "Diez B. Roggisch" <d...@nospam.web.dewrote:
ai schrieb:
It assumes that there is a module A which have two global variables X
and Y. If I run "import A" in the IDLE shell, then I can use A.X and
A.Y correctly. But if I want to change the module A and then delete
the variable Y, I find I can use A.Y just the same as before!
In fact, I have tried all the following methods but can't remove the
A.Y:
execute "import A" again
"reload(A)"
"del A; import A"
Yes, if you use "del A.Y", it works. But it is stupid since there are
probably many names. In my thought, if no one references objects in A,
"del A" will release all memory about A. But it seems that the fact is
not. So I can not refresh the namespace to follow changes of a module
easily and I will worry about the memory if I del a module.
I want to know if there is a way to clear a module entirely.

There might be other answers - but the easiest and IMHO best is to
simply restart the interpreter. Because whatever you type in there, you
could or should even (if it reaches some complexity) put in a small test
script - and execute that from the interpreter at a shell prompt. The
advantage is that you don't suffer from any side-effects e.g. IDLE has
(no Tk mainloop for example) and avoid the problems you describe
entirely. Together with a bunch of others.

If you want/have to, you can drop into interpreter mode after script
execution with

python -i myscript.py

Diez

Jun 1 '07 #4
ai
Perhaps you misundstand me. I means if you reedit a module file and
reload it, the interpreter doesn't follow the change you have made
exactly.
For example, you import a module, edit the module file (you may
remove a global variable or change its name), save the change, reload
the module (or del the module and import it again). At last, you will
find the origin variable still exists in the interpreter.
If you don't notice this, you may meet some strange problems when you
do refacting.
On Jun 1, 12:17 am, Maric Michaud <m...@aristote.infowrote:
ai a écrit :
It assumes that there is a module A which have two global variables X
and Y. If I run "import A" in the IDLE shell, then I can use A.X and
A.Y correctly. But if I want to change the module A and then delete
the variable Y, I find I can use A.Y just the same as before!

It's unlikely to be true, see below.
In fact, I have tried all the following methods but can't remove the
A.Y:
execute "import A" again
"reload(A)"
"del A; import A"
Yes, if you use "del A.Y", it works. But it is stupid since there are
probably many names. In my thought, if no one references objects in A,
"del A" will release all memory about A. But it seems that the fact is
not. So I can not refresh the namespace to follow changes of a module
easily and I will worry about the memory if I del a module.
I want to know if there is a way to clear a module entirely.

Actually I do not see your problem and your exact need, when I type the
following in python prompt I just see expected behavior, what is a
problem to you ? Maybe you could post a code explaining it.

In [64]: import a

In [65]: a.X

Out[65]: 0

In [66]: a.X = 2

In [67]: del a

In [68]: import a as b

In [69]: b.X

Out[69]: 2

In [71]: for name in [ n for n in b.__dict__ if not n.startswith('__') ]
:
....: b.__dict__.__delitem__(name)

....:

....:

In [72]: b.X

---------------------------------------------------------------------------

<type 'exceptions.AttributeError' Traceback (most recent call
last)

C:\Documents and Settings\maric\Bureau\<ipython consolein <module>()

<type 'exceptions.AttributeError'>: 'module' object has no attribute 'X'

In [73]: reload(b)

Out[73]: <module 'a' from 'a.pyc'>

In [74]: b.X

Out[74]: 0

In [75]: del b.X

In [76]: del b

In [77]: import a

In [78]: a.b

---------------------------------------------------------------------------

<type 'exceptions.AttributeError' Traceback (most recent call
last)

C:\Documents and Settings\maric\Bureau\<ipython consolein <module>()

<type 'exceptions.AttributeError'>: 'module' object has no attribute 'b'

Jun 1 '07 #5
ai wrote:
Yes, you are right.
But from this problem, could I infer that the statement "del xxx"
doesn't release the memory which xxx used?
It just removes the name xxx from the current scope - which will result in a
reference counter decrease. If that was the last reference, the object will
be destroyed. Most times. There are some special cases involving the usage
of __del__-methods on objects.

Diez
Jun 1 '07 #6
ai
thx

On Jun 1, 6:50 pm, "Diez B. Roggisch" <d...@nospam.web.dewrote:
ai wrote:
Yes, you are right.
But from this problem, could I infer that the statement "del xxx"
doesn't release the memory which xxx used?

It just removes the name xxx from the current scope - which will result in a
reference counter decrease. If that was the last reference, the object will
be destroyed. Most times. There are some special cases involving the usage
of __del__-methods on objects.

Diez

Jun 1 '07 #7

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

Similar topics

1
1518
by: Eric Texier | last post by:
On linux, I am parsing the output of an executable that's returning color and font information, thing like \x1b[1m\x1b[31m. It's there a module that will help me clean up those control, or do I...
5
41176
by: Rafal Kleger-Rudomin | last post by:
Hello, I'm looking for a command to reset interpreter's environment i.e. unload all modules, delete variables etc. Regards, Rafal
0
1452
by: Steve Jorgensen | last post by:
Hi all, I had just participating in a small thread here about when to set recordset variables to Nothing when I ran into an interesting case in my own code that highlighted the need for a...
14
1364
by: Jack | last post by:
I like Python but I don't really like Python's installation. With PHP, I only need one file (on Linux) and maybe two files on Windows then I can run my PHP script. This means no installation is...
2
4549
by: Ronald | last post by:
I just started with dotnetnuke, and with a wrong login (wrong password on a clean install i can crash the application pool from IIS. (when i login with the right username/password information the...
7
12673
by: siggi | last post by:
Hi all, when I do >>>sys.path in IDLE (winXP), i get a horrendously long list of paths, paths I may have used during a lot of trials and errors. How can I clean up sys.path? I mean, trim it of...
232
13065
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
3
1722
by: Starke | last post by:
I am using bar tender hooked up to a MS Access DB to print labels. I have a problem with the Access table. The problem is that the "Ship to" field is written like this: Warehouse "adidas,...
9
2170
by: William Manley | last post by:
Hello. I've been using the ConfigParser module, which is a great module, gets the job done, but when it creates INI files its hard to read them. Heres what I mean, currently, when written to, the...
0
7364
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...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.