Comments written in Python
#
: Use the pound were single-line comments
Python, seemingly does not provide multi-line comments, but we can take advantage of multi-line strings triple quotes to multi-line comments
"""
多行注释内容
多行注释内容
......
"""
或者
'''
多行注释内容
多行注释内容
......
'''
Documentation strings
In the definition of the function or class, the first line indent after using triple quotes annotation is the document string
It can be used __doc__
to view this string after
def a():
'''这是文档字符串'''
pass
print(a.__doc__)
#!
Designated interpreter
#! /usr/bin/env python3
Specify the encoding
With equal number of
# coding=<encoding name>
The most common, with a colon (most editors correctly identified)
#!/usr/bin/python
# -*- coding: <encoding name> -*-
vim's:
#!/usr/bin/python
# vim: set fileencoding=<encoding name> :
Exact expression:
更加精确的解释是:
符合正则表达式:"^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)" 就可以;
关于正则表达式的理解:
1."^"表示开始;
2."[ \t\v]"表示匹配制表符\t和垂直制表符\v,*表示匹配0次或者多次,则[ \t\v]*合起来理解就是匹配0次或者多次\t\v;
3."#"匹配#字符,即对应标题的#字符;
4.".*"表示接下来匹配任意字符,".*?"表示以非贪心算法匹配任意字符,对应标题中的“ -*- ”;
5."coding"对应标题中的coding;
6."[:=]"表示接下来的字符是":"或者"="出现的任意多个字符,对应标题中的":";
7.[ \t]*表示接下来匹配0次或者多次\t;标题中表示使用了0次;
8.[-_.a-zA-Z0-9]表示匹配出现'_'字符、小写字母'a至z'、大小字母'A至Z'、数字‘0至9’的任意多的字符,对应标题中的utf-8
9.接下来标题中的" -*- "则这个官方表达式没有给出解释,因此这个正则表达式应该是不完整的,我觉得完整的正则表达式可以为:
"^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+).*$"