开发者

Python source file organization

I'm starting a new Python project, and want to follow standard conventions as closely as possible开发者_运维技巧. I've read that import statements should come first, for example. But I haven't found any conventions for things like putting all function definitions before or after all class definitions. Are there any conventions for things like this? Or does everyone typically just organize things like function and class definitions in whatever order seems to make sense?


PEP8 is the Python style guide: http://www.python.org/dev/peps/pep-0008/

Imports come at the top of the file, though method-level imports are allowed.

There's no specific ordering to classes and functions. Use what makes sense.


No, there's no convention for organization of functions and classes. However, there are some basic guidelines that will make your source flow better and make more sense to readers:

  • Document. Whatever you do, make sure that the uses (and in some cases, implementation) of classes and functions is described in plain english)
  • Group together things that are alike. Two functions that perform similar functions? Put them together.
  • Use common sense. If a class extends another, the base class should come first. If a function takes an instance of a class you defined, make sure the class definition comes first.

For examples on how this is done, look no further than the Python standard library. The source files from some of the modules should give you an idea how source is organized in Python.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜