简体   繁体   中英

Python Etiquette: Importing Modules

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 etiquette to re-import module2 , or just refer to 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 of the functions from module2 .

On the other hand, (assuming 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, which is better etiquette and practice? To import module2 again, or to just refer to module1 's importation?

Quoting the PEP 8 style guide :

When importing a class from a class-containing module, it's usually okay to spell this:

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

If this spelling causes local name clashes , then spell them

 import myclass import foo.bar.yourclass 

Emphasis mine.

Don't use module1.module2 ; you are relying on the internal implementation details of module1 , which may later change what imports it is using. You can import module2 directly, so do so unless otherwise documented by the module author.

You can use the __all__ convention to limit what is imported from a module with from modulename import * ; the help() command honours that list as well. Listing the names you explicitly export in __all__ helps clean up the help() text presentation:

The public names defined by a module are determined by checking the module's namespace for a variable named __all__ ; if defined, it must be a sequence of strings which are names defined or imported by that module. The names given in __all__ are all considered public and are required to exist. If __all__ is not defined, the set of public names includes all names found in the module's namespace which do not begin with an underscore character ( '_' ). __all__ should contain the entire public API. It is intended to avoid accidentally exporting items that are not part of the API (such as library modules which were imported and used within the module).

Just import module2 . Re-importing is relatively costless, since Python caches module objects in sys.modules .

Moreover, chaining dots as in module1.module2.myFunct is a violation of the Law of Demeter . Perhaps some day you will want to replace module1 with some other module module1a which does not import module2 . By using import module2 , you will avoid having to rewrite all occurrences of module1.module2.myFunct .

from module2 import * is generally a bad practice since it makes it hard to trace where variables come from. And mixing module namespaces can create variable-name conflicts. For example, from numpy import * is a definite no-no, since doing so would override Python's builtin sum , min , max , any , all , abs and round .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM