Imports awareness in Imported modules problem

alex23 wuwei23 at gmail.com
Sun Aug 24 07:27:41 EDT 2008


On Aug 24, 8:34 pm, "Mohamed Yousef" <harrr... at gmail.com> wrote:
> and sys  ,string.. etc add them all twice or four / n times ?
> 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

No, having each module contain all of the necessary imports for itself
-is- good practice.

While it might seem more convenient to define all of your imports in
one place, what you're really doing is injecting -all- references to -
all- imported items into -every- module. Having a global soup of
imported items like such is much, much harder to control for
unexpected name conflicts.

Ideally, you want each module's namespace to -only- contain references
to the external entities it's actually using. This not only makes each
module self-contained and more open to independent re-use, it makes
them -incredibly- easier to debug.

Please don't confuse temporary convenience with good practice.



More information about the Python-list mailing list