472,961 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 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 2619
"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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.