473,386 Members | 2,114 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.

merging the global namespaces of two modules

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.

F. Pollastri
Jun 27 '08 #1
4 2441
Can you be more specific? modA and modB don't import from each other
but both need to access objects in the global namespace of what
module? The controlling application? Or do you mean that, for
example, modA needs to access some functions in modB, but does not
have import statements to do so, and you want the controlling program
to do the imports for it? If that is the case, that is bad
programming practice in Python. Python is not C and you can't just
share header files :)

If you need to avoid recursive import conflicts, you can perform your
imports at the top of functions that use objects from another module.

Jeff
http://www.artfulcode.net
Jun 27 '08 #2
Fabrizio Pollastri a écrit :
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.
IOW, you have a circular dependency between modA and modB.
There is a possible solution?
The obvious one : extract the relevant functions from modA and modB into
modC and import modC in both modA and modB.

If not applyable in your case, please provide more informations.

Jun 27 '08 #3
Jeff wrote:
Can you be more specific? modA and modB don't import from each other
but both need to access objects in the global namespace of what
module? The controlling application?
Or do you mean that, for
example, modA needs to access some functions in modB, but does not
have import statements to do so, and you want the controlling program
to do the imports for it?
Exactly this.
If that is the case, that is bad
programming practice in Python. Python is not C and you can't just
share header files :)
Ok. I know that I want a python import to behave like a C include, it is so bad?

The motivation arise from a family of applications that import, among others,
two modules: a basic one and a specific one. The specific one is chosen from a
set of modules at import time depending on other imported modules, so the basic
module can known its external references against the specific module only at run
time.

One solution can be:
-the application imports the basic module,
-the basic module selects the proper specific module among the allowable
specific modules,
-the basic module imports the selected module with an __import__ call equivalent
to "import selected_module_X as select_module",
-Any reference to the specific module in the basic module is written in the form
"selected_module.object_name".

Any better solution?
Fabrizio.
Jun 27 '08 #4
Fabrizio Pollastri wrote:
Jeff wrote:
>Can you be more specific? modA and modB don't import from each other
but both need to access objects in the global namespace of what
module? The controlling application?
>Or do you mean that, for
example, modA needs to access some functions in modB, but does not
have import statements to do so, and you want the controlling program
to do the imports for it?

Exactly this.
>If that is the case, that is bad
programming practice in Python. Python is not C and you can't just
share header files :)

Ok. I know that I want a python import to behave like a C include, it is
so bad?

The motivation arise from a family of applications that import, among
others, two modules: a basic one and a specific one. The specific one is
chosen from a set of modules at import time depending on other imported
modules, so the basic module can known its external references against the
specific module only at run time.

One solution can be:
-the application imports the basic module,
-the basic module selects the proper specific module among the allowable
specific modules,
-the basic module imports the selected module with an __import__ call
equivalent to "import selected_module_X as select_module",
-Any reference to the specific module in the basic module is written in
the form "selected_module.object_name".

Any better solution?
http://peak.telecommunity.com/DevCen...ackage-support

Diez
Jun 27 '08 #5

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

Similar topics

88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
3
by: George P | last post by:
I've run into a strange package related namespace problem. Follow these steps (on UNIX system) to illustrate: ------------------------- mkdir /tmp/mypkg cd /tmp/mypkg touch __init__.py echo...
1
by: Andr? Roberge | last post by:
I have the following two files: #--testexec.py-- def exec_code(co): try: exec co except: print "error" #-- test.py--
4
by: Bart | last post by:
Hi all I don't understand globals between multiple modules in a python program. I really don't. I've narrowed it down to the following two very simple programs a.py and b.py. When I run a.py I...
7
by: Martin Stich | last post by:
hi, i want to redefine the global operator new for logging and stats purposes. is it a good idea to use malloc() as allocation function ? or should i call one of those weird crt functions....or...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
8
by: newbie | last post by:
Hello, I have questions about global variables in OOP (in general) and Python (in specific). I understand (I think) that global variables are generally not a good idea. However, if there are...
18
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you...
2
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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,...

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.