gpt4 book ai didi

visual-studio-code - 如何使 VS Code 自动完成 Python 类属性初始化

转载 作者:行者123 更新时间:2023-12-02 19:19:19 55 4
gpt4 key购买 nike

我正在为 Python 项目使用 VS Code。

当我写的时候:

class User:
def __init__(self, name, age, group=None):

我希望 VS Code 自动完成以下内容:

class User:
def __init__(self, name, age, group=None):
self.name = name
self.age = age
self.group = group

这可以用 VS Code 实现吗?我见过其他一些编辑这样做。这个有扩展吗?非常感谢!

最佳答案

我已经制作了一个基于 class init snippet by Mark 的带有属性初始值设定项的 Python 版本

  "Class Initializer": {
"prefix": "cinit",
"body": [
"def __init__(self, $1):",
"${1/([^,=]+)(?:=[^,]+)?(,\\s*|)/\tself.$1 = $1${2:+\n\t}/g}"
],
"description": "Class __init__"
}

我使用空格进行缩进,在另一个片段中 \t 被转换为空格。

如果选项卡未正确展开,请将 \t 替换为适当数量的空格。 (有 2 个 \t)。

输入class name: Enter后缩进1,然后输入前缀cinit

关于visual-studio-code - 如何使 VS Code 自动完成 Python 类属性初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63253433/

55 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com