【问题标题】:List of python keywords [duplicate]python关键字列表[重复]
【发布时间】:2013-01-30 02:54:24
【问题描述】:

我正在编写一个程序,它对 python 进行一些代码生成,如果它是 python 关键字,则需要以不同的方式处理字符串。 help(keywords) 打印 python 关键字和一些额外的东西,但我想知道是否有一种 python 方式来获取包含这些字符串的实际可迭代对象?

【问题讨论】:

标签: python


【解决方案1】:

你最好使用keyword 模块

>>> 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']

【讨论】:

    【解决方案2】:

    您也可以检查特定的关键字

    import keyword
    keyword.iskeyword("print") # TRUE
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 2012-01-24
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      相关资源
      最近更新 更多