是否可以获取Python中的关键字列表? [英] Is it possible to get a list of keywords in Python?
本文介绍了是否可以获取Python中的关键字列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取所有Pythons关键字的列表作为字符串.如果我可以对内置函数做类似的事情,那也很不错.
I'd like to get a list of all of Pythons keywords as strings. It would also be rather nifty if I could do a similar thing for built in functions.
类似这样的东西:
import syntax
print syntax.keywords
# prints ['print', 'if', 'for', etc...]
推荐答案
您询问了声明,同时在输出示例中显示了关键字.
You asked about statements, while showing keywords in your output example.
如果您要查找关键字,它们都列在 keyword
模块:
If you're looking for keywords, they're all listed in the keyword
module:
>>> import keyword
>>> keyword.kwlist
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif',
'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import',
'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try',
'while', 'with', 'yield']
来自 keyword.kwlist
文档:
包含为解释程序定义的所有关键字的序列.如果将任何关键字定义为仅在特定的
__future__
语句生效时才处于活动状态,则这些关键字也将包括在内.
Sequence containing all the keywords defined for the interpreter. If any keywords are defined to only be active when particular
__future__
statements are in effect, these will be included as well.
这篇关于是否可以获取Python中的关键字列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文