What is the common header format of Python files?

后端 未结 4 443
后悔当初
后悔当初 2020-11-28 17:20

I came across the following header format for Python source files in a document about Python coding guidelines:

#!/usr/bin/env python

\"\"\"Foobar.py: Descr         


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 17:44

    The answers above are really complete, but if you want a quick and dirty header to copy'n paste, use this:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    """Module documentation goes here
       and here
       and ...
    """
    

    Why this is a good one:

    • The first line is for *nix users. It will choose the Python interpreter in the user path, so will automatically choose the user preferred interpreter.
    • The second one is the file encoding. Nowadays every file must have a encoding associated. UTF-8 will work everywhere. Just legacy projects would use other encoding.
    • And a very simple documentation. It can fill multiple lines.

    See also: https://www.python.org/dev/peps/pep-0263/

    If you just write a class in each file, you don't even need the documentation (it would go inside the class doc).

提交回复
热议问题