美文网首页
Python函数(一)

Python函数(一)

作者: dataengineer | 来源:发表于2020-03-16 10:12 被阅读0次

获取函数func的docstring信息,方式如下:

  • 方式一:
docstring = func.__doc__
  • 方式二:
import inspect
docstring = inspect.getdoc(func)

获取Python关键字,方式如下:

import keyword
print(keyword.kwlist)

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

在文本my_string中查找关键字,关键字的样式为pattern,方式如下:

  • 方式一:仅匹配my_string的起始部分,若关键字不在文本的起始位置,则无结果返回
import re
re.match(pattern, my_string)
  • 方式二:匹配my_string的所有位置
import re
re.search(pattern, my_string) 

相关文章

网友评论

      本文标题:Python函数(一)

      本文链接:https://www.haomeiwen.com/subject/xhfxehtx.html