• 03 关键字?保留字?预留字?


    结论

    • 在 Python 中,不要多想,keyword 就是“关键字”

    • Python3.4 有 33 个关键字

    • Python3.7 有 35 个关键字

    • 官网中点击 What’s New In Python 3.7,查找 keywords 就能看到增加的两个关键字

      async and await are now reserved keywords

    查看

    方式一

    >>> help("keywords")
    
    Here is a list of the Python keywords.  Enter any keyword to get more help.
    
    False               class               from                or
    None                continue            global              pass
    True                def                 if                  raise
    and                 del                 import              return
    as                  elif                in                  try
    assert              else                is                  while
    async               except              lambda              with
    await               finally             nonlocal            yield
    break               for                 not                 
    
    >>> 
    >>> help(None)  # 三个大写开头的直接用 help 即可
    Help on NoneType object:
    
    class NoneType(object)
     |  Methods defined here:
     |  
     |  __bool__(self, /)
     |      self != 0
     |  
     |  __repr__(self, /)
     |      Return repr(self).
     |  
     |  ----------------------------------------------------------------------
     |  Static methods defined here:
     |  
     |  __new__(*args, **kwargs) from builtins.type
     |      Create and return a new object.  See help(type) for accurate signature.
    
    >>> 
    >>> help("break")  # 首字母非大写的需要先变成字符串,再 help
    The "break" statement
    *********************
    
       break_stmt ::= "break"
    
    "break" may only occur syntactically nested in a "for" or "while"
    loop, but not nested in a function or class definition within that
    loop.
    
    It terminates the nearest enclosing loop, skipping the optional "else"
    clause if the loop has one.
    
    If a "for" loop is terminated by "break", the loop control target
    keeps its current value.
    
    When "break" passes control out of a "try" statement with a "finally"
    clause, that "finally" clause is executed before really leaving the
    loop.
    
    Related help topics: while, for
    
    >>> 
    

    方式二

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

    补充

    • 对 Java 而言,“关键字”与“保留字”有所不同
      • goto, const 等是“保留字”但不是“关键字”
      • 简单地说,它们不能作为标识符,但也不是关键字
    • 这些“保留字”,就像有些电影公司买下 IP,自己不拍,别人也不能拍
    • 翻译关系,也有“预留字”的说法,有时候指关键字,有时候指保留字,视具体情况而定了
  • 相关阅读:
    ThinkPHP5.0被攻击,发现漏洞
    ThinkPHP5.0引用PHPExcel插件,在页面中导出数据库数据
    ThinkPHP5.0引入插件
    引入UEditor插件
    Form
    点击链接只跳转到首页/本地正常,上传后,除首页外,其余页面404
    Thinkphp5中嵌套循环
    ARouter转场动画无效,试试下面这种写法
    windows下运行.sh文件
    List集合增删元素时,UnsupportedOperationException报错问题
  • 原文地址:https://www.cnblogs.com/yorkyu/p/10306369.html
Copyright © 2020-2023  润新知