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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With