{"id":171569,"date":"2020-03-12T09:15:43","date_gmt":"2020-03-12T01:15:43","guid":{"rendered":"https:\/\/gulass.cn\/?p=171569"},"modified":"2020-03-02T10:18:19","modified_gmt":"2020-03-02T02:18:19","slug":"python-3-8-2020","status":"publish","type":"post","link":"https:\/\/gulass.cn\/python-3-8-2020.html","title":{"rendered":"2020\u65b0\u770b\u70b9\uff1aPython 3.8\u516d\u5927\u65b0\u529f\u80fd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd1\u671f\uff0cPython 3.8\u5df2\u7ecf\u53d1\u5e03\u3002 \u4e0d\u8fc7\u522b\u62c5\u5fc3\uff0c\u5c0f\u82af\u5df2\u7ecf\u4e3a\u4f60\u603b\u7ed3\u597d\u4e86Python 3.8\u4e2d\u6bcf\u4e2a\u65b0\u624b\u90fd\u5e94\u8be5\u638c\u63e1\u76846\u5927\u65b0\u529f\u80fd\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

IT\u4e1a\u65e5\u65b0\u6708\u5f02\uff0c\u843d\u540e\u7684Python 2\u5c06\u57282020\u5e74\u9010\u6e10\u88ab\u6dd8\u6c70\u3002<\/p>\n

\"\"<\/p>\n

2019\u5e74\uff0c\u968f\u7740\u6570\u636e\u79d1\u5b66\u7684\u5174\u8d77\uff0cPython\u6210\u4e3a\u4e86\u6700\u70ed\u95e8\u7684\u7a0b\u5e8f\u8bed\u8a00\u3002 \u5c3d\u7ba1\u5982\u6b64\uff0c\u9762\u5bf9\u6d69\u5982\u70df\u6d77\u7684\u77e5\u8bc6\uff0c\u5e94\u63a5\u4e0d\u6687\u4e5f\u662f\u6b63\u5e38\u7684\u3002\u8bed\u6cd5\u4e0d\u65ad\u53d8\u5316\uff0c\u6bcf\u6b21Python\u66f4\u65b0\uff0c\u90fd\u4f1a\u6dfb\u52a0\u8bb8\u591a\u65b0\u7684\u8868\u8fbe\u3002\u6709\u5f88\u591a\u529f\u80fd\u5c0f\u82af\u53ea\u6068\u81ea\u5df1\u77e5\u9053\u5f97\u592a\u665a\u4e86\u3002 \u5982\u679c\u8bfb\u8005\u4e5f\u611f\u540c\u8eab\u53d7\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u574f\u6d88\u606f\uff1a\u8fd1\u671f\uff0cPython 3.8\u5df2\u7ecf\u53d1\u5e03\u3002 \u4e0d\u8fc7\u522b\u62c5\u5fc3\uff0c\u5c0f\u82af\u5df2\u7ecf\u4e3a\u4f60\u603b\u7ed3\u597d\u4e86Python 3.8\u4e2d\u6bcf\u4e2a\u65b0\u624b\u90fd\u5e94\u8be5\u638c\u63e1\u76846\u5927\u65b0\u529f\u80fd\u3002<\/p>\n

1. \u65b0\u6a21\u5757\uff1a\u8bfb\u53d6\u5143\u6570\u636e\u5386<\/strong><\/span><\/div>\n

\u4f7f\u7528\u65b0\u7684importlib.metadata\u6a21\u5757\u53ef\u4ee5\u8bfb\u53d6\u7b2c\u4e09\u65b9\u5305\u7684\u5143\u6570\u636e\u3002\u53ef\u4ee5\u5728\u811a\u672c\u5305\u4e2d\u83b7\u53d6\u7248\u672c\u53f7\u3002<\/p>\n

2. \u5b57\u5178\u53cd\u8f6c\uff1a\u6392\u5e8f<\/strong><\/span><\/div>\n

\u73b0\u5728\uff0c\u5b57\u5178\u53ef\u4ee5\u7528resersed( )\u5728\u53cd\u8f6c\u63d2\u5165\u6392\u5e8f\u4e2d\u5b9e\u73b0\u8fed\u4ee3\u4e86\u3002<\/p>\n

3. Continue \u2013 finally<\/strong><\/span><\/div>\n

\u7531\u4e8ePython\u7684\u5b9e\u73b0\u95ee\u9898\uff0c\u4ee5\u524d\u5728Finally\u5b50\u53e5\u4e2d\u6dfb\u52a0continue\u8bed\u53e5\u662f\u4e0d\u5408\u6cd5\u7684\u3002\u73b0\u5728\u8fd9\u4e00\u95ee\u9898\u5df2\u4e0d\u590d\u5b58\u5728\u3002<\/p>\n

for i inrange(2): \r\n    try: \r\n        print(i) \r\n    finally: \r\n        print('A sentence.') \r\n        continue \r\n        print('This never shows.') \r\n# Python <= 3.7 >>SyntaxError: 'continue'not supported inside 'finally' clause \r\n# Python 3.8 \r\n>>0 \r\n   A sentence. \r\n   1 \r\n   A sentence. \r\n<\/pre>\n
4. \u6307\u793a\u53c2\u6570\u7c7b\u578b\uff1a\u8ba9\u4ee3\u7801\u66f4\u5f3a\u5927<\/strong><\/span><\/div>\n

Python\u51fd\u6570\u53ea\u63a5\u53d7\u4e24\u79cd\u53c2\u6570\u3002<\/p>\n

\u4f4d\u7f6e\u53c2\u6570\uff1a\u6839\u636e\u51fd\u6570\u5b9a\u4e49\u7684\u53c2\u6570\u4f4d\u7f6e\u6765\u4f20\u9012\u53c2\u6570\u3002
\n\u5173\u952e\u5b57\u53c2\u6570\uff1a\u4f7f\u7528\u5173\u952e\u5b57\u6765\u6307\u5b9a\u53c2\u6570\u3002
\n\u5728\u4e0b\u4f8b\u4e2d\uff0c\u53c2\u6570a\u548c\u53c2\u6570b\u7684\u503c\u53ef\u7531\u4f4d\u7f6e\u53c2\u6570\u6216\u5173\u952e\u5b57\u53c2\u6570\u786e\u5b9a\uff0c\u975e\u5e38\u7075\u6d3b\u3002<\/p>\n

def my_func(a, b=1): \r\n    return a+bmy_func(5,2)     # both positional arguments \r\nmy_func(a=5,b=2) # both keyword arguments \r\n<\/pre>\n

\u65b0\u7248\u672c\u7684Python\u7528\u8bed\u6cd5\/\u548c*\u4f5c\u533a\u5206\uff0c\u63d0\u4f9b\u4e86\u6307\u5b9a\u63a5\u53d7\u4f4d\u7f6e\u53c2\u6570\u6216\u5173\u952e\u5b57\u53c2\u6570\u7684\u65b9\u6cd5\u3002\u6ce8\u610f\uff1a\u8bed\u6cd5*\u5e76\u4e0d\u662f\u5728Python 3.8\u4e2d\u624d\u51fa\u73b0\u3002<\/p>\n

\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f00\u5934\u7684\u4e24\u4e2a\u53c2\u6570a\u548cb\u53ea\u63a5\u53d7\u4f4d\u7f6e\u53c2\u6570\uff0c\u4e2d\u95f4\u7684c\u548cd\u53ef\u63a5\u53d7\u4f4d\u7f6e\u53c2\u6570\u6216\u5173\u952e\u5b57\u53c2\u6570\uff0c\u6700\u540e\u7684e\u548cf\u53ea\u63a5\u53d7\u5173\u952e\u5b57\u53c2\u6570\u3002<\/p>\n

defmy_func(a, b, \/, c, d, *, e, f): \r\n    return a+b+c+d+e+f \r\nmy_func(1,2,3,4,5,6)          # invalid  as e and f are keyword-only \r\nmy_func(a=1,b=2,3,4,e=5,f=6) # invalid as a and b are position-only \r\nmy_func(1,2,c=3,4,e=5,f=6)    # returns  21 \r\nmy_unc(1,2,c=3,d=4,e=5,f=6)  # returns 21 \r\n<\/pre>\n

\u4e3a\u4ec0\u4e48\u8981\u727a\u7272\u7075\u6d3b\u6027\u5462?\u5982\u679c\u53c2\u6570\u540d\u79f0\u968f\u610f\u4e14\u65e0\u7528\uff0c\u5c31\u5e94\u8be5\u6392\u9664\u5173\u952e\u5b57\u53c2\u6570\u3002\u5982\u679c\u9884\u8ba1\u51fd\u6570\u4e2d\u7684\u53c2\u6570\u540d\u53ef\u80fd\u4f1a\u53d1\u751f\u53d8\u5316\uff0c\u6307\u5b9a\u53c2\u6570\u7c7b\u578b\u80fd\u907f\u514d\u4ee3\u7801\u5d29\u6e83\uff0c\u4e5f\u80fd\u8ba9\u4ee3\u7801\u66f4\u52a0\u5f3a\u5927\u3002<\/p>\n

5. \u8d4b\u503c\u8868\u8fbe\u5f0f\uff1a\u63d0\u5347\u4ee3\u7801\u53ef\u8bfb\u6027<\/strong><\/span><\/div>\n

\u8d4b\u503c\u8868\u8fbe\u5f0f\uff0c\u53c8\u79f0\u6d77\u8c61\u8fd0\u7b97\u7b26\uff0c\u662f\u4ee5:=\u4e3a\u8bed\u6cd5\u5f62\u5f0f\u7684\u65b0\u8fd0\u7b97\u7b26\uff0c\u5b83\u80fd\u628a\u8f83\u5927\u8868\u8fbe\u5f0f\u4e2d\u7684\u67d0\u90e8\u5206\u8d4b\u503c\u7ed9\u53d8\u91cf\u3002\u8fd9\u65e0\u7591\u662fPython 3.8\u4e2d\u8ba8\u8bba\u6700\u591a\u7684\u65b0\u529f\u80fd\u4e86\u3002<\/p>\n

\u6765\u770b\u4e2a\u4f8b\u5b50\uff1a \u8d4b\u503c\u8868\u8fbe\u5f0fb := a**2 \u5c06a\u7684\u5e73\u65b9\uff0c\u4e5f\u5c31\u662f36\uff0c\u8d4b\u503c\u7ed9b\uff0c\u7136\u540e\u68c0\u67e5b\u7684\u503c\u662f\u5426\u5927\u4e8e0\u3002<\/p>\n

a =6 \r\n# The following statement \r\n# assigns the value a ** 2 to  variable b, \r\n# and then check if b > 0 is  true \r\nif (b := a **2) >0: \r\n    print(f'The  square of {a} is {b}.') # The square of 6 is 36. \r\n<\/pre>\n

\u6709\u65f6\u8d4b\u503c\u8868\u8fbe\u5f0f\u80fd\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u53ef\u8bfb\u3002\u4f46\u662f\uff0c\u8bf7\u6ce8\u610f\u4e0d\u8981\u6ee5\u7528\u8d4b\u503c\u8868\u8fbe\u5f0f\uff0c\u56e0\u4e3a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5b83\u4f1a\u8ba9\u4ee3\u7801\u66f4\u52a0\u96be\u61c2\u3002<\/p>\n

# DON'T DO THIS! \r\na = 5 \r\nd = [b := a+1, a := b-1, a := a*2] \r\n<\/pre>\n

\u5176\u4ed6(\u8001)\u7a0b\u5e8f\u8bed\u8a00\u4e2d\u4e5f\u6709\u8d4b\u503c\u8868\u8fbe\u5f0f\uff0c\u5e0c\u671b\u6539\u7528Python\u7684\u7a0b\u5e8f\u5458\u4eec\u80fd(\u591a)\u7528\u8fd9\u4e00\u65b0\u529f\u80fd\u3002\u5728\u8d4b\u503c\u8868\u8fbe\u5f0f\u6cdb\u6ee5\u4e4b\u524d\u8d76\u5feb\u638c\u63e1\u5b83\u5427\u3002<\/p>\n

6. F\u5b57\u7b26\u4e322.0: \u8c03\u8bd5(debug)\u7684\u5229\u5668<\/strong><\/span><\/div>\n

Python f\u5b57\u7b26\u4e32\u6539\u53d8\u4e86\u6e38\u620f\u89c4\u5219\u3002F\u5b57\u7b26\u4e32\u662f\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u8bed\u6cd5\uff0c\u7b80\u6d01\u4e14\u6613\u8bfb\u3002\u5229\u7528\u8bed\u6cd5f'{expr}'\uff0c\u5728\u5f15\u53f7\u524d\u52a0\u4e0af\uff0c\u5c06\u8868\u8fbe\u5f0f\u7528\u5927\u62ec\u53f7\u62ec\u4f4f\uff0c\u5c31\u80fd\u591f\u5c06\u8868\u8fbe\u5f0f\u63d2\u5165\u5b57\u7b26\u4e32\u3002<\/p>\n

Python\u66f4\u65b0\u4e4b\u540e\uff0c\u7b49\u4e8e\u53f7\u201c=\u201d\u80fd\u591f\u5728f\u5b57\u7b26\u4e32f'{expr=}'\u8bed\u6cd5\u4e2d\u4f5c\u4e3a\u683c\u5f0f\u9650\u5b9a\u7b26\u4f7f\u7528\u3002\u8f93\u51fa\u7684\u5b57\u7b26\u4e32\u4e3a\u53d8\u91cf\u540d=\u53d8\u91cf\u503c\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

# DON'T DO THIS! \r\na = 5 \r\nd = [b := a+1, a := b-1, a := a*2] \r\n<\/pre>\n

\u51fa\u4e8e\u7f16\u5236\u6587\u4ef6\u6216\u8c03\u8bd5 (Debug) \u7684\u9700\u8981\uff0c\u5e38\u9700\u8981\u6307\u660e\u53d8\u91cf\u503c\u3002F\u5b57\u7b26\u4e32\u4f7f\u5f97\u8c03\u8bd5(debug)\u6613\u5982\u53cd\u638c\u3002<\/p>\n

\u5f53\u7136\uff0c\u5728\u6295\u8eabPython 3.8\u7f16\u7a0b\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u638c\u63e1Python\u57fa\u672c\u529f\u80fd(\u5fc5\u4e0d\u53ef\u5c11)\u3002<\/p>\n

\u8fd9\u5c31\u662fPython\u76846\u5927\u65b0\u529f\u80fd\uff0c\u6bcf\u4e2aPython\u65b0\u624b\u90fd\u80fd\u4ece\u4e2d\u83b7\u76ca\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

IT\u4e1a\u65e5\u65b0\u6708\u5f02\uff0c\u843d\u540e\u7684Python 2\u5c06\u57282020\u5e74\u9010\u6e10\u88ab\u6dd8\u6c70\u3002 2019\u5e74\uff0c\u968f\u7740\u6570\u636e\u79d1\u5b66\u7684\u5174\u8d77\uff0cPython […]<\/p>\n","protected":false},"author":1898,"featured_media":171571,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[153],"class_list":["post-171569","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/171569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=171569"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/171569\/revisions"}],"predecessor-version":[{"id":173558,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/171569\/revisions\/173558"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/171571"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=171569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=171569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=171569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}