插件 Black Formatter
Black 默認會遵循 PEP 8 的規范,可配置的參數很少,用的人很多。
setting.json 配置,更改插件的每行字符數限制
{"[python]": {"editor.defaultFormatter": "ms-python.black-formatter"},"black-formatter.args": ["--line-length","120"],
}
可以使用 # fmt: off 和 # fmt: on 來告訴 Black 不要格式化特定的代碼塊。
# fmt: off
if True:print( "我想怎么排版就怎么排版")
# fmt: on
插件 yapf
由 google 開發并維護的格式化工具,特點是支持多種格式化風格。默認支持三種格式化樣式:pep8,google,Facebook和 chromium。 yapf的初衷不是讓代碼符合pep準則,而是讓代碼看起來更整潔更友好。
setting.json 配置
{"[python]": {"editor.defaultFormatter": "eeyore.yapf"},"yapf.args": ["--style","{based_on_style: google, indent_width: 4,column_limit: 160}"],
}
不要格式化特定代碼塊
# yapf: disable
if True:print( "我想怎么排版就怎么排版")
# yapf: enable
官方文檔:https://github.com/google/yapf
列表格式化,如果末尾加逗號,就會豎向格式化,沒有逗號則是橫向格式化。
abc = ['a','b','c',
]