zoukankan      html  css  js  c++  java
  • python2和python3中的关键字的区别--keyword模块

    一、python3.6中的:

    共33个关键字:

    通过导入keyword模块,查看python所有的关键字。在ipython中:

    Python 3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1
    900 64 bit (AMD64)]
    Type "copyright", "credits" or "license" for more information.

    IPython 5.1.0 -- An enhanced Interactive Python.
    ? -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help -> Python's own help system.
    object? -> Details about 'object', use 'object??' for extra details.

    In [1]: import keyword

    In [2]: dir(keyword)
    Out[2]:
    ['__all__',
    '__builtins__',
    '__cached__',
    '__doc__',
    '__file__',
    '__loader__',
    '__name__',
    '__package__',
    '__spec__',
    'iskeyword',
    'kwlist',
    'main']

    In [3]: keyword.iskeyword
    Out[3]: <function frozenset.__contains__>

    In [4]: keyword.iskeyword?
    Docstring: x.__contains__(y) <==> y in x.
    Type: builtin_function_or_method

    二、python2.7中的:

    共31个关键字

    [zqf@zengqingfu ~]$ ipython
    Python 2.7.13 |Anaconda custom (64-bit)| (default, Dec 20 2016, 23:09:15)
    Type "copyright", "credits" or "license" for more information.

    IPython 5.1.0 -- An enhanced Interactive Python.
    ? -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help -> Python's own help system.
    object? -> Details about 'object', use 'object??' for extra details.

    In [2]: import keyword

    In [3]: keyword.kwlist
    Out[3]:
    ['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']

    In [4]: dir(keyword)
    Out[4]:
    ['__all__',
    '__builtins__',
    '__doc__',
    '__file__',
    '__name__',
    '__package__',
    'iskeyword',
    'kwlist',
    'main']

    三、区别:

    python3 相比python2的关键字,减 少了print和exec这两个关键字, 但是新增了False,None,True,nonlocal这4个关键字。

  • 相关阅读:
    地址栏传值 JS取值方法
    定位导航 制作
    验证码
    图片水印
    AJAX 三级联动
    javascript 和Jquery 互转
    Jquery 事件 DOM操作
    Jquery 基础
    软件工程中的形式化方法读后感
    软件工程理论、方法与实践 需求工程读后感
  • 原文地址:https://www.cnblogs.com/zengqingfu1442/p/7257645.html
Copyright © 2011-2022 走看看