使用VS Code或者Spyder写代码时,使用三个双引号,可以给函数添加英文的注释。
但是,添加的注释,格式是设定好的,无法改变,而且是英文的,使用起来不方便。
那么,要怎么给函数自动添加中文注释呢?
1.VS Code
左上角菜单,打开文件,选择首选项,选择配置用户代码片段。
然后在弹出的框里输入并选择python.json,即可打开此文件。
进入后界面如下:
{
// Place your snippets for python here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
}
翻译一下这段描述:
{
//将python代码段放在这里。每个代码段都在代码段名称下定义,并具有前缀、正文和描述。
//前缀是用来触发代码段的前缀,正文将展开并插入。可能的变量有:
//制表位:$1,$2,最后光标位置:$0,占位符:${1:label},${2:another}。
//具有相同ID的占位符会被连接起来。
//示例:
// "Print to console": {
// "prefix": "log", "前缀":"log"
// "body": [ "正文":[
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console" "描述":"打印到控制台"
// }
}
文档里有一个说明和示例。
现在,我们往里面添加我们想要的内容。
{
// Place your snippets for python here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
//自定义的函数注释
"插入注释": {
"prefix": "zhushi",
"body": [
//添加3个双引号
"\"\"\"",
//$0表示最后光标移到说明后面
"${1:说明:}$0",
"------------------------------------",
"变量:",
"------------------------------------",
"返回:",
"------------------------------------",
//添加3个双引号
"\"\"\"",
],
"description": "添加函数注释"
}
}
保存,搞定!在写代码时,输入zhushi,然后回车,就可以插入预先设置好的注释内容了。
鼠标的光标,则自动定位到说明后面。
def jieshi():
"""
说明:
------------------------------------
变量:
------------------------------------
返回:
------------------------------------
"""
2.Spyder
在左上角菜单,依次点开工具→偏好设置→代码补全及语法检查,然后选择代码片段。
然后创建新的代码片段。
触发文本、描述和代码片段文本都填上后,点OK。
"""
说明:
------------------------------------
变量:
------------------------------------
返回:
------------------------------------
"""
在写代码时,输入zhushi,然后回车,就可以插入预先设置好的注释内容了。
例如:
def lizi():
"""
说明:这是个示例。
------------------------------------
变量:无
------------------------------------
返回:无
------------------------------------
"""
pass