473,666 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2461
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_modul e.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_modul e.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
5118
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 the days before PHP got object-oriented features. For instance we currently have:
3
3810
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 'import os\ndef test():\n print os.getcwd()' > os.py cd /tmp
1
2225
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
1988
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 get the following output: inc: 2 A: 2 inc: 3 B: 3
7
2152
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 ask the OS directly? oh and another question: which parts of my program will actually use my version of new ? only the ones that include the declaration (do i even need one or is a definition in a cpp file ok?). what about statically linked...
11
2549
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 opaque data types to transfer information in between them. At some stage I was stuck and needed to make a variable global, and I also needed to make the struct declaration public to some other parts. Looking through the code I found out that lots...
8
2699
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 variables that need to be accessed by a number of classes that exists in separate namespaces (files), what would be the best way to do this? So far, I have approached the problem by making the variables attributes of one class and passing...
18
2940
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 have a local identical variable name and want to save/load it to/from the global with same name * while you add code, the definition of globals moves more and more apart from their use cases -> weirdness; programmers thinking is fragmented * using...
2
2313
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
8356
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
8869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
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
6198
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
4198
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
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1775
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.