python2和python3中的关键字的区别--keyword模块

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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个关键字

[[email protected] ~]$ 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个关键字。







































































以上是关于python2和python3中的关键字的区别--keyword模块的主要内容,如果未能解决你的问题,请参考以下文章

python2和python3的一些区别

python2.X与Python3.X区别

python2 与 python3的区别

python2 与 python3的区别

python2 与 python3的区别

Python3 中的 Futures 和 ES6 中的 Promise 的区别