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

前端 未结 4 621
说谎
说谎 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:19

    As explicited in PEP 8, never mix tabs and space. However, a file with both may just run... As it says there:

    The most popular way of indenting Python is with spaces only.  The
    second-most popular way is with tabs only.  Code indented with a mixture
    of tabs and spaces should be converted to using spaces exclusively.
    When invoking the Python command line interpreter with the -t option, it issues
    warnings about code that illegally mixes tabs and spaces.  When using -tt
    these warnings become errors.  These options are highly recommended!
    

    the solution is therefore to use as a default:

    python -t my_mixed_code.py
    

    To answer at the editor level, this depends on the editor, please specify!

提交回复
热议问题