473,386 Members | 1,694 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.

Importing and namespace visibility

As an application programmer, I'm not well versed in the material
aspects of computing (memory, cpu, bus and all). My understanding of
imports in Python is such: the __main__ program is the center piece
which holds the programs reference: globals, functions, classes,
modules etc. The objects of this file (functions and classes) are
directly accessible; 'import suchModule' s objects are attainable
through the *qualified name* (module.function); the 'from suchModule
import *' the objects are directly attainable.

A recent msg from F. Lundh
http://groups.google.ca/group/comp.l...51ab24748251d8
suggested being careful with recursive importing...

BUT, of all this I thought that if you import module1, then module2
(into __main__), objects from module1 would be available to objects of
module2 which came (into memory space) after module1 was loaded. This
does not seem to be the case, and module2 requires an 'import module1'
statement in its own file to see this last module's objects. This is
not the recursive situation that was a pitfall Fredrik was evoking.
What am I missing here???

The reason I'm asking is to setup team development, using Tkinter,
where different people will be programming diverse sections to be
'packed' into the main interface...

Thanks for any help in understanding what is happening in this
situation,

JMD

Jul 19 '05 #1
4 2643
"jean-marc" wrote:
BUT, of all this I thought that if you import module1, then module2
(into __main__), objects from module1 would be available to objects of
module2 which came (into memory space) after module1 was loaded. This
does not seem to be the case, and module2 requires an 'import module1'
statement in its own file to see this last module's objects. This is
not the recursive situation that was a pitfall Fredrik was evoking.
What am I missing here???


importing something into a module only makes the names available in
that module, not in every other module. (import is not some kind of
global include). for the details, read the "There are Many Ways to
Import a Module" section under:

http://effbot.org/zone/import-confusion.htm

(and as mentioned under "What Does Python Do to Import a Module",
importing a module twice doesn't load the module again; it only makes
the name(s) available to the importing module).

reading the following sections in the language reference may also help:

http://docs.python.org/ref/naming.html
http://docs.python.org/ref/import.html

</F>

Jul 19 '05 #2
jean-marc wrote:
As an application programmer, I'm not well versed in the material
aspects of computing (memory, cpu, bus and all). My understanding of
imports in Python is such: the __main__ program is the center piece
which holds the programs reference: globals, functions, classes,
modules etc.
the '__main__' program is in fact just another Python module. What makes
it the 'main program' is the fact that you pass it to the Python
interpreter as a 'main program'.
The objects of this file (functions and classes) are
directly accessible;
from within this particular module only.
'import suchModule' s objects are attainable
through the *qualified name* (module.function); the 'from suchModule
import *' the objects are directly attainable.
Take care, this has other implications too.

(snip)
BUT, of all this I thought that if you import module1, then module2
(into __main__), objects from module1 would be available to objects of
module2 which came (into memory space) after module1 was loaded. This
does not seem to be the case,
This is not.
and module2 requires an 'import module1'
Right. And that's a Good Thing(tm).
statement in its own file to see this last module's objects. This is
not the recursive situation that was a pitfall Fredrik was evoking.
What am I missing here???


The term 'global' in Python is somewhat misleading. What it really means
is "global to the current module". Each module (and remember the 'main
program' is a module too) has it's own namespace.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 19 '05 #3
Merci Bruno, ( and also to Fredrik )

So I think I understand correctly, if I say that:
each modulkes requires its own set of reference to whatever objects it
needs to speak. The interpreter wil see not to create extra copies of
the compiled code if many modules import the same modules but will make
them all point to the one already existing (in this program's
execution).

Practically, it means every module import whatever it needs - be
careful with recursive imports - keep things tidy by using the 'import
suchModule' (and use dotted name chains to reach whatever is needed).

Again thank you,

JM
PS Nice weather in Bordeaux ? I've been there once... (when I was young
(sigh))

Jul 19 '05 #4
jean-marc wrote:
Merci Bruno, ( and also to Fredrik )

So I think I understand correctly, if I say that:
each modulkes requires its own set of reference to whatever objects it
needs to speak. The interpreter wil see not to create extra copies of
the compiled code if many modules import the same modules but will make
them all point to the one already existing (in this program's
execution).
Seems right.
Practically, it means every module import whatever it needs - be
careful with recursive imports - keep things tidy by using the 'import
suchModule' (and use dotted name chains to reach whatever is needed).
<hint>
There's a way to shortcut long.fully.dotted.Name without using the 'from
xxx import' syntax:

import long.fully.dotted
Name = long.fully.dotted.Name
</hint>

<OT> PS Nice weather in Bordeaux ?


Well... varying, as usual !-)
</OT>
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 19 '05 #5

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

Similar topics

12
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In...
1
by: Steve George | last post by:
Hi, I have a scenario where I have a master schema that defines a number of complex and simple types. I then have a number of other schemas (with different namespaces) where I would like to reuse...
8
by: ma740988 | last post by:
Consider // file fft_data.h class FFT_DATA { public: static const unsigned int Data_Words = 10; struct Data_Block_Type {
8
by: Poonam | last post by:
Hi, Can some one please help me with (or point me to) a very simple but working code sample that shows how to import XML Schema. I have tried many samples out there on internet but nothing seems...
3
by: Mark | last post by:
All, As you can see below, I have had problems with inquisitive souls looking at data they shouldn't be. I though disabling the ability to create new databases using my workgroup would stop this...
29
by: Natan | last post by:
When you create and aspx page, this is generated by default: using System; using System.Collections; using System.Collections.Specialized; using System.Configuration; using System.Text; using...
5
by: Christoffer | last post by:
Hi, I'm having trouble with namespaces when importing nodes. I'd like to get this output: <exampleRoot xmlns="http://mynamespace"> <row Lsm_Info="ABC123" /> <row Lsm_Info="DEF456" />...
6
by: gmarkowsky | last post by:
Hi all, I'm trying to import a class from a module. The class looks like this: class App: def __init__(self, master): frame = Frame(master) frame.pack()
22
by: Luna Moon | last post by:
I am reading the book "C++ Annotations", and here is a quote from the book: Namespaces can be defined without a name. Such a namespace is anonymous and it restricts the visibility of the...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.