473,405 Members | 2,171 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,405 software developers and data experts.

Re: Imports awareness in Imported modules problem

En Sun, 24 Aug 2008 07:34:41 -0300, Mohamed Yousef <ha******@gmail.comescribió:
On Sun, Aug 24, 2008 at 9:59 AM, Fredrik Lundh <fr*****@pythonware.comwrote:
>Mohamed Yousef wrote:
>>why am i doing this in the first place
I'm in the process of a medium project where imports of modules start
to make a jungle and i wanted all needed imports to be in a single
file (namely __init__.py)
and then all imports are made once and other modules feel it

Python doesn't use a global namespace -- importing a given module into one
module doesn't make it visible everywhere else (and trust me, this is a very
good thing).
why isn't it a good thing (even if optional)
consider the sitution in which a utility module is used every where
else - other modules -
you may say import it in them all ,
Yes. That way, when you see a name "foo" used in a module, you can look at the imports to see where it comes from. (BTW, this is why using "from module import *" is not a good idea)
Having a single global namespace is a lot worse.
what i changed it's name ? go back
change all imports... this doesn't seem good
Yes. How often do you change the module's name? You may use an alias if you want to be backwards compatible.
and what about package wide varailbles ?
If pkgA is a package, and you define a variable foo in its __init__.py, it is available as pkgA.foo - does it count as a "package wide variable"?
>>my goal is basically making W() aware of the re module when called
from A

to do that, add "import re" to the top of the C module.
and sys ,string.. etc add them all twice or four / n times ?
Every module has its own namespace, it contains its own set of imports, unrelated to others.
If you want to use the re, or sys, or string modules - import them. As F.L. has already pointed, a module is loaded from disk only the first time it is imported; subsequent imports find the module in sys.modules and do nothing.
remove one and then forget to remove one of them in a file and start
debugging ... this really doesn't look to be a good practice
Why is that? Importing an otherwise unused module is usually just a performance problem - at least for well-behaving modules that don't have side effects on import.

--
Gabriel Genellina

Aug 24 '08 #1
0 1337

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

Similar topics

16
by: max(01)* | last post by:
hi everybody. suppose that code-1.py imports code-2.py and code-3.py (because it uses names from both), and that code-2.py imports code-3.py. if python were c, code-1.c should only *include*...
2
by: Matthias Kramm | last post by:
Hi All, I'm having a little bit of trouble using the "imp" module to dynamically import modules. It seems that somehow cyclic references of modules don't work. I'm unable to get the following...
4
by: Martin M. | last post by:
Hi, I have the following question: How can an imported module see/find the path to itself? Background: From my main script I import a module which needs a file (AppleScript) located in the...
6
by: Ritesh Raj Sarraf | last post by:
Hi, I've been very confused about why this doesn't work. I mean I don't see any reason why this has been made not to work. class Log: def __init__(self, verbose, lock = None): if verbose...
0
by: Kay Schluehr | last post by:
Since their introduction in Python 2.5 I only reviewed the new "relative import" notation briefly by reading the "What's new in Python 2.5" article. Now I wanted checkout if I get comfortable with...
3
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and...
0
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and...
0
by: Terry Reedy | last post by:
Mohamed Yousef wrote: If you want to use module A in both B and C, B and C should both import A. No problem. ??
4
by: Mohamed Yousef | last post by:
On Sun, Aug 24, 2008 at 9:59 AM, Fredrik Lundh <fredrik@pythonware.comwrote: why isn't it a good thing (even if optional) consider the sitution in which a utility module is used every where else -...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
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...
0
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...
0
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,...
0
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...

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.