Modules, namespaces, parents, and import

D

Dan Williams

Hi people

I've just joined the Python list and although I'm sure my question must be
asked fairly frequently, I have been unable to find an answer after two days
of searching. For the record, my total Python experience is also two days :)
(I come from a C++ background though, so I have good programming knowledge
in general).

My question is thus: how do I reference a parent module and its children?

For instance:

main.py
-------
import sys, module1
a = 5
module1.DoStuff()

module1.py
----------
def DoStuff():
print a
sys.exit(0)

OK, so of course everyone is going to go "ahhhh, that won't work!" ...well,
of course not, but you can see what my intention is. The first problem is
referencing the sys module - the only way I have managed to make it work is
to import sys in module1, which is inefficient and impractical (to say
nothing of leading to awful disorganisation!). So... how do I refer to the
sys module from inside module1, when sys has been loaded inside the main
module's namespace? (e.g. parent.sys.exit(), self.parent.sys.exit(), etc.)
I'm looking for some kind of reference to parent here, and although I have
found some stuff about __dict__ and it's relatives (and some stuff about
____ but that seems discontinued) I cannot find how to do this.

So, the next thing is the variables that belong to main.py, namely a in this
case. Assuming that some kind and helpful soul points me in the right
direction by giving me a way to reference the parent namespace, can I then
access member variables in the way that seems logical...? e.g. parent.a or
self.parent.a or whatever kind of structure the referencing follows. (Yup
this one should be self-explanatory once I know how to reference the parent
module, but I just want to check :) ).

Finally, if you can see any obvious flaws with the way I am thinking about
modules, please point them out to me. I'm used to C++ headers and externs,
and PHP includes, but Python modules and packages are now to me. That said,
the basic theory seems straightforward and so long as I can reference
parents, I like the namespace implementation.

Cheers

Dan
 
D

Dang Griffith

My question is thus: how do I reference a parent module and its children? ....
Finally, if you can see any obvious flaws with the way I am thinking about
modules, please point them out to me. I'm used to C++ headers and externs,
and PHP includes, but Python modules and packages are now to me. That said,
the basic theory seems straightforward and so long as I can reference
parents, I like the namespace implementation.

The flaw in your thinking is the thought that there is a parent
module. When you have multiple C/C++ files, they are not
parent/child. They are simply separate, perhaps sharing common
headers. Just as in C++, one module doesn't know what other module
(or modules!) has included it.

To have a called method reference variables declared in its caller's
namespace, pass parameters instead:

------------------
# modulea
import moduleb
a = 5
moduleb.DoStuff(a)
------------------
# moduleb
import sys
def DoStuff(x):
print x
sys.exit(0)
------------------

The import statement is not at all like the C/C++ #include
preprocessor directive. I'm not familiar with C++ namespaces (I
dropped out of C++ before they were added to the language), and can't
say if/how the Python import statement compares with them.

If you're looking for a way to avoid passing parameters, you can
create a third module that is used to hold your shared/global
variables:

-------------
# moda
import modb, modc

modc.variable = 27
modb.DoStuff()
-------------
# modb
import modc

def DoStuff():
print modc.variable
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

How to use variables across modules 2
Adding modules to library? / package? 1
Import redirects 0
Question about import hooks 0
python import error 7
staticmethod and namespaces 12
Can't import modules 18
import problem 5

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top