What is the common header format of Python files?

后端 未结 4 447
后悔当初
后悔当初 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:41

    I strongly favour minimal file headers, by which I mean just:

    • The hashbang (#! line) if this is an executable script
    • Module docstring
    • Imports, grouped in the standard way, eg:
      import os    # standard library
      import sys
    
      import requests  # 3rd party packages
    
      from mypackage import (  # local source
          mymodule,
          myothermodule,
      )
    

    ie. three groups of imports, with a single blank line between them. Within each group, imports are sorted. The final group, imports from local source, can either be absolute imports as shown, or explicit relative imports.

    Everything else is a waste of time, visual space, and is actively misleading.

    If you have legal disclaimers or licencing info, it goes into a separate file. It does not need to infect every source code file. Your copyright should be part of this. People should be able to find it in your LICENSE file, not random source code.

    Metadata such as authorship and dates is already maintained by your source control. There is no need to add a less-detailed, erroneous, and out-of-date version of the same info in the file itself.

    I don't believe there is any other data that everyone needs to put into all their source files. You may have some particular requirement to do so, but such things apply, by definition, only to you. They have no place in “general headers recommended for everyone”.

提交回复
热议问题