473,796 Members | 2,603 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 1723
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__.__de litem__(name)

....:

....:

In [72]: b.X

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

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

<type 'exceptions.Att ributeError'>: '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.Att ributeError' Traceback (most recent call
last)
C:\Documents and Settings\maric\ Bureau\<ipython consolein <module>()

<type 'exceptions.Att ributeError'>: '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.we b.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__.__de litem__(name)

....:

....:

In [72]: b.X

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

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

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

<type 'exceptions.Att ributeError'>: '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.Att ributeError' Traceback (most recent call
last)

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

<type 'exceptions.Att ributeError'>: '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.we b.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
1526
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 have to do it with regular expression. Thanks for your help
5
41291
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
1461
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 formal, centralized function to deal with this. The case that came up was that I have a class module that handles 2 recordsets that it must clean up at termination, but the termination should not assume that the recordsets have necessarily been...
14
1389
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 required in most cases. I just copy the file and I get PHP. Extending PHP is fairly easy, too, just by copying a few DLLs and modifying the php.ini a bit. With Python, things are really messy. I have to run the installer to install dozens of...
2
4565
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 site works fine, i can do anything it is supposed to do) I have the following setup: -Windows 2003 x64 (fully patched, clean install) -Dotnetframework 2.0.50727 -Dotnetnuke 4.3.1
7
12922
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 unnecessary paths? So far, I know only the command >>>sys.path.append(r'c:....etc...'), but how to delete or insert at the beginning of the list, I know not. Thanks,
232
13360
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 set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
3
1735
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, Ltd." /142181, MOSKOVSKAYA OBLAST,/KLIMOVSK, BEREGKOVSKYI/PROEZD 17-A 38290 I need it formated like this: Warehouse "adidas, Ltd."
9
2180
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 INI files look something like this: something = somethingelse 5 = True 1 = false 3 = False
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10242
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6800
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2931
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.