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',
]
相关推荐
databook20 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室20 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三21 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户2519162427111 天前
Python之语言特点
python
刘立军1 天前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机1 天前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i1 天前
django中的FBV 和 CBV
python·django
c8i1 天前
python中的闭包和装饰器
python
这里有鱼汤1 天前
小白必看:QMT里的miniQMT入门教程
后端·python