What are Python coding standards/best practices?

PythonProgramming Scripts

You can use the PEP8 guide as a holy grail. Almost all python world uses this guide to write clean understandable and standard python code. This is available as an extension as a linter for all modern text editors. You can check it out at  http://www.python.org/dev/peps/pep-0008/

Properly Structure your folders. All projects need proper structuring. This helps organize code better. Python has an opinionated folder structure that you should use.

README.rst
LICENSE
setup.py
requirements.txt
sample/__init__.py
sample/core.py
sample/helpers.py
docs/conf.py
docs/index.rst
tests/test_basic.py
tests/test_advanced.py


Use doctests. The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown. This is one of the best and most useful peices of the language and you should use it to the max. This allows doing TDD.

Use PyPi to get all your modules. PyPi is a central repository of python modules. Instead of building your own packages, use pip to install modules off of Pypi.


Write Pythonic code by adhering to python idioms. This takes some getting used to but can help you go a long way. You can read more about idiomatic python on
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html

raja
Published on 18-Apr-2018 12:07:58
Advertisements