{"id":330,"date":"2019-01-28T21:35:37","date_gmt":"2019-01-28T13:35:37","guid":{"rendered":"http:\/\/www.yumefx.com\/?p=330"},"modified":"2021-03-04T16:18:55","modified_gmt":"2021-03-04T08:18:55","slug":"python3-x%e4%b8%8epython2-x%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/www.yumefx.com\/?p=330","title":{"rendered":"Python3.X\u4e0ePython2.X\u7684\u533a\u522b"},"content":{"rendered":"\n<p>\u867d\u7136\u5f88\u591aCG\u5236\u4f5c\u8f6f\u4ef6\u8fd8\u7528\u7740Python2.7\uff08\u9664\u4e86\u5f00\u6e90\u7684Blender\u7528\u4e0a\u4e863.5\uff09\uff0c\u4f46\u6211\u8fd8\u662f\u51b3\u5b9a\u8f6c\u5411Python 3\u4e86\u3002\u4eca\u5929\u628a\u4e24\u4e2a\u7248\u672c\u4e3b\u8981\u7684\u4e00\u4e9b\u533a\u522b\u653e\u5728\u8fd9\u91cc\uff1a<\/p>\n<ul>\n<li>print\u51fd\u6570<\/li>\n<li>\u6574\u6570\u76f8\u9664<\/li>\n<li>Unicode<\/li>\n<li>\u5f02\u5e38\u5904\u7406<\/li>\n<li>xrange<\/li>\n<li>map\u51fd\u6570<\/li>\n<li>\u4e0d\u652f\u6301has_key<\/li>\n<\/ul>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">print\u51fd\u6570<\/h1>\n<p>Python 2\u4e2dprint\u662f\u8bed\u53e5\uff08statement\uff09\uff0cPython 3\u4e2dprint\u5219\u53d8\u6210\u4e86\u51fd\u6570\u3002\u5728Python 3\u4e2d\u8c03\u7528print\u9700\u8981\u52a0\u4e0a\u62ec\u53f7\uff0c\u4e0d\u52a0\u62ec\u53f7\u4f1a\u62a5SyntaxError<\/p>\n<h3>\u00a0<\/h3>\n<h3>Python 2<\/h3>\n\n\n\n<pre>\n<span>print &quot;hello world&quot;<\/span>\n<span>\/\/ hello world<\/span>\n<\/pre>\n\n\n\n<h3>\u00a0<\/h3>\n<h3>Python 3<\/h3>\n\n\n\n<pre>\n<span>print(&quot;hello world&quot;)<\/span>\n<span>\/\/ hello world<\/span>\n<span><\/span>\n<span>print &quot;hello world&quot;<\/span>\n<span>\/\/ File &quot;&lt;stdin&gt;&quot;, line 1<\/span>\n<span>\/\/      print &quot;hello world&quot;<\/span>\n<span>\/\/                              ^<\/span>\n<span>\/\/ SyntaxError: Missing parentheses in call to &#x27;print&#x27;<\/span>\n<\/pre>\n\n\n\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">\u6574\u6570\u76f8\u9664<\/h1>\n<p>\u5728Python 2\u4e2d\uff0c3\/2\u7684\u7ed3\u679c\u662f\u6574\u6570\uff0c\u5728Python 3\u4e2d\uff0c\u7ed3\u679c\u5219\u662f\u6d6e\u70b9\u6570\u3002<\/p>\n<h3>\u00a0<\/h3>\n<h3>Python 2<\/h3>\n\n\n\n<pre>\n<span>print &#x27;3 \/ 2 =&#x27;, 3 \/ 2<\/span>\n<span>print &#x27;3 \/ 2.0 =&#x27;, 3 \/ 2.0<\/span>\n<span>\/\/ 3 \/ 2 = 1<\/span>\n<span>\/\/ 3 \/ 2.0 = 1.5<\/span>\n<\/pre>\n\n\n\n<h3>\u00a0<\/h3>\n<h3>Python 3<\/h3>\n\n\n\n<pre>\n<span>print(&#x27;3 \/ 2 =&#x27;, 3 \/ 2)<\/span>\n<span>print(&#x27;3 \/ 2.0 =&#x27;, 3 \/ 2.0)<\/span>\n<span>\/\/ 3 \/ 2 = 1.5<\/span>\n<span>\/\/ 3 \/ 2.0 = 1.5<\/span>\n<\/pre>\n\n\n\n<p>\u8fd9\u91cc\u8865\u5145\u4e00\u4e0b\uff0cPython3\u4e2d\u6574\u9664\u53ef\u4ee5\u4f7f\u7528<code>\/\/<\/code>\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">Unicode<\/h1>\n<p>Python 2\u6709\u4e24\u79cd\u5b57\u7b26\u4e32\u7c7b\u578b\uff1astr\u548cunicode\uff0cPython 3\u4e2d\u7684\u5b57\u7b26\u4e32\u9ed8\u8ba4\u5c31\u662fUnicode\uff0cPython 3\u4e2d\u7684str\u76f8\u5f53\u4e8ePython 2\u4e2d\u7684unicode\u3002<\/p>\n<p>\u5728Python 2\u4e2d\uff0c\u5982\u679c\u4ee3\u7801\u4e2d\u5305\u542b\u975e\u82f1\u6587\u5b57\u7b26\uff0c\u9700\u8981\u5728\u4ee3\u7801\u6587\u4ef6\u7684\u6700\u5f00\u59cb\u58f0\u660e\u7f16\u7801\uff0c\u5982\u4e0b<\/p>\n\n\n\n<pre>\n<span># -*- coding: utf-8 -*-<\/span>\n<\/pre>\n\n\n\n<p>\u5728Python 3\u4e2d\uff0c\u9ed8\u8ba4\u7684\u5b57\u7b26\u4e32\u5c31\u662fUnicode\uff0c\u5c31\u7701\u53bb\u4e86\u8fd9\u4e2a\u9ebb\u70e6\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5728Python 3\u53ef\u4ee5\u6b63\u5e38\u5730\u8fd0\u884c\u3002<\/p>\n\n\n\n<pre>\n<span>a = &quot;\u4f60\u597d&quot;<\/span>\n<span>print(a)<\/span>\n<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u53ef\u4ee5\u53c2\u8003\u4e4b\u524d\u7684\u535a\u6587<a href=\"https:\/\/www.yumefx.com\/?p=308\">\u5173\u4e8e\u5b57\u7b26\u7f16\u7801<\/a>\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">\u5f02\u5e38\u5904\u7406<\/h1>\n<p>Python 2\u4e2d\u6355\u83b7\u5f02\u5e38\u4e00\u822c\u7528\u4e0b\u9762\u7684\u8bed\u6cd5<\/p>\n\n\n\n<pre>\n<span>try:<\/span>\n<span>    1\/0 <\/span>\n<span>except ZeroDivisionError, e:<\/span>\n<span>    print str(e)<\/span>\n<span># \u6216\u8005<\/span>\n<span>try:<\/span>\n<span>    1\/0 <\/span>\n<span>except ZeroDivisionError as e:<\/span>\n<span>    print str(e)<\/span>\n<\/pre>\n\n\n\n<p>Python 3\u4e2d\u4e0d\u518d\u652f\u6301\u524d\u4e00\u79cd\u8bed\u6cd5\uff0c\u5fc5\u987b\u4f7f\u7528as\u5173\u952e\u5b57\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">xrange<\/h1>\n<p>Python 2\u4e2d\u6709 range \u548c xrange \u4e24\u4e2a\u65b9\u6cd5\u3002<\/p>\n<p>\u5176\u533a\u522b\u5728\u4e8e\uff0crange\u8fd4\u56de\u4e00\u4e2alist\uff0c\u5728\u88ab\u8c03\u7528\u7684\u65f6\u5019\u5373\u8fd4\u56de\u6574\u4e2a\u5e8f\u5217\uff1bxrange\u8fd4\u56de\u4e00\u4e2aiterator\uff0c\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\u751f\u6210\u5e8f\u5217\u7684\u4e0b\u4e00\u4e2a\u6570\u5b57\u3002<\/p>\n<p>Python 3\u4e2d\u4e0d\u518d\u652f\u6301 xrange \u65b9\u6cd5\uff0cPython 3\u4e2d\u7684 range \u65b9\u6cd5\u5c31\u76f8\u5f53\u4e8e Python 2\u4e2d\u7684 xrange \u65b9\u6cd5\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">map\u51fd\u6570<\/h1>\n<p>\u5728Python 2\u4e2d\uff0cmap\u51fd\u6570\u8fd4\u56delist\uff0c\u800c\u5728Python 3\u4e2d\uff0cmap\u51fd\u6570\u8fd4\u56deiterator\u3002<\/p>\n<h3>\u00a0<\/h3>\n<h3>Python 2<\/h3>\n\n\n\n<pre>\n<span>map(lambda x: x+1, range(5))<\/span>\n<span>\/\/ [1, 2, 3, 4, 5]<\/span>\n<\/pre>\n\n\n\n<h3>\u00a0<\/h3>\n<h3>Python 3<\/h3>\n\n\n\n<pre>\n<span>map(lambda x: x+1, range(5))<\/span>\n<span>\/\/ &lt;map object at 0x7ff5b103d2b0&gt;<\/span>\n<span><\/span>\n<span>list(map(lambda x: x+1, range(5)))<\/span>\n<span>\/\/ [1, 2, 3, 4, 5]<\/span>\n<\/pre>\n\n\n\n<p>filter\u51fd\u6570\u5728Python 2\u548cPython 3\u4e2d\u4e5f\u662f\u540c\u6837\u7684\u533a\u522b\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">\u4e0d\u652f\u6301has_key<\/h1>\n<p>Python 3\u4e2d\u7684\u5b57\u5178\u4e0d\u518d\u652f\u6301has_key\u65b9\u6cd5<\/p>\n<h3>\u00a0<\/h3>\n<h3>Python 2<\/h3>\n\n\n\n<pre>\n<span>person = {&quot;age&quot;: 30, &quot;name&quot;: &quot;Xiao Wang&quot;}<\/span>\n<span>print &quot;person has key \\&quot;age\\&quot;: &quot;, person.has_key(&quot;age&quot;)<\/span>\n<span>print &quot;person has key \\&quot;age\\&quot;: &quot;, &quot;age&quot; in person<\/span>\n<span><\/span>\n<span>\/\/ person has key &quot;age&quot;:  True<\/span>\n<span>\/\/ person has key &quot;age&quot;:  True<\/span>\n<\/pre>\n\n\n\n<h3>\u00a0<\/h3>\n<h3>Python 3<\/h3>\n\n\n\n<pre>\n<span>person = {&quot;age&quot;: 30, &quot;name&quot;: &quot;Xiao Wang&quot;}<\/span>\n<span>print(&quot;person has key \\&quot;age\\&quot;: &quot;, &quot;age&quot; in person)<\/span>\n<span>\/\/ person has key &quot;age&quot;:  True<\/span>\n<span><\/span>\n<span>print(&quot;person has key \\&quot;age\\&quot;: &quot;, person.has_key(&quot;age&quot;))<\/span>\n<span>\/\/ Traceback (most recent call last):<\/span>\n<span>\/\/   File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;<\/span>\n<span>\/\/ AttributeError: &#x27;dict&#x27; object has no attribute &#x27;has_key&#x27;<\/span>\n<\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u8f6c\u8f7d\u81ea\u77e5\u4e4e<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/34751635\">\u5f3a\u54e5<\/a>\u3002<\/p>\n<p>\u00a0<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5373\u4f7f\u662f\u9a6c\u620f\u56e2\u7684\u72ee\u5b50\uff0c<br \/>\u4e5f\u4f1a\u56e0\u4e3a\u6015\u97ad\u6253\u800c\u5b66\u4f1a\u5750\u5728\u6905\u5b50\u4e0a\uff0c<br \/>\u4f46\u8fd9\u662f\u201c\u826f\u597d\u7684\u8bad\u7ec3\u201d\uff0c<br \/>\u800c\u4e0d\u662f\u201c\u826f\u597d\u7684\u6559\u80b2\u201d\u3002<\/p>\n<p style=\"text-align: right;\">\u300a\u4e09\u50bb\u5927\u95f9\u5b9d\u83b1\u575e\u300b<\/p>\n<p style=\"text-align: right;\">\u2014\u2014\u62c9\u5e93\u9a6c\u00b7\u5e0c\u62c9\u5c3c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u867d\u7136\u5f88\u591aCG\u5236\u4f5c\u8f6f\u4ef6\u8fd8\u7528\u7740Python2.7\uff08\u9664\u4e86\u5f00\u6e90\u7684Blender\u7528\u4e0a\u4e863.5\uff09\uff0c\u4f46\u6211\u8fd8\u662f\u51b3\u5b9a\u8f6c\u5411Pyth [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":922,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,4],"tags":[37,15],"_links":{"self":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts\/330"}],"collection":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=330"}],"version-history":[{"count":0,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts\/330\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/media\/922"}],"wp:attachment":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}