473,287 Members | 1,629 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,287 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 2435
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.