Style Guide For Python Code

Mulai dari awal
                                        

    the standard library (see PEP 3131): All identifiers in the Python

    standard library MUST use ASCII-only identifiers, and SHOULD use

    English words wherever feasible (in many cases, abbreviations and

    technical terms are used which aren't English). In addition,

    string literals and comments must also be in ASCII. The only

    exceptions are (a) test cases testing the non-ASCII features, and

    (b) names of authors. Authors whose names are not based on the

    latin alphabet MUST provide a latin transliteration of their

    names.

    Open source projects with a global audience are encouraged to

    adopt a similar policy.

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

      You should put a blank line between each group of imports.

      Put any relevant __all__ specification after the imports.

    - Relative imports for intra-package imports are highly discouraged.

      Always use the absolute package path for all imports.

      Even now that PEP 328 [7] is fully implemented in Python 2.5,

      its style of explicit relative imports is actively discouraged;

      absolute imports are more portable and usually more readable.

    - When importing a class from a class-containing module, it's usually okay

      to spell this

        from myclass import MyClass

        from foo.bar.yourclass import YourClass

      If this spelling causes local name clashes, then spell them

        import myclass

        import foo.bar.yourclass

      and use "myclass.MyClass" and "foo.bar.yourclass.YourClass"

Whitespace in Expressions and Statements

  Pet Peeves

    Avoid extraneous whitespace in the following situations:

Docstring ConventionsTempat cerita menjadi hidup. Temukan sekarang