python关键字列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14595922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List of python keywords
提问by Mike Vella
I'm writing a program which does some code generation to python and need to treat a string differently if it is a python keyword. help(keywords)prints the python keywords and some extra stuff, but I wonder if there's a pythonic way to get an actual iterable object with these strings in it?
我正在编写一个程序,它对 python 进行一些代码生成,如果它是 python 关键字,则需要以不同的方式处理字符串。help(keywords)打印 python 关键字和一些额外的东西,但我想知道是否有一种 pythonic 方法来获取包含这些字符串的实际可迭代对象?
采纳答案by Abhijit
You are better of using the keywordmodule
你最好使用关键字模块
>>> 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']
回答by Thai Tran
You can check a specific keyword as well
您也可以检查特定的关键字
import keyword
keyword.iskeyword("print") # TRUE

