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',
]
相关推荐
量化分析码农8 小时前
【Python量化趋势跟踪实战 02】突破买入总被假突破坑:通道系统与入场时机
python
Agent世界8 小时前
多平台登录态巡检:cookie 判据为什么会撒谎
python
量化分析码农9 小时前
【Python量化趋势跟踪实战 01】均线多头排列就是趋势开始吗:过滤与确认
python
用户298698530149 小时前
Python 实战:把代码写入 Word 文档的两种实现方式
后端·python·api
小小张说故事9 小时前
Python 里 pickle 到底存了什么:拆开 __reduce__、__getstate__ 和那串能执行代码的字节码
python
reasonsummer9 小时前
【教学类-150-03】20261108蝴蝶连连看html(一页2个版面、3个版面)
python·html
用户019027581619 小时前
用 Polars 快速分析 A 股 K 线:比 pandas 更快的列式处理
python
用户03321266636710 小时前
使用 Python 轻松加密或解密 Excel 文档
python·excel
CyrusYiVPP10 小时前
手写 MCP Server:给问答机器人接上本地文件工具,Python 实战第 13 期
人工智能·python
特立独行的猫A11 小时前
cj-tauri:用仓颉写桌面与鸿蒙 PC 应用
java·后端·python