zoukankan      html  css  js  c++  java
  • 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,自己不拍,别人也不能拍
    • 翻译关系,也有“预留字”的说法,有时候指关键字,有时候指保留字,视具体情况而定了
  • 相关阅读:
    Java设计模式—状态模式
    Java设计模式—备忘录模式
    android AsyncTask介绍
    Android UI线程和非UI线程
    Java设计模式—代理模式
    Java设计模式—命令模式
    <Android 应用 之路> MPAndroidChart~PieChart
    FPGA的EPCS 配置的2种方法 FPGA下载程序的方法(EPCS)
    如何将.sof转换成.jic
    quartus ii工程文件的分析
  • 原文地址:https://www.cnblogs.com/yorkyu/p/10306369.html
Copyright © 2011-2022 走看看