vscode python格式化

插件 Black Formatter

Black 默认会遵循 PEP 8 的规范,可配置的参数很少,用的人很多。

setting.json 配置,更改插件的每行字符数限制

javascript 复制代码
{
    "[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter"
    },
    "black-formatter.args": [
        "--line-length",
        "120"
    ],
}

可以使用 # fmt: off 和 # fmt: on 来告诉 Black 不要格式化特定的代码块。

python 复制代码
# fmt: off
if     True:
            print(    "我想怎么排版就怎么排版")
# fmt: on

插件 yapf

由 google 开发并维护的格式化工具,特点是支持多种格式化风格。默认支持三种格式化样式:pep8,google,Facebook和 chromium。 yapf的初衷不是让代码符合pep准则,而是让代码看起来更整洁更友好。

setting.json 配置

javascript 复制代码
{
    "[python]": {
        "editor.defaultFormatter": "eeyore.yapf"
    },
    "yapf.args": [
        "--style",
        "{based_on_style: google, indent_width: 4,column_limit: 160}"
    ],
}

不要格式化特定代码块

python 复制代码
# yapf: disable
if     True:
    print(    "我想怎么排版就怎么排版")
# yapf: enable

官方文档:https://github.com/google/yapf

列表格式化,如果末尾加逗号,就会竖向格式化,没有逗号则是横向格式化。

python 复制代码
abc = [
    'a',
    'b',
    'c',
]
相关推荐
renzao_ai8 小时前
本地 35B 大模型部署实战:Ollama 跑 Ornith-35B 全流程
python·llama·免费ai大模型
the局外人8 小时前
学习 FastAPI 的 Day 4:完成用户系统与接口联调(完结)
后端·python·fastapi
AIFQuant8 小时前
Python股票实时价格告警系统:WebSocket订阅与REST快照实战
开发语言·python·websocket·a股行情
名字还没想好☜9 小时前
Java Collectors.groupingBy 进阶:多级分组、下游收集器与统计聚合一次搞定
java·windows·后端·python·spring
2601_962078239 小时前
Python接口自动化流程(pytest)
python·pytest·接口自动化·测试框架·断言
杨女士YRJ9 小时前
python+pytest+01
python·pytest
颜颜yan_9 小时前
Eclipse Theia 鸿蒙 PC 适配全记录:在 HarmonyOS PC 上运行完整 IDE 工作台
ide·eclipse·harmonyos
鸽芷咕10 小时前
Mu Editor 鸿蒙 PC 适配全记录:用 Qt 与嵌入式 CPython 重建教学型 Python IDE
python·qt·harmonyos
微软技术分享10 小时前
基于 HuggingFace Tokenizers 训练自定义分词器
python·ubuntu·大模型·huggingface
gf132111110 小时前
python_调用远程服务器上的openclaw
服务器·python·php