代码之家  ›  专栏  ›  技术社区  ›  Jorge Guberte

用Python嵌套导入声明是不是不好?

  •  0
  • Jorge Guberte  · 技术社区  · 14 年前

    我正在阅读App Engine的文档,我看到可以嵌套一些导入,例如:

    from google.appengine.ext import db, webapp #so on
    

    3 回复  |  直到 14 年前
        1
  •  2
  •   Sam Dolan    14 年前

    不,不是。见 PEP8 python styling standards 。以下是相关摘录:

    Imports
    
    - Imports should usually be on separate lines, e.g.:
    
        Yes: import os
             import sys
    
        No:  import sys, os
    
      it's okay to say this though:
    
        from subprocess import Popen, PIPE
    
    - Imports are always put at the top of the file, just after any module
      comments and docstrings, and before module globals and constants.
    
      Imports should be grouped in the following order:
    
      1. standard library imports
      2. related third party imports
      3. local application/library specific imports
    
        2
  •  3
  •   Ignacio Vazquez-Abrams    14 年前

    这不是嵌套,只是导入特定的名称。这本身没什么问题,但还是一如既往地咨询 PEP 8 样式指南。

        3
  •  1
  •   Nick Johnson    14 年前

    我会避免的。sdolan引用的PEP 8中给出的示例是从模块内部导入特定名称,但您要问的是在一行中导入多个不同的包。它不会中断,但是将它分离出来会更整洁,并且使以后重构和更改导入更容易。