﻿{"id":1041,"date":"2021-03-23T20:32:12","date_gmt":"2021-03-23T12:32:12","guid":{"rendered":"https:\/\/byy3.com\/?p=1041"},"modified":"2021-11-30T06:15:39","modified_gmt":"2021-11-29T22:15:39","slug":"2to3%e8%87%aa%e5%8a%a8%e5%b0%86python2%e4%bb%a3%e7%a0%81%e8%bd%ac%e4%b8%bapython3%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/byy3.com\/?p=1041","title":{"rendered":"2to3\u81ea\u52a8\u5c06Python2\u4ee3\u7801\u8f6c\u4e3aPython3\u4ee3\u7801\u548c\u5728\u7ebf\u7f51\u7ad9"},"content":{"rendered":"<p>\u5728\u7ebf<a href=\"https:\/\/byy3.com\/1041.html\">python2\u8f6c\u6362python3<\/a>\u5728\u7ebfhttps:\/\/360techexplorer.com\/tools\/python-2-to-3-converter\/<\/p>\n<hr \/>\n<p><strong>\u5728python3\u4e2d\u5b89\u88c5python\u5e93 pip3 install 2to3<br \/>\n\u7136\u540e\u518d\u4f7f\u7528 2to3 -w test.py<br \/>\n\u4f1a\u81ea\u52a8\u8f6c\u6362\u5e76\u751f\u6210\u4e00\u4e2atest.py.bak<\/strong><\/p>\n<hr \/>\n<p>2to3 \u662f\u4e00\u4e2a Python \u7a0b\u5e8f\uff0c\u5b83\u53ef\u4ee5\u8bfb\u53d6 Python 2.x \u7684\u6e90\u4ee3\u7801\u5e76\u4f7f\u7528\u4e00\u7cfb\u5217\u7684 \u4fee\u590d\u5668 \u6765\u5c06\u5176\u8f6c\u6362\u4e3a\u5408\u6cd5\u7684 Python 3.x \u4ee3\u7801\u3002 \u6807\u51c6\u5e93\u5df2\u5305\u542b\u4e86\u4e30\u5bcc\u7684\u4fee\u590d\u5668\uff0c\u8fd9\u8db3\u4ee5\u5904\u7406\u51e0\u4e4e\u6240\u6709\u4ee3\u7801\u3002 \u4e0d\u8fc7 2to3 \u7684\u652f\u6301\u5e93 lib2to3 \u662f\u4e00\u4e2a\u5f88\u7075\u6d3b\u901a\u7528\u7684\u5e93\uff0c\u6240\u4ee5\u8fd8\u53ef\u4ee5\u7f16\u5199\u4f60\u81ea\u5df1\u7684 2to3 \u4fee\u590d\u5668\u3002<\/p>\n<p>\u4f7f\u7528 2to3<br \/>\n2to3 \u901a\u5e38\u4f1a\u4f5c\u4e3a\u811a\u672c\u548c Python \u89e3\u91ca\u5668\u4e00\u8d77\u5b89\u88c5\uff0c\u4f60\u53ef\u4ee5\u5728 Python \u6839\u76ee\u5f55\u7684 Tools\/scripts \u6587\u4ef6\u5939\u4e0b\u627e\u5230\u5b83\u3002<\/p>\n<p>2to3 \u7684\u57fa\u672c\u8c03\u7528\u53c2\u6570\u662f\u4e00\u4e2a\u9700\u8981\u8f6c\u6362\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u5217\u8868\u3002\u5bf9\u4e8e\u76ee\u5f55\uff0c\u4f1a\u9012\u5f52\u5730\u5bfb\u627e\u5176\u4e2d\u7684 Python \u6e90\u7801\u3002<\/p>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a Python 2.x \u7684\u6e90\u7801\u6587\u4ef6\uff0cexample.py\uff1a<\/p>\n<p>def greet(name):<br \/>\nprint \"Hello, {0}!\".format(name)<br \/>\nprint \"What's your name?\"<br \/>\nname = raw_input()<br \/>\ngreet(name)<br \/>\n\u5b83\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528 2to3 \u8f6c\u6362\u6210 Python 3.x \u7248\u672c\u7684\u4ee3\u7801\uff1a<\/p>\n<p>$ 2to3 example.py<br \/>\n\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u6253\u5370\u51fa\u548c\u6e90\u6587\u4ef6\u7684\u533a\u522b\u3002\u901a\u8fc7\u4f20\u5165 -w \u53c2\u6570\uff0c2to3 \u4e5f\u53ef\u4ee5\u628a\u9700\u8981\u7684\u4fee\u6539\u5199\u56de\u5230\u539f\u6587\u4ef6\u4e2d\uff08\u9664\u975e\u4f20\u5165\u4e86 -n \u53c2\u6570\uff0c\u5426\u5219\u4f1a\u4e3a\u539f\u59cb\u6587\u4ef6\u521b\u5efa\u4e00\u4e2a\u526f\u672c\uff09\uff1a<\/p>\n<p>$ 2to3 -w example.py<br \/>\n\u5728\u8f6c\u6362\u5b8c\u6210\u540e\uff0cexample.py \u770b\u8d77\u6765\u50cf\u662f\u8fd9\u6837\uff1a<\/p>\n<p>def greet(name):<br \/>\nprint(\"Hello, {0}!\".format(name))<br \/>\nprint(\"What's your name?\")<br \/>\nname = input()<br \/>\ngreet(name)<br \/>\n\u6ce8\u91ca\u548c\u7f29\u8fdb\u90fd\u4f1a\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c2to3 \u4f1a\u6267\u884c \u9884\u5b9a\u4e49\u4fee\u590d\u5668 \u7684\u96c6\u5408\u3002\u4f7f\u7528 -l \u53c2\u6570\u53ef\u4ee5\u5217\u51fa\u6240\u6709\u53ef\u7528\u7684\u4fee\u590d\u5668\u3002\u4f7f\u7528 -f \u53c2\u6570\u53ef\u4ee5\u660e\u786e\u6307\u5b9a\u9700\u8981\u4f7f\u7528\u7684\u4fee\u590d\u5668\u96c6\u5408\u3002\u800c\u4f7f\u7528 -x \u53c2\u6570\u5219\u53ef\u4ee5\u660e\u786e\u6307\u5b9a\u4e0d\u4f7f\u7528\u7684\u4fee\u590d\u5668\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u4f1a\u53ea\u4f7f\u7528 imports \u548c has_key \u4fee\u590d\u5668\u8fd0\u884c\uff1a<\/p>\n<p>$ 2to3 -f imports -f has_key example.py<br \/>\n\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u6267\u884c\u9664\u4e86 apply \u4e4b\u5916\u7684\u6240\u6709\u4fee\u590d\u5668\uff1a<\/p>\n<p>$ 2to3 -x apply example.py<br \/>\n\u6709\u4e00\u4e9b\u4fee\u590d\u5668\u662f\u9700\u8981 \u663e\u5f0f\u6307\u5b9a \u7684\uff0c\u5b83\u4eec\u9ed8\u8ba4\u4e0d\u4f1a\u6267\u884c\uff0c\u5fc5\u987b\u5728\u547d\u4ee4\u884c\u4e2d\u5217\u51fa\u624d\u4f1a\u6267\u884c\u3002\u6bd4\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\uff0c\u9664\u4e86\u9ed8\u8ba4\u7684\u4fee\u590d\u5668\u4ee5\u5916\uff0c\u8fd8\u4f1a\u6267\u884c idioms \u4fee\u590d\u5668\uff1a<\/p>\n<p>$ 2to3 -f all -f idioms example.py<br \/>\n\u6ce8\u610f\u8fd9\u91cc\u4f7f\u7528 all \u6765\u542f\u7528\u6240\u6709\u9ed8\u8ba4\u7684\u4fee\u590d\u5668\u3002<\/p>\n<p>\u6709\u4e9b\u60c5\u51b5\u4e0b 2to3 \u4f1a\u627e\u5230\u6e90\u7801\u4e2d\u6709\u4e00\u4e9b\u9700\u8981\u4fee\u6539\uff0c\u4f46\u662f\u65e0\u6cd5\u81ea\u52a8\u5904\u7406\u7684\u4ee3\u7801\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c2to3 \u4f1a\u5728\u5dee\u5f02\u5904\u4e0b\u9762\u6253\u5370\u4e00\u4e2a\u8b66\u544a\u4fe1\u606f\u3002\u4f60\u5e94\u8be5\u5b9a\u4f4d\u5230\u76f8\u5e94\u7684\u4ee3\u7801\u5e76\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\uff0c\u4ee5\u4f7f\u5176\u517c\u5bb9 Python 3.x\u3002<\/p>\n<p>2to3 \u4e5f\u53ef\u4ee5\u91cd\u6784 doctests\u3002\u4f7f\u7528 -d \u5f00\u542f\u8fd9\u4e2a\u6a21\u5f0f\u3002\u9700\u8981\u6ce8\u610f<em>\u53ea\u6709<\/em> doctests \u4f1a\u88ab\u91cd\u6784\u3002\u8fd9\u79cd\u6a21\u5f0f\u4e0b\u4e0d\u9700\u8981\u6587\u4ef6\u662f\u5408\u6cd5\u7684 Python \u4ee3\u7801\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0creST \u6587\u6863\u4e2d\u7c7b\u4f3c doctests \u7684\u793a\u4f8b\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u9009\u9879\u8fdb\u884c\u91cd\u6784\u3002<\/p>\n<p>-v \u9009\u9879\u53ef\u4ee5\u8f93\u51fa\u66f4\u591a\u8f6c\u6362\u7a0b\u5e8f\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p>\u7531\u4e8e\u67d0\u4e9b print \u8bed\u53e5\u53ef\u88ab\u89e3\u8bfb\u4e3a\u51fd\u6570\u8c03\u7528\u6216\u662f\u8bed\u53e5\uff0c2to3 \u5e76\u4e0d\u603b\u80fd\u8bfb\u53d6\u5305\u542b print \u51fd\u6570\u7684\u6587\u4ef6\u3002 \u5f53 2to3 \u68c0\u6d4b\u5230\u5b58\u5728 from <strong>future<\/strong> import print_function \u7f16\u8bd1\u5668\u6307\u4ee4\u65f6\uff0c\u4f1a\u4fee\u6539\u5176\u5185\u90e8\u8bed\u6cd5\u5c06 print() \u89e3\u8bfb\u4e3a\u51fd\u6570\u3002 \u8fd9\u4e00\u53d8\u52a8\u4e5f\u53ef\u4ee5\u4f7f\u7528 -p \u65d7\u6807\u624b\u52a8\u5f00\u542f\u3002 \u4f7f\u7528 -p \u6765\u4e3a\u5df2\u8f6c\u6362\u8fc7 print \u8bed\u53e5\u7684\u4ee3\u7801\u8fd0\u884c\u4fee\u590d\u5668\u3002 \u4e5f\u53ef\u4ee5\u4f7f\u7528 -e \u5c06 exec() \u89e3\u8bfb\u4e3a\u51fd\u6570\u3002<\/p>\n<p>-o \u6216 --output-dir \u9009\u9879\u53ef\u4ee5\u6307\u5b9a\u5c06\u8f6c\u6362\u540e\u7684\u6587\u4ef6\u5199\u5165\u5176\u4ed6\u76ee\u5f55\u4e2d\u3002\u7531\u4e8e\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u8986\u5199\u539f\u59cb\u6587\u4ef6\uff0c\u6240\u4ee5\u521b\u5efa\u526f\u672c\u6587\u4ef6\u6beb\u65e0\u610f\u4e49\uff0c\u56e0\u6b64\u4e5f\u9700\u8981\u4f7f\u7528 -n \u9009\u9879\u6765\u7981\u7528\u521b\u5efa\u526f\u672c\u3002<\/p>\n<p>3.2.3 \u65b0\u7248\u529f\u80fd: \u589e\u52a0\u4e86 -o \u9009\u9879\u3002<\/p>\n<p>-W \u6216 --write-unchanged-files \u9009\u9879\u7528\u6765\u544a\u8bc9 2to3 \u59cb\u7ec8\u9700\u8981\u8f93\u51fa\u6587\u4ef6\uff0c\u5373\u4f7f\u6ca1\u6709\u4efb\u4f55\u6539\u52a8\u3002\u8fd9\u5728\u4f7f\u7528 -o \u53c2\u6570\u65f6\u5341\u5206\u6709\u7528\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u6574\u4e2a Python \u6e90\u7801\u5305\u5b8c\u6574\u5730\u8f6c\u6362\u5230\u53e6\u4e00\u4e2a\u76ee\u5f55\u3002\u8fd9\u4e2a\u9009\u9879\u9690\u542b\u4e86 -w \u9009\u9879\uff0c\u5426\u5219\u7b49\u4e8e\u6ca1\u6709\u4f5c\u7528\u3002<\/p>\n<p>3.2.3 \u65b0\u7248\u529f\u80fd: \u589e\u52a0\u4e86 -W \u9009\u9879\u3002<\/p>\n<p>--add-suffix \u9009\u9879\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u7528\u6765\u4f5c\u4e3a\u540e\u7f00\u9644\u52a0\u5728\u8f93\u51fa\u6587\u4ef6\u540d\u540e\u9762\u7684\u540e\u9762\u3002\u7531\u4e8e\u5199\u5165\u7684\u6587\u4ef6\u540d\u4e0e\u539f\u59cb\u6587\u4ef6\u4e0d\u540c\uff0c\u6240\u4ee5\u6ca1\u6709\u5fc5\u8981\u521b\u5efa\u526f\u672c\uff0c\u56e0\u6b64 -n \u9009\u9879\u4e5f\u662f\u5fc5\u8981\u7684\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<p>$ 2to3 -n -W --add-suffix=3 example.py<br \/>\n\u8fd9\u6837\u4f1a\u628a\u8f6c\u6362\u540e\u7684\u6587\u4ef6\u5199\u5165 example.py3 \u6587\u4ef6\u3002<\/p>\n<p>3.2.3 \u65b0\u7248\u529f\u80fd: \u589e\u52a0\u4e86 --add-suffix \u9009\u9879\u3002<\/p>\n<p>\u5c06\u6574\u4e2a\u9879\u76ee\u4ece\u4e00\u4e2a\u76ee\u5f55\u8f6c\u6362\u5230\u53e6\u4e00\u4e2a\u76ee\u5f55\u53ef\u4ee5\u7528\u8fd9\u6837\u7684\u547d\u4ee4\uff1a<\/p>\n<p>$ 2to3 --output-dir=python3-version\/mycode -W -n python2-version\/mycode<br \/>\n\u4fee\u590d\u5668<br \/>\n\u8f6c\u6362\u4ee3\u7801\u7684\u6bcf\u4e00\u4e2a\u6b65\u9aa4\u90fd\u5c01\u88c5\u5728\u4fee\u590d\u5668\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528 2to3 -l \u6765\u5217\u51fa\u53ef\u7528\u7684\u4fee\u590d\u5668\u3002\u4e4b\u524d\u5df2\u7ecf\u63d0\u5230\uff0c\u6bcf\u4e2a\u4fee\u590d\u5668\u90fd\u53ef\u4ee5\u72ec\u7acb\u5730\u6253\u5f00\u6216\u662f\u5173\u95ed\u3002\u4e0b\u9762\u4f1a\u5bf9\u5404\u4e2a\u4fee\u590d\u5668\u505a\u66f4\u8be6\u7ec6\u7684\u63cf\u8ff0\u3002<\/p>\n<p>apply<br \/>\n\u79fb\u9664\u5bf9 apply() \u7684\u4f7f\u7528\uff0c\u4e3e\u4f8b\u6765\u8bf4\uff0capply(function, *args, *<em>kwargs) \u4f1a\u88ab\u8f6c\u6362\u6210 function(<\/em>args, **kwargs)\u3002<\/p>\n<p>asserts<br \/>\n\u5c06\u5df2\u5f03\u7528\u7684 unittest \u65b9\u6cd5\u66ff\u6362\u4e3a\u6b63\u786e\u7684\u3002<\/p>\n<p>Python 2.x<\/p>\n<p>Python 3.x<\/p>\n<p>failUnlessEqual(a, b)<\/p>\n<p>assertEqual(a, b)<\/p>\n<p>assertEquals(a, b)<\/p>\n<p>assertEqual(a, b)<\/p>\n<p>failIfEqual(a, b)<\/p>\n<p>assertNotEqual(a, b)<\/p>\n<p>assertNotEquals(a, b)<\/p>\n<p>assertNotEqual(a, b)<\/p>\n<p>failUnless(a)<\/p>\n<p>assertTrue(a)<\/p>\n<p>assert_(a)<\/p>\n<p>assertTrue(a)<\/p>\n<p>failIf(a)<\/p>\n<p>assertFalse(a)<\/p>\n<p>failUnlessRaises(exc, cal)<\/p>\n<p>assertRaises(exc, cal)<\/p>\n<p>failUnlessAlmostEqual(a, b)<\/p>\n<p>assertAlmostEqual(a, b)<\/p>\n<p>assertAlmostEquals(a, b)<\/p>\n<p>assertAlmostEqual(a, b)<\/p>\n<p>failIfAlmostEqual(a, b)<\/p>\n<p>assertNotAlmostEqual(a, b)<\/p>\n<p>assertNotAlmostEquals(a, b)<\/p>\n<p>assertNotAlmostEqual(a, b)<\/p>\n<p>basestring<br \/>\n\u5c06 basestring \u8f6c\u6362\u4e3a str\u3002<\/p>\n<p>buffer<br \/>\n\u5c06 buffer \u8f6c\u6362\u4e3a memoryview\u3002\u8fd9\u4e2a\u4fee\u590d\u5668\u662f\u53ef\u9009\u7684\uff0c\u56e0\u4e3a memoryview API \u548c buffer \u5f88\u76f8\u4f3c\uff0c\u4f46\u4e0d\u5b8c\u5168\u4e00\u6837\u3002<\/p>\n<p>dict<br \/>\n\u4fee\u590d\u5b57\u5178\u8fed\u4ee3\u65b9\u6cd5\u3002dict.iteritems() \u4f1a\u8f6c\u6362\u6210 dict.items()\uff0cdict.iterkeys() \u4f1a\u8f6c\u6362\u6210 dict.keys()\uff0cdict.itervalues() \u4f1a\u8f6c\u6362\u6210 dict.values()\u3002\u7c7b\u4f3c\u7684\uff0cdict.viewitems()\uff0cdict.viewkeys() \u548c dict.viewvalues() \u4f1a\u5206\u522b\u8f6c\u6362\u6210 dict.items()\uff0cdict.keys() \u548c dict.values()\u3002\u53e6\u5916\u4e5f\u4f1a\u5c06\u539f\u6709\u7684 dict.items()\uff0cdict.keys() \u548c dict.values() \u65b9\u6cd5\u8c03\u7528\u7528 list \u5305\u88c5\u4e00\u5c42\u3002<\/p>\n<p>except<br \/>\n\u5c06 except X, T \u8f6c\u6362\u4e3a except X as T\u3002<\/p>\n<p>exec<br \/>\n\u5c06 exec \u8bed\u53e5\u8f6c\u6362\u4e3a exec() \u51fd\u6570\u8c03\u7528\u3002<\/p>\n<p>execfile<br \/>\n\u79fb\u9664 execfile() \u7684\u4f7f\u7528\u3002execfile() \u7684\u5b9e\u53c2\u4f1a\u4f7f\u7528 open()\uff0ccompile() \u548c exec() \u5305\u88c5\u3002<\/p>\n<p>exitfunc<br \/>\n\u5c06\u5bf9 sys.exitfunc \u7684\u8d4b\u503c\u6539\u4e3a\u4f7f\u7528 atexit \u6a21\u5757\u4ee3\u66ff\u3002<\/p>\n<p>filter<br \/>\n\u5c06 filter() \u51fd\u6570\u7528 list \u5305\u88c5\u4e00\u5c42\u3002<\/p>\n<p>funcattrs<br \/>\n\u4fee\u590d\u5df2\u7ecf\u91cd\u547d\u540d\u7684\u51fd\u6570\u5c5e\u6027\u3002\u6bd4\u5982 my_function.func_closure \u4f1a\u88ab\u8f6c\u6362\u4e3a my_function.<strong>closure<\/strong>\u3002<\/p>\n<p>future<br \/>\n\u79fb\u9664 from <strong>future<\/strong> import new_feature \u8bed\u53e5\u3002<\/p>\n<p>getcwdu<br \/>\n\u5c06 os.getcwdu() \u91cd\u547d\u540d\u4e3a os.getcwd()\u3002<\/p>\n<p>has_key<br \/>\n\u5c06 dict.has_key(key) \u8f6c\u6362\u4e3a key in dict\u3002<\/p>\n<p>idioms<br \/>\n\u8fd9\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u4fee\u590d\u5668\uff0c\u4f1a\u8fdb\u884c\u591a\u79cd\u8f6c\u6362\uff0c\u5c06 Python \u4ee3\u7801\u53d8\u6210\u66f4\u52a0\u5e38\u89c1\u7684\u5199\u6cd5\u3002\u7c7b\u4f3c type(x) is SomeClass \u548c type(x) == SomeClass \u7684\u7c7b\u578b\u5bf9\u6bd4\u4f1a\u88ab\u8f6c\u6362\u6210 isinstance(x, SomeClass)\u3002while 1 \u8f6c\u6362\u6210 while True\u3002\u8fd9\u4e2a\u4fee\u590d\u5668\u8fd8\u4f1a\u5728\u5408\u9002\u7684\u5730\u65b9\u4f7f\u7528 sorted() \u51fd\u6570\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u8fd9\u6837\u7684\u4ee3\u7801\u5757\uff1a<\/p>\n<p>L = list(some_iterable)<br \/>\nL.sort()<br \/>\n\u4f1a\u88ab\u8f6c\u6362\u4e3a\uff1a<\/p>\n<p>L = sorted(some_iterable)<br \/>\nimport<br \/>\n\u68c0\u6d4b sibling imports\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u6210\u76f8\u5bf9 import\u3002<\/p>\n<p>imports<br \/>\n\u5904\u7406\u6807\u51c6\u5e93\u6a21\u5757\u7684\u91cd\u547d\u540d\u3002<\/p>\n<p>imports2<br \/>\n\u5904\u7406\u6807\u51c6\u5e93\u4e2d\u5176\u4ed6\u6a21\u5757\u7684\u91cd\u547d\u540d\u3002\u8fd9\u4e2a\u4fee\u590d\u5668\u7531\u4e8e\u4e00\u4e9b\u6280\u672f\u4e0a\u7684\u9650\u5236\uff0c\u56e0\u6b64\u548c imports \u62c6\u5206\u5f00\u4e86\u3002<\/p>\n<p>input<br \/>\n\u5c06 input(prompt) \u8f6c\u6362\u4e3a eval(input(prompt))\u3002<\/p>\n<p>intern<br \/>\n\u5c06 intern() \u8f6c\u6362\u4e3a sys.intern()\u3002<\/p>\n<p>isinstance<br \/>\n\u4fee\u590d isinstance() \u51fd\u6570\u7b2c\u4e8c\u4e2a\u5b9e\u53c2\u4e2d\u91cd\u590d\u7684\u7c7b\u578b\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0cisinstance(x, (int, int)) \u4f1a\u8f6c\u6362\u4e3a isinstance(x, int), isinstance(x, (int, float, int)) \u4f1a\u8f6c\u6362\u4e3a isinstance(x, (int, float))\u3002<\/p>\n<p>itertools_imports<br \/>\n\u79fb\u9664 itertools.ifilter()\uff0citertools.izip() \u4ee5\u53ca itertools.imap() \u7684 import\u3002\u5bf9 itertools.ifilterfalse() \u7684 import \u4e5f\u4f1a\u66ff\u6362\u6210 itertools.filterfalse()\u3002<\/p>\n<p>itertools<br \/>\n\u4fee\u6539 itertools.ifilter()\uff0citertools.izip() \u548c itertools.imap() \u7684\u8c03\u7528\u4e3a\u5bf9\u5e94\u7684\u5185\u5efa\u5b9e\u73b0\u3002itertools.ifilterfalse() \u4f1a\u66ff\u6362\u6210 itertools.filterfalse()\u3002<\/p>\n<p>long<br \/>\n\u5c06 long \u91cd\u547d\u540d\u4e3a int\u3002<\/p>\n<p>map<br \/>\n\u7528 list \u5305\u88c5 map()\u3002\u540c\u65f6\u4e5f\u4f1a\u5c06 map(None, x) \u66ff\u6362\u4e3a list(x)\u3002\u4f7f\u7528 from future_builtins import map \u7981\u7528\u8fd9\u4e2a\u4fee\u590d\u5668\u3002<\/p>\n<p>metaclass<br \/>\n\u5c06\u8001\u7684\u5143\u7c7b\u8bed\u6cd5\uff08\u7c7b\u4f53\u4e2d\u7684 <strong>metaclass<\/strong> = Meta\uff09\u66ff\u6362\u4e3a\u65b0\u7684\uff08class X(metaclass=Meta)\uff09\u3002<\/p>\n<p>methodattrs<br \/>\n\u4fee\u590d\u8001\u7684\u65b9\u6cd5\u5c5e\u6027\u540d\u3002\u4f8b\u5982 meth.im_func \u4f1a\u88ab\u8f6c\u6362\u4e3a meth.<strong>func<\/strong>\u3002<\/p>\n<p>ne<br \/>\n\u8f6c\u6362\u8001\u7684\u4e0d\u7b49\u8bed\u6cd5\uff0c\u5c06 &lt;&gt; \u8f6c\u4e3a !=\u3002<\/p>\n<p>next<br \/>\n\u5c06\u8fed\u4ee3\u5668\u7684 next() \u65b9\u6cd5\u8c03\u7528\u8f6c\u4e3a next() \u51fd\u6570\u3002\u4e5f\u4f1a\u5c06 next() \u65b9\u6cd5\u91cd\u547d\u540d\u4e3a <strong>next<\/strong>()\u3002<\/p>\n<p>nonzero<br \/>\n\u5c06 <strong>nonzero<\/strong>() \u8f6c\u6362\u4e3a <strong>bool<\/strong>()\u3002<\/p>\n<p>numliterals<br \/>\n\u5c06\u516b\u8fdb\u5236\u5b57\u9762\u91cf\u8f6c\u4e3a\u65b0\u7684\u8bed\u6cd5\u3002<\/p>\n<p>operator<br \/>\n\u5c06 operator \u6a21\u5757\u4e2d\u7684\u8bb8\u591a\u65b9\u6cd5\u8c03\u7528\u8f6c\u4e3a\u5176\u4ed6\u7684\u7b49\u6548\u51fd\u6570\u8c03\u7528\u3002\u5982\u679c\u6709\u9700\u8981\uff0c\u4f1a\u6dfb\u52a0\u9002\u5f53\u7684 import \u8bed\u53e5\uff0c\u6bd4\u5982 import collections.abc\u3002\u6709\u4ee5\u4e0b\u8f6c\u6362\u6620\u5c04\uff1a<\/p>\n<p>Python 2.x<\/p>\n<p>Python 3.x<\/p>\n<p>operator.isCallable(obj)<\/p>\n<p>callable(obj)<\/p>\n<p>operator.sequenceIncludes(obj)<\/p>\n<p>operator.contains(obj)<\/p>\n<p>operator.isSequenceType(obj)<\/p>\n<p>isinstance(obj, collections.abc.Sequence)<\/p>\n<p>operator.isMappingType(obj)<\/p>\n<p>isinstance(obj, collections.abc.Mapping)<\/p>\n<p>operator.isNumberType(obj)<\/p>\n<p>isinstance(obj, numbers.Number)<\/p>\n<p>operator.repeat(obj, n)<\/p>\n<p>operator.mul(obj, n)<\/p>\n<p>operator.irepeat(obj, n)<\/p>\n<p>operator.imul(obj, n)<\/p>\n<p>paren<br \/>\n\u5728\u5217\u8868\u751f\u6210\u5f0f\u4e2d\u589e\u52a0\u5fc5\u987b\u7684\u62ec\u53f7\u3002\u4f8b\u5982\u5c06 [x for x in 1, 2] \u8f6c\u6362\u4e3a [x for x in (1, 2)]\u3002<\/p>\n<p>print<br \/>\n\u5c06 print \u8bed\u53e5\u8f6c\u6362\u4e3a print() \u51fd\u6570\u3002<\/p>\n<p>raise<br \/>\n\u5c06 raise E, V \u8f6c\u6362\u4e3a raise E(V)\uff0c\u5c06 raise E, V, T \u8f6c\u6362\u4e3a raise E(V).with_traceback(T)\u3002\u5982\u679c E \u662f\u5143\u7ec4\uff0c\u8fd9\u6837\u7684\u8f6c\u6362\u662f\u4e0d\u6b63\u786e\u7684\uff0c\u56e0\u4e3a\u7528\u5143\u7ec4\u4ee3\u66ff\u5f02\u5e38\u7684\u505a\u6cd5\u5728 3.0 \u4e2d\u5df2\u7ecf\u79fb\u9664\u4e86\u3002<\/p>\n<p>raw_input<br \/>\n\u5c06 raw_input() \u8f6c\u6362\u4e3a input()\u3002<\/p>\n<p>reduce<br \/>\n\u5c06 reduce() \u8f6c\u6362\u4e3a functools.reduce()\u3002<\/p>\n<p>reload<br \/>\n\u5c06 reload() \u8f6c\u6362\u4e3a importlib.reload()\u3002<\/p>\n<p>renames<br \/>\n\u5c06 sys.maxint \u8f6c\u6362\u4e3a sys.maxsize\u3002<\/p>\n<p>repr<br \/>\n\u5c06\u53cd\u5f15\u53f7 repr \u8868\u8fbe\u5f0f\u66ff\u6362\u4e3a repr() \u51fd\u6570\u3002<\/p>\n<p>set_literal<br \/>\n\u5c06 set \u6784\u9020\u51fd\u6570\u66ff\u6362\u4e3a set literals \u5199\u6cd5\u3002\u8fd9\u4e2a\u4fee\u590d\u5668\u662f\u53ef\u9009\u7684\u3002<\/p>\n<p>standarderror<br \/>\n\u5c06 StandardError \u91cd\u547d\u540d\u4e3a Exception\u3002<\/p>\n<p>sys_exc<br \/>\n\u5c06\u5f03\u7528\u7684 sys.exc_value\uff0csys.exc_type\uff0csys.exc_traceback \u66ff\u6362\u4e3a sys.exc_info() \u7684\u7528\u6cd5\u3002<\/p>\n<p>throw<br \/>\n\u4fee\u590d\u751f\u6210\u5668\u7684 throw() \u65b9\u6cd5\u7684 API \u53d8\u66f4\u3002<\/p>\n<p>tuple_params<br \/>\n\u79fb\u9664\u9690\u5f0f\u7684\u5143\u7ec4\u53c2\u6570\u89e3\u5305\u3002\u8fd9\u4e2a\u4fee\u590d\u5668\u4f1a\u63d2\u5165\u4e34\u65f6\u53d8\u91cf\u3002<\/p>\n<p>types<br \/>\n\u4fee\u590d type \u6a21\u5757\u4e2d\u4e00\u4e9b\u6210\u5458\u7684\u79fb\u9664\u5f15\u8d77\u7684\u4ee3\u7801\u95ee\u9898\u3002<\/p>\n<p>unicode<br \/>\n\u5c06 unicode \u91cd\u547d\u540d\u4e3a str\u3002<\/p>\n<p>urllib<br \/>\n\u5c06 urllib \u548c urllib2 \u91cd\u547d\u540d\u4e3a urllib \u5305\u3002<\/p>\n<p>ws_comma<br \/>\n\u79fb\u9664\u9017\u53f7\u5206\u9694\u7684\u5143\u7d20\u4e4b\u95f4\u591a\u4f59\u7684\u7a7a\u767d\u3002\u8fd9\u4e2a\u4fee\u590d\u5668\u662f\u53ef\u9009\u7684\u3002<\/p>\n<p>xrange<br \/>\n\u5c06 xrange() \u91cd\u547d\u540d\u4e3a range()\uff0c\u5e76\u7528 list \u5305\u88c5\u539f\u6709\u7684 range()\u3002<\/p>\n<p>xreadlines<br \/>\n\u5c06 for x in file.xreadlines() \u8f6c\u6362\u4e3a for x in file\u3002<\/p>\n<p>zip<br \/>\n\u7528 list \u5305\u88c5 zip()\u3002\u5982\u679c\u4f7f\u7528\u4e86 from future_builtins import zip \u7684\u8bdd\u4f1a\u7981\u7528\u3002<\/p>\n<p>lib2to3 \u2014\u2014 2to3 \u652f\u6301\u5e93<br \/>\n\u6e90\u4ee3\u7801\uff1a Lib\/lib2to3\/<\/p>\n<p>3.10 \u7248\u540e\u5df2\u79fb\u9664: Python 3.9 \u5c06\u5207\u6362\u5230 PEG \u89e3\u6790\u5668 (\u53c2\u89c1 PEP 617)\uff0cPython 3.10 \u53ef\u80fd\u4f1a\u5305\u542b lib2to3 \u7684 LL(1) \u89e3\u6790\u5668\u6240\u4e0d\u80fd\u89e3\u6790\u7684\u65b0\u8bed\u6cd5\u3002 lib2to3 \u6a21\u5757\u53ef\u80fd\u4f1a\u5728\u672a\u6765\u7684 Python \u7248\u672c\u4e2d\u88ab\u79fb\u51fa\u6807\u51c6\u5e93\u3002 \u8bf7\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u66ff\u4ee3\u4f8b\u5982 LibCST \u6216 parso\u3002<\/p>\n<p>\u6ce8\u89e3 lib2to3 API \u5e76\u4e0d\u7a33\u5b9a\uff0c\u5e76\u53ef\u80fd\u5728\u672a\u6765\u5927\u5e45\u4fee\u6539\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7ebfpython2\u8f6c\u6362python3\u5728\u7ebfhttps:\/\/360techexplorer.com\/tools\/p [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[583,585,584,582],"class_list":["post-1041","post","type-post","status-publish","format-standard","hentry","category-python","tag-2to3","tag-python2","tag-python2python3"],"_links":{"self":[{"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/posts\/1041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1041"}],"version-history":[{"count":0,"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/posts\/1041\/revisions"}],"wp:attachment":[{"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}