When to import

Konrad Hinsen hinsen at cnrs-orleans.fr
Wed Jun 14 05:17:40 EDT 2000


Anders M Eriksson <anders.eriksson at morateknikutveckling.se> writes:

> When does the import take place?

When the import statement is executed.

> Will there be any difference in the two cases below?

If the import of module2 has no side effects, then the result of the
two scripts will be the same.

> If I have a module that I very seldom use should I only import it when
> needed (using an if statement) or is it better to allways import it?

There is no reason in principle to import everything always. Importing
is just another way to assign an object to a name. If something is used
rarely, and more importantly only in a well-defined region of the code,
putting the import statement close to the "consumer" is a very reasonable
approach in my opinion (and I do it myself).
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                            | E-Mail: hinsen at cnrs-orleans.fr
Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.55.69
Rue Charles Sadron                       | Fax:  +33-2.38.63.15.17
45071 Orleans Cedex 2                    | Deutsch/Esperanto/English/
France                                   | Nederlands/Francais
-------------------------------------------------------------------------------



More information about the Python-list mailing list