Enforce “spaces” or “tabs” only in python files?

前端 未结 4 618
说谎
说谎 2021-01-13 16:38

In Python, is there a mean to enforce the use of spaces or tabs indentation with a per file basis ?

Well, perhaps \"enforce\" is too strong, more like a \"recommend

4条回答
  •  时光说笑
    2021-01-13 17:09

    Tim Peters has written a nifty script called reindent.py which converts .py files to use 4-space indents and no tabs. It is available here, but check your distribution first -- it may have come bundled in an Examples or Tools directory. (On the latest LTS Ubuntu, it is provided by the python2.7-examples package.)

    If you can set up a Mercurial hook you should be able to run all files through reindent.py.

    By the way, if you are using unix, then your system may also have the expand (and unexpand) command, which convert all tabs to spaces (and spaces to tabs). However, in this case, I think reindent.py is the right tool.

提交回复
热议问题