473,606 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Modules, namespaces, parents, and import

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

Jul 18 '05 #1
1 2076
On Wed, 4 Feb 2004 06:28:38 -0000, "Dan Williams" <da*@ithium.net >
wrote:
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
-------------
# modc

variable = 0
-------------

I'm not recommending this, just mentioning it as a possibility.

--dang
Jul 18 '05 #2

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

Similar topics

3
1638
by: Dan Rawson | last post by:
I have several simple classes which need to us "standard" modules like os and sys A class might look like: # --------- MyClass.py ----------------------- class MyClass: def __init__ (self): self.data = '' self.data2 = 'mydata'
0
1064
by: Mage | last post by:
Hello, I thought that this will work: #m1.py def f1(): return string.join('a','a') #m2.py def f2():
5
1648
by: Alex | last post by:
Hi, this is my first mail to the list so please correct me if Ive done anything wrong. What Im trying to figure out is a good way to organise my code. One class per .py file is a system I like, keeps stuff apart. If I do that, I usually name the .py file to the same as the class in it. File: Foo.py *********************** class Foo:
3
2343
by: Fuzzyman | last post by:
Hello all, I am messing with namespaces, so that code I exec thinks it is executing in the __main__ module. I have the following code : import imp import sys
17
2061
by: Neil Cerutti | last post by:
The Glk API (which I'm implementing in native Python code) defines 120 or so constants that users must use. The constants already have fairly long names, e.g., gestalt_Version, evtype_Timer, keycode_PageDown. Calls to Glk functions are thus ugly and tedious. scriptref = glk.fileref_create_by_prompt( glk.fileusage_Transcript | glk.fileusage_TextMode, glk.filemode_WriteAppend, 0)
173
5652
by: Zytan | last post by:
I've read the docs on this, but one thing was left unclear. It seems as though a Module does not have to be fully qualified. Is this the case? I have source that apparently shows this. Are modules left-over from VB6, and not much used anymore? It seems that it is better to require Imports or use fully qualified names for functions in other classes/modules, but a Module doesn't require this, cluttering the global namespace. It seems...
2
2310
by: deltaquattro | last post by:
Hi, I'm currently learning C++ and I would like to ask you the difference between Fortran 95 modules and C++ namespaces. To me they look the same, except for the scope operator :: in C++, which is a bit more convenient than F95 rename operator =>, to avoid name clashes. Thank you, greetings,
0
1513
by: Martin P. Hellwig | last post by:
Hello all, I had some troubles in the past how to arrange my packages and modules, because I usually don't develop my stuff in the Lib\site-packages directory I have some troubles when importing depending modules that are in 'sibling' directories. Like in the following scenario: pkg_root\ -__init__ - common\ - - __init__
4
2460
by: Fabrizio Pollastri | last post by:
Hi, just an import problem: a program imports two modules, modA and modB, each module do not known anything about the other module (i.e. no cross imports in them), both modules needs to refer to some functions that are defined in the global namespace of the other module. There is a possible solution? In other words, can the importing program cover in some way the lack of cross imports between the modules? Thank in advance for any help.
0
7978
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
8448
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...
1
8126
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6796
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
5987
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
5470
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
3948
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
4010
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1313
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.