Python怎么给函数自动添加中文注释?

使用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

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏悠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值