Importing modules inside python class

前端 未结 4 1676
孤独总比滥情好
孤独总比滥情好 2020-12-23 09:11

I\'m currently writing a class that needs os, stat and some others.

What\'s the best way to import these modules in my class?

I\'m

4条回答
  •  醉话见心
    2020-12-23 09:44

    PEP 8 on imports:

    Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

    This makes it easy to see all modules used by the file at hand, and avoids having to replicate the import in several places when a module is used in more than one place. Everything else (e.g. function-/method-level imports) should be an absolute exception and needs to be justified well.

提交回复
热议问题