Vscode相对于Pycharm简直是轻量,但是代码自动提示也弱了很多,比如pylance就没有支持运算符重载函数(__iter__、__next__()、__add__()等)的自动完成提示。
废话不多说,直接上解决方案:
1. File->Preference->User Snippets->python.json
2. 在编辑框中输入一下内容
{
// 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:
"def __del__(self):": {
"prefix": "def __del__(self):",
"body": [
"def __del__(self):",
"\t'''析构函数,其功能是销毁对象时进行回收资源的操作'''",
"\t$2"
]
},
"def __add__(self, other):": {
"prefix": "def __add__(self, other):",
"body": [
"def __add__(self, other):",
"\t'''加法运算符 +,当类对象 X 做例如 X+Y 或者 X+=Y 等操作,内部会调用此方法。但如果类中对 __iadd__ 方法进行了重载,则类对象 X 在做 X+=Y 类似操作时,会优先选择调用 __iadd__ 方法。'''",
"\t$2"
]
},
"def __radd__(self, other):": {
"prefix": "def __radd__(self, other):",
"body": [
"def __radd__(self, other):",
"\t'''当类对象 X 做类似 Y+X 的运算时,会调用此方法。'''",
"\t$2"
]
},
"def __iadd__(self, other):": {
"prefix": "def __iadd__(self, other):",
"body": [
"def __iadd__(self, other):",
"\t'''析构重载 += 运算符,也就是说,当类对象 X 做类似 X+=Y 的操作时,会调用此方法。函数