开发者

Is it a bad idea to nest importing declarations in Python?

I'm reading App Engine's documentation and i saw that i could nest some imports, for example:

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

Is it a bad thing to do so? If it's not (since it works), what's the limit/advantages/disadvan开发者_如何学Pythontages of that?


That's not nesting, that's just importing specific names. There's nothing wrong with that per se, but as always consult PEP 8 for style guidelines.


No it's not. See the PEP8 python styling standards. Here's the pertinent excerpt:

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


I would avoid it. The examples given in PEP 8, quoted by sdolan, are imports of specific names from inside a module, but what you're asking about is importing multiple different packages on a single line. It won't break, but separating it out is neater, and makes it easier to refactor and change imports later.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜