python - Sublime 3,带有参数和返回的 python 函数文档字符串的片段

标签 python code-snippets sublimetext sublimetext3

我想写一个片段来将文档字符串添加到 Python 函数:

例如:

def foo(a):

    b=a+1

    return b

应该产生以下内容

def foo(a):
    """One liner description

    Parameters
    ----------
    a : type
        some comment

    Returns
    -------

    b : type
        some comment

    """

    b=a+1

    return b

我的第一次尝试是这个片段:

<snippet>
    <content><![CDATA[
"""${1:One liner description}

Parameters
----------
${2}
Returns
-------
${3}
"""
    ]]></content>
    <tabTrigger>docstring</tabTrigger>
    <scope>source.python</scope>
    <description>Adds a docstring skeleton to function</description>
</snippet>

在键入 docstring 和 Tab 时产生以下结果:

def foo(a):
    """One liner description

    Parameters
    ----------

    Returns
    -------

    """

    b=a+1

    return b

如何获取在代码片段中自动检测到的参数和返回值,例如示例中的“a”和“b”?

最佳答案

只需添加一个:< 默认值 >#之后在每个${#}停止点:

<snippet>
    <content><![CDATA[
"""${1:One liner description}

Parameters
----------
${2:a} : ${3:type}

Returns
-------
${4:b} : ${5:type}
"""
]]></content>
    <tabTrigger>docstring</tabTrigger>
    <scope>source.python</scope>
    <description>Adds a docstring skeleton to function</description>
</snippet>

关于python - Sublime 3,带有参数和返回的 python 函数文档字符串的片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18759312/

相关文章:

regex - Sublime Text : Exclude selection with regex?

python - Windows下Python从文件中读取路径名

jquery - 我如何使用 jQuery 将 HTML 转换为文本?

python - 从 pandas 列中删除仅彼此存在的重复字母,Python

visual-studio-code - VSCode 代码片段中的转换和正则表达式 - 文档

sublimetext - ST3 片段 : trigger HTML snippet but not on PHP scope

ftp - Sublime Text 3 中的 SFTP 不起作用

sublimetext - Sublime Text 2是否支持XML代码提示/完成?

python - Opencv (Python) - 脑肿瘤的异常形状 Blob 检测

python , Pandas : A Better way to get the first None position in list which give maximum consecutive None count