Python ID: Import Modules

Let's say I have two Python modules:

module1.py :

 import module2 def myFunct(): print "called from module1" 

module2.py :

 def myFunct(): print "called from module2" def someFunct(): print "also called from module2" 

If I import module1 , is it better to etiquette re-import module2 or just reference it as module1.module2 ?

For example ( someotherfile.py ):

 import module1 module1.myFunct() # prints "called from module1" module1.module2.myFunct() # prints "called from module2" 

I can also do this: module2 = module1.module2 . Now I can directly call module2.myFunct() .

However, I can change module1.py to:

 from module2 import * def myFunct(): print "called from module1" 

Now, in someotherfile.py , I can do this:

 import module1 module1.myFunct() # prints "called from module1"; overrides module2 module1.someFunct() # prints "also called from module2" 

Also, by importing * , help ('module1') shows all the functions from module2 .

On the other hand, (if module1.py uses import module2 ), I can do: someotherfile.py :

  import module1, module2 module1.myFunct() # prints "called from module1" module2.myFunct() # prints "called from module2" 

Again, what is the best etiquette and practice? To import module2 again, or just reference module1 import?

+6
source share
2 answers

Just import module2 . Reimporting is relatively inconclusive, since Python caches module objects in sys.modules .

In addition, point chains, as in module1.module2.myFunct , are in violation of the law of the Law of Demeter . You might someday want to replace module1 with another module1a that does not import module2 . Using import module2 , you do not have to rewrite all occurrences of module1.module2.myFunct .

from module2 import * is generally bad practice as it makes it difficult to keep track of where the variables come from. And the module namespace can create variable name conflicts. For example, from numpy import * is a specific no-no value, as this will override Python's builtin sum , min , max , any , all , abs and round .

+2
source

Quoting PEP 8 Style Guide :

When importing a class from a module containing a class, this is usually normal:

 from myclass import MyClass from foo.bar.yourclass import YourClass 

If this spelling causes local name conflicts, write them down

 import myclass import foo.bar.yourclass 

Emphasis is mine.

Do not use module1.module2 ; you rely on the internal implementation details of module1 , which can later change the import. You can import module2 directly, so do it unless otherwise documented by the module author.

You can use the __all__ to restrict import from a module using from modulename import * ; help() also rewards this list. The list of names explicitly exported to __all__ helps to clear the text representation of help() :

The public names defined by the module are determined by checking the module namespace for the variable named __all__ ; if defined, it should be a sequence of strings that are names defined or imported by this module. Names specified in __all__ are considered public and must exist. If __all__ not defined, the set of public names includes all names found in the module namespace that do not begin with an underscore ( '_' ). __all__ must contain the entire open API. It is designed to avoid accidentally exporting elements that are not part of the API (for example, library modules that have been imported and used in the module).

+3
source

Source: https://habr.com/ru/post/948399/


All Articles