473,396 Members | 1,812 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,396 software developers and data experts.

opposite of import

Hi
I am new to python. I wanted to know if there is an opposite of "import"

Aug 3 '06 #1
9 9125
pr**************@gmail.com <pr**************@gmail.comwrote:
I am new to python. I wanted to know if there is an opposite of "import"
What do you mean? What are you trying to accomplish?

--
Lawrence - http://www.oluyede.org/blog
"Nothing is more dangerous than an idea
if it's the only one you have" - E. A. Chartier
Aug 3 '06 #2
pr**************@gmail.com wrote:
Hi
I am new to python. I wanted to know if there is an opposite of "import"
If you mean 'import' adds something, so you ask how to get rid of
something? Here you are:

Look at the 'del' statement if it is what you are looking for.

Claudio Grondi
Aug 3 '06 #3
On 3 Aug 2006 04:50:35 -0700, pr**************@gmail.com
<pr**************@gmail.comwrote:
Hi
I am new to python. I wanted to know if there is an opposite of "import"
Err, depends upon what you mean by opposite.
If you want to remove the module from a namespace into which you
imported it, you can do that with del:

import amodule
amodule.afunction() # Works fine

del amodule
amodule.afunction() # Will die now
If you want to "de-import" a module in order that you can import a
new, updated version, you can do that (with caveats) with the reload
built-in function. Check out the docs.
If you want a module to be able to specify what happens when it is
imported elsewhere, well, you have some measure of control there, too.
All the top level code will be executed (other than that in a "if
n__name == '__main__'" block).

If the import is of the "import spam" form, all names in the imported
namespace will be available in the importing module.

If the import os of the "from spam import *" form, only *public* names
in the module namespace are made available to the importing module.
Public names are those defined in a top level __all__ list, or not
starting with an underscore of no such list has been defined.
And if none of those are what you meant by the opposite of an import,
you'll need to be more explicit. ;-)

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Aug 3 '06 #4
On 8/3/06, Simon Brunning <si***@brunningonline.netwrote:
If you want to remove the module from a namespace into which you
imported it, you can do that with del:

import amodule
amodule.afunction() # Works fine

del amodule
amodule.afunction() # Will die now
Note that this doesn't get rid of a module entirely. Python will still
holds on to the module, and if you just import it again at this point,
it won't be re-executed - you'll just get another reference to the
original module.

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Aug 3 '06 #5
On 2006-08-03 09:26:54, Simon Brunning wrote:
>import amodule
amodule.afunction() # Works fine

del amodule
amodule.afunction() # Will die now

Note that this doesn't get rid of a module entirely. Python will still
holds on to the module, and if you just import it again at this point,
it won't be re-executed - you'll just get another reference to the
original module.
Is that guaranteed, or is that just until the garbage collector has removed
the module (at some arbitrary point)?

Gerhard

Aug 3 '06 #6
On 8/3/06, Gerhard Fiedler <ge*****@gmail.comwrote:
Is that guaranteed, or is that just until the garbage collector has removed
the module (at some arbitrary point)?
I *think* it's guaranteed.

It's not a matter for the garbage collector. GC only exists to remove
cyclic references. Ordinary reference counting *would* remove the
module as soon as you de referenced it, but for the fact that Python
stashes a reference to the module in (IIRC) sys.__modules__. And you
mess with *that* at your peril. ;-)

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Aug 3 '06 #7
Gerhard Fiedler wrote:
On 2006-08-03 09:26:54, Simon Brunning wrote:
>>import amodule
amodule.afunction() # Works fine

del amodule
amodule.afunction() # Will die now

Note that this doesn't get rid of a module entirely. Python will
still holds on to the module, and if you just import it again at this
point, it won't be re-executed - you'll just get another reference to
the original module.

Is that guaranteed, or is that just until the garbage collector has
removed the module (at some arbitrary point)?
It is guaranteed that simply deleting the module from the module which
imported it won't be sufficient to throw it away as other references to the
module will remain (in particular the list of modules used by the import
mechanism to ensure you get the same module if you re-import it again in
the future).

Aug 3 '06 #8
Simon Brunning wrote:
but for the fact that Python
stashes a reference to the module in (IIRC) sys.__modules__. And you
mess with *that* at your peril. ;-)
According to Python in a Nutshell, references are stored in the
dictionary sys.modules, but I'm not sure if it matters that it's not
__modules__ instead (unless that also exists).
Aug 3 '06 #9
On 8/3/06, John Salerno <jo******@nospamgmail.comwrote:
According to Python in a Nutshell, references are stored in the
dictionary sys.modules, but I'm not sure if it matters that it's not
__modules__ instead (unless that also exists).
Right you are - it's sys.modules, not sys.__modules__.

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Aug 3 '06 #10

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

Similar topics

4
by: comp.lang.php | last post by:
Hi. Relative php newbie but longtime coder in other langs. Environment: PHP4. Looking for a language construct or function that gives the opposite of array(), example: function xyz($fmt,...
6
by: Tertius | last post by:
Is there a method to create a dict from a list of keys and a list of values ? TIA Tertius
22
by: achrist | last post by:
The yield statement looks to be a big step toward some kind of lightweight concurrency-oriented programming in python. Is there any similarly nice way to do the opposite of yield, just sit around...
0
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:
15
by: Greg Schmidt | last post by:
We all know that one trick in dealing with old browsers is to add extra bits of content with class="old", where old is defined as display:none in a style sheet that is @imported so that old...
5
by: Tony | last post by:
Hi, I am looking for the opposite of wm_close, I assume it's possibly wm_open but no info comes up with that term. so, what is the opposite of WM_Close Any ideas welcome Tony
11
by: igor.tatarinov | last post by:
Given a bunch of arrays, if I want to create tuples, there is zip(arrays). What if I want to do the opposite: break a tuple up and append the values to given arrays: map(append, arrays, tupl)...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...

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.