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',
]
相关推荐
Li emily29 分钟前
如何通过外汇API平台快速实现实时数据接入?
开发语言·python·api·fastapi·美股
m0_5613596736 分钟前
掌握Python魔法方法(Magic Methods)
jvm·数据库·python
Ulyanov43 分钟前
顶层设计——单脉冲雷达仿真器的灵魂蓝图
python·算法·pyside·仿真系统·单脉冲
2401_838472511 小时前
使用Python进行图像识别:CNN卷积神经网络实战
jvm·数据库·python
CoLiuRs2 小时前
语义搜索系统原理与实现
redis·python·向量·es
摇滚侠2 小时前
IDEA 定义返回值快捷键
java·ide·intellij-idea
zhihuaba2 小时前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python
u0109272712 小时前
Python Web爬虫入门:使用Requests和BeautifulSoup
jvm·数据库·python
Stream_Silver3 小时前
【Agent学习笔记3:使用Python开发简单MCP服务】
笔记·python
穿过锁扣的风3 小时前
零基础入门 Python 爬虫:从基础到实战,爬取虎扑 / 豆瓣 / 图片全掌握
开发语言·爬虫·python