{"id":204,"date":"2023-10-26T16:36:32","date_gmt":"2023-10-26T08:36:32","guid":{"rendered":"http:\/\/ai.gitpp.com\/?p=204"},"modified":"2023-10-27T09:38:00","modified_gmt":"2023-10-27T01:38:00","slug":"9-%e7%b1%bb","status":"publish","type":"post","link":"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/9-%e7%b1%bb\/","title":{"rendered":"\u300aPython\u5165\u95e8\u6559\u7a0b\u300b9. \u7c7b"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>\u5f00\u80c3\u83dc<\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/2-%e4%bd%bf%e7%94%a8-python-%e8%a7%a3%e9%87%8a%e5%99%a8\/\">\u4f7f\u7528 Python \u89e3\u91ca\u5668<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/3-%e9%9d%9e%e6%ad%a3%e5%bc%8f%e4%bb%8b%e7%bb%8dpython\/\">\u975e\u6b63\u5f0f\u4ecb\u7ecdPython<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/4-%e6%b7%b1%e5%85%a5%e6%b5%81%e7%a8%8b%e6%8e%a7%e5%88%b6\/\">\u6df1\u5165\u6d41\u7a0b\u63a7\u5236<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/5-%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84\/\">\u6570\u636e\u7ed3\u6784<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/6-%e6%a8%a1%e5%9d%97\/\">\u6a21\u5757<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/7-%e8%be%93%e5%85%a5%e5%92%8c%e8%be%93%e5%87%ba\/\">\u8f93\u5165\u548c\u8f93\u51fa<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/8-%e9%94%99%e8%af%af%e5%92%8c%e5%bc%82%e5%b8%b8\/\">\u9519\u8bef\u548c\u5f02\u5e38<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/9-%e7%b1%bb\/\">\u7c7b<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/10-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/11-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d%ef%bc%88%e7%ac%ac%e4%ba%8c%e9%83%a8%e5%88%86%ef%bc%89\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd\uff08\u7b2c\u4e8c\u90e8\u5206\uff09<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/12-%e7%8e%b0%e5%9c%a8%e5%b9%b2%e4%bb%80%e4%b9%88\/\">\u73b0\u5728\u5e72\u4ec0\u4e48?<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/13-%e4%ba%a4%e4%ba%92%e5%bc%8f%e8%be%93%e5%85%a5%e7%bc%96%e8%be%91%e5%8f%8a%e5%8e%86%e5%8f%b2%e6%9b%bf%e4%bb%a3\/\">\u4ea4\u4e92\u5f0f\u8f93\u5165\u7f16\u8f91\u53ca\u5386\u53f2\u66ff\u4ee3<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/14-%e6%b5%ae%e7%82%b9%e7%ae%97%e6%9c%af-%e9%97%ae%e9%a2%98%e5%92%8c%e9%99%90%e5%88%b6\/\">\u6d6e\u70b9\u7b97\u672f: \u95ee\u9898\u548c\u9650\u5236<\/a><\/li>\n<\/ol>\n\n\n\n<p>\u540c\u522b\u7684\u7f16\u7a0b\u8bed\u8a00\u76f8\u6bd4, Python \u7684\u7c7b\u673a\u5236\u4e2d\u589e\u52a0\u4e86\u5c11\u91cf\u65b0\u7684\u8bed\u6cd5\u548c\u8bed\u4e49. \u5b83\u662f C++ \u7684\u7c7b\u673a\u5236\u548c Modula-3 \u7684\u7c7b\u673a\u5236\u7684\u6df7\u5408\u4f53. Python \u7c7b\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u6240\u6709\u57fa\u672c\u7279\u5f81: \u5141\u8bb8\u591a\u7ee7\u627f\u7684\u7c7b\u7ee7\u627f\u673a\u5236, \u6d3e\u751f\u7c7b\u53ef\u4ee5\u91cd\u5199\u5b83\u7236\u7c7b\u7684\u4efb\u4f55\u65b9\u6cd5, \u4e00\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u8c03\u7528\u7236\u7c7b\u4e2d\u91cd\u540d\u7684\u65b9\u6cd5. \u5bf9\u8c61\u53ef\u4ee5\u5305\u542b\u4efb\u610f\u6570\u91cf\u548c\u7c7b\u578b\u7684\u6570\u636e\u6210\u5458. \u4f5c\u4e3a\u6a21\u5757, \u7c7b\u4e5f\u62e5\u6709 Python \u7684\u52a8\u6001\u7279\u5f81: \u4ed6\u4eec\u53ef\u4ee5\u88ab\u52a8\u6001\u521b\u5efa, \u5e76\u4e14\u53ef\u4ee5\u5728\u521b\u5efa\u4e4b\u540e\u88ab\u4fee\u6539.<\/p>\n\n\n\n<p>\u4eceC++\u672f\u8bed\u4e0a\u8bb2, Python \u7c7b\u7684\u6210\u5458 (\u5305\u62ec\u6570\u636e\u6210\u5458) \u901a\u5e38\u90fd\u662f&nbsp;<em>public<\/em>&nbsp;\u7684 (\u4f8b\u5916\u89c1\u4e0b&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-private\">\u79c1\u6709\u53d8\u91cf<\/a>), \u5e76\u4e14\u6240\u6709\u7684\u6210\u5458\u51fd\u6570\u90fd\u662f&nbsp;<em>virtual<\/em>&nbsp;\u7684. \u548c Modula-3 \u4e2d\u4e00\u6837, Python \u4e2d\u6ca1\u6709\u5173\u8054\u5bf9\u8c61\u6210\u5458\u548c\u65b9\u6cd5\u7684\u9690\u5f0f\u8868\u8fbe: \u6240\u6709\u65b9\u6cd5\u51fd\u6570\u5728\u58f0\u660e\u65f6\u663e\u5f0f\u5730\u5c06\u7b2c\u4e00\u4e2a\u53c2\u6570\u8868\u793a\u4e3a\u5bf9\u8c61, \u8fd9\u4e2a\u53c2\u6570\u7684\u503c\u5728\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\u9690\u5f0f\u8d4b\u503c. \u540c Smalltalk \u7c7b\u4f3c, Python \u7c7b\u672c\u8eab\u5c31\u662f\u5bf9\u8c61. \u8fd9\u5c31\u63d0\u4f9b\u4e86\u5bfc\u5165\u548c\u91cd\u547d\u540d\u7684\u8bed\u4e49. \u4e0e C++ \u548c Modula-3 \u4e0d\u540c\u7684\u662f, Python \u7684\u5185\u7f6e\u7c7b\u578b\u53ef\u4ee5\u88ab\u5f53\u505a\u57fa\u7c7b\u6765\u8ba9\u4f7f\u7528\u8005\u6269\u5c55. \u53e6\u5916, \u50cf C++ \u4e00\u6837, \u5927\u591a\u6570\u6709\u7279\u6b8a\u8bed\u6cd5\u7684\u5185\u7f6e\u64cd\u4f5c\u7b26(\u7b97\u6570\u8fd0\u7b97\u7b26, \u4e0b\u6807\u64cd\u4f5c\u7b26\u7b49\u7b49) \u5728\u7c7b\u7684\u5b9e\u4f8b\u4e2d\u90fd\u53ef\u4ee5\u91cd\u5b9a\u4e49.<\/p>\n\n\n\n<p>(\u7531\u4e8e\u5728\u8c08\u8bba\u7c7b\u7684\u65f6\u5019\u7f3a\u4e4f\u516c\u8ba4\u7684\u672f\u8bed, \u6211\u4f1a\u5076\u5c14\u4f7f\u7528 Smalltalk \u548c C++ \u7684\u672f\u8bed. \u6211\u66f4\u613f\u610f\u7528 Modula-3\u7684\u672f\u8bed, \u56e0\u4e3a\u5b83\u9762\u5411\u5bf9\u8c61\u7684\u8bed\u4e49\u6bd4C++\u66f4\u8d34\u8fd1Python, \u4f46\u662f\u6211\u4f30\u8ba1\u6ca1\u6709\u8bfb\u8005\u542c\u8fc7\u8fd9\u4e2a\u8bf4\u6cd5.)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9.1. \u5173\u4e8e\u540d\u79f0\u548c\u5bf9\u8c61\u7684\u8ba8\u8bba<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-object\"><\/a><\/h2>\n\n\n\n<p>\u5bf9\u8c61\u90fd\u5177\u6709\u4e2a\u522b\u6027, \u591a\u4e2a\u540d\u79f0(\u5728\u591a\u4e2a\u4f5c\u7528\u57df\u4e2d) \u53ef\u4ee5\u88ab\u7ed1\u5b9a\u5230\u540c\u4e00\u4e2a\u5bf9\u8c61\u4e0a. \u8fd9\u5c31\u662f\u5176\u4ed6\u8bed\u8a00\u4e2d\u6240\u8c13\u7684\u522b\u540d. \u901a\u5e38\u7b2c\u4e00\u6b21\u63a5\u89e6 Python \u53ef\u80fd\u4e0d\u4f1a\u610f\u8bc6\u5230\u8fd9\u4e00\u70b9, \u800c\u4e14\u5728\u5904\u7406\u4e0d\u53d8\u7684\u57fa\u672c\u7c7b\u578b(\u6570\u503c, \u5b57\u7b26\u4e32, \u5143\u7ec4)\u65f6\u8fd9\u4e00\u70b9\u53ef\u80fd\u4f1a\u88ab\u5b89\u5168\u7684\u5ffd\u7565. \u4f46\u662f, \u5728\u6d89\u53ca\u5230\u53ef\u53d8\u5bf9\u8c61\u5982 lists, dictionaries, \u4ee5\u53ca\u5927\u591a\u6570\u5176\u4ed6\u7c7b\u578b\u65f6, \u522b\u540d\u53ef\u80fd\u4f1a\u5728 Python \u4ee3\u7801\u7684\u8bed\u4e49\u4e0a\u8d77\u5230\u60ca\u4eba\u7684\u6548\u679c. \u522b\u540d\u901a\u5e38\u5bf9\u7f16\u7a0b\u6709\u76ca\u5904, \u56e0\u4e3a\u522b\u540d\u5728\u67d0\u4e9b\u65b9\u9762\u8868\u73b0\u5f97\u50cf\u6307\u9488. \u6bd4\u5982, \u7531\u4e8e\u5728\u5b9e\u73b0\u7684\u65f6\u5019\u4f20\u9012\u7684\u662f\u6307\u9488, \u6240\u4ee5\u4f20\u9012\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f00\u9500\u5f88\u5c0f; \u53c8\u6bd4\u5982\u5c06\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\u6765\u5bf9\u5b83\u8fdb\u884c\u4fee\u6539, \u8c03\u7528\u8005\u5c06\u4f1a\u770b\u5230\u5bf9\u8c61\u7684\u53d8\u5316 \u2014 \u8fd9\u5c31\u6d88\u9664\u4e86\u50cfPascal \u8bed\u8a00\u4e2d\u7684\u4e24\u4e2a\u4e0d\u540c\u53c2\u6570\u4e4b\u95f4\u7684\u4f20\u9012\u673a\u5236\u7684\u5fc5\u8981.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9.2. Python \u7684\u4f5c\u7528\u57df\u548c\u547d\u540d\u7a7a\u95f4<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-scopes\"><\/a><\/h2>\n\n\n\n<p>\u5728\u4ecb\u7ecd\u7c7b\u4e4b\u524d, \u6211\u5fc5\u987b\u5148\u544a\u8bc9\u4f60\u4e00\u4e9b\u5173\u4e8e Python \u4f5c\u7528\u57df\u89c4\u5219\u7684\u4e8b. \u7c7b\u5b9a\u4e49\u7528\u547d\u540d\u7a7a\u95f4\u73a9\u4e86\u4e00\u4e9b\u5de7\u5999\u7684\u628a\u620f, \u800c\u4f60\u4e3a\u4e86\u5b8c\u5168\u7406\u89e3\u53d1\u751f\u4e86\u4ec0\u4e48\u5c31\u5fc5\u987b\u77e5\u9053\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u662f\u600e\u4e48\u5de5\u4f5c\u7684. \u987a\u4fbf\u8bf4\u4e00\u4e0b, \u8fd9\u4e00\u4e3b\u9898\u7684\u77e5\u8bc6\u5bf9\u4efb\u4f55\u9ad8\u7ea7 Python \u7a0b\u5e8f\u5458\u90fd\u662f\u6709\u7528\u7684.<\/p>\n\n\n\n<p>\u8ba9\u6211\u4eec\u4ece\u5b9a\u4e49\u5f00\u59cb.<\/p>\n\n\n\n<p><em>\u547d\u540d\u7a7a\u95f4<\/em>&nbsp;\u662f\u4ece\u540d\u79f0\u5230\u5bf9\u8c61\u7684\u6620\u5c04. \u5927\u591a\u6570\u547d\u540d\u7a7a\u95f4\u73b0\u5728\u7684\u5b9e\u73b0\u5c31\u5982\u540c Python \u7684\u5b57\u5178, \u4f46\u901a\u5e38\u8fd9\u4e00\u70b9\u5e76\u4e0d\u660e\u663e(\u9664\u4e86\u5728\u6027\u80fd\u4e0a), \u800c\u4e14\u5b83\u6709\u53ef\u80fd\u5728\u5c06\u6765\u53d1\u751f\u6539\u53d8.<\/p>\n\n\n\n<p>\u987a\u4fbf\u8bf4\u4e00\u4e0b, \u6211\u7528\u4e86&nbsp;<em>\u5c5e\u6027<\/em>&nbsp;\u8fd9\u4e2a\u8bcd\u6765\u79f0\u547c\u4efb\u4f55\u70b9\u540e\u9762\u8ddf\u7684\u540d\u79f0 \u2014 \u6bd4\u5982, \u5728\u8868\u8fbe\u5f0f&nbsp;<code>z.real<\/code>&nbsp;\u4e2d,&nbsp;<code>real<\/code>&nbsp;\u5c31\u662f\u5bf9\u8c61&nbsp;<code>z<\/code>&nbsp;\u7684\u5c5e\u6027. \u66f4\u76f4\u63a5\u7684\u8bf4, \u5bf9\u6a21\u5757\u4e2d\u540d\u79f0\u7684\u5f15\u7528\u5c31\u662f\u5c5e\u6027\u5f15\u7528: \u5728\u8868\u8fbe\u5f0f&nbsp;<code>modname.funcname<\/code>&nbsp;\u4e2d,&nbsp;<code>modname<\/code>&nbsp;\u662f\u6a21\u5757\u5bf9\u8c61\u800c&nbsp;<code>funcname<\/code>&nbsp;\u662f\u5b83\u7684\u4e00\u4e2a\u5c5e\u6027. \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6a21\u5757\u7684\u5c5e\u6027\u548c\u5b83\u91cc\u9762\u6240\u5b9a\u4e49\u7684\u5168\u5c40\u540d\u79f0\u4e4b\u95f4\u5c31\u521a\u597d\u6709\u4e00\u4e2a\u76f4\u63a5\u7684\u6620\u5c04\u5173\u7cfb: \u4ed6\u4eec\u5171\u4eab\u540c\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4!&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#id26\">[1]<\/a><\/p>\n\n\n\n<p>\u5c5e\u6027\u53ef\u4ee5\u662f\u53ea\u8bfb\u7684\u6216\u53ef\u5199\u7684. \u5728\u540e\u4e00\u79cd\u60c5\u51b5\u4e0b, \u7ed9\u5c5e\u6027\u8d4b\u503c\u624d\u662f\u53ef\u80fd\u7684. \u6a21\u5757\u5c5e\u6027\u662f\u53ef\u5199\u7684: \u4f60\u53ef\u4ee5\u5199&nbsp;<code>modname.the_answer&nbsp;=&nbsp;42<\/code>. \u53ef\u4ee5\u5229\u7528:keyword:del&nbsp;\u8bed\u53e5\u6765\u5220\u9664\u53ef\u5199\u5c5e\u6027. \u4f8b\u5982,&nbsp;<code>del&nbsp;modname.the_answer<\/code>&nbsp;\u5c06\u4ece\u540d\u4e3a&nbsp;<code>modname<\/code>&nbsp;\u7684\u6a21\u5757\u4e2d\u79fb\u9664\u5c5e\u6027&nbsp;<code>the_answer<\/code>.<\/p>\n\n\n\n<p>\u547d\u540d\u7a7a\u95f4\u4eec\u662f\u5728\u4e0d\u540c\u65f6\u523b\u521b\u5efa\u7684,\u5e76\u4e14\u6709\u7740\u4e0d\u540c\u7684\u751f\u547d\u671f. \u5305\u542b\u5185\u7f6e\u540d\u79f0\u7684\u547d\u540d\u7a7a\u95f4\u662f\u5728 Python \u89e3\u91ca\u5668\u542f\u52a8\u65f6\u521b\u5efa\u7684, \u800c\u4e14\u5b83\u6c38\u8fdc\u4e0d\u88ab\u5220\u9664. \u4e00\u4e2a\u6a21\u5757\u7684\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u5728\u6a21\u5757\u7684\u5b9a\u4e49\u88ab\u8bfb\u53d6\u7684\u65f6\u5019\u521b\u5efa; \u901a\u5e38\u60c5\u51b5\u4e0b, \u6a21\u5757\u7684\u547d\u540d\u7a7a\u95f4\u4e00\u76f4\u6301\u7eed\u5230\u89e3\u91ca\u5668\u9000\u51fa\u65f6. \u88ab\u6700\u9ad8\u7ea7\u522b\u7684\u89e3\u91ca\u5668\u8c03\u7528\u7684\u8bed\u53e5, \u4e0d\u8bba\u662f\u4ece\u811a\u672c\u8fd8\u662f\u4ece\u4ea4\u4e92\u8bfb\u53d6\u7684, \u90fd\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u540d\u53eb&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/__main__.html#module-__main__\"><code>__main__<\/code><\/a>&nbsp;\u7684\u6a21\u5757\u7684\u4e00\u90e8\u5206, \u6240\u4ee5\u5b83\u4eec\u6709\u81ea\u5df1\u7684\u5168\u5c40\u547d\u540d\u7a7a\u95f4. (\u5185\u7f6e\u540d\u79f0\u5b9e\u9645\u4e0a\u4e5f\u5b58\u5728\u4e8e\u4e00\u4e2a\u6a21\u5757\u4e2d; \u8fd9\u4e2a\u6a21\u5757\u53eb&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/builtins.html#module-builtins\"><code>builtins<\/code><\/a>.)<\/p>\n\n\n\n<p>\u51fd\u6570\u7684\u5c40\u90e8\u547d\u540d\u7a7a\u95f4\u5728\u51fd\u6570\u8c03\u7528\u65f6\u88ab\u521b\u5efa, \u5728\u51fd\u6570\u8fd4\u56de\u65f6\u6216\u8005\u53d1\u751f\u5f02\u5e38\u800c\u7ec8\u6b62\u65f6\u88ab\u5220\u9664. (\u4e8b\u5b9e\u4e0a, \u5fd8\u8bb0\u53ef\u80fd\u662f\u66f4\u597d\u7684\u65b9\u5f0f\u6765\u63cf\u8ff0\u771f\u6b63\u53d1\u751f\u4e86\u4ec0\u4e48.) \u5f53\u7136, \u9012\u5f52\u8c03\u7528\u4f1a\u6709\u5b83\u4eec\u81ea\u5df1\u7684\u5c40\u90e8\u547d\u540d\u7a7a\u95f4.<\/p>\n\n\n\n<p>\u5728 Python \u4e2d, \u4e00\u4e2a\u4f5c\u7528\u57df\u53ea\u662f\u4e00\u4e2a\u7ed3\u6784\u4e0a\u7684\u533a\u57df, \u5728\u8fd9\u91cc\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee. \u201c\u76f4\u63a5\u8bbf\u95ee\u201d \u5c31\u610f\u5473\u7740\u65e0\u987b\u7279\u6b8a\u7684\u6307\u660e\u5f15\u7528.<\/p>\n\n\n\n<p>\u5c3d\u7ba1\u4f5c\u7528\u57df\u662f\u9759\u6001\u7684\u51b3\u5b9a\u7684, \u5b83\u4eec\u4f7f\u7528\u65f6\u5374\u662f\u52a8\u6001\u7684. \u5728\u6267\u884c\u65f6\u7684\u4efb\u4f55\u65f6\u523b, \u81f3\u5c11\u6709\u4e09\u4e2a\u5d4c\u5957\u7684\u4f5c\u7528\u57df\u5176\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6700\u5185\u5c42\u7684\u4f5c\u7528\u57df, \u9996\u5148\u88ab\u641c\u7d22, \u5305\u542b\u5c40\u90e8\u53d8\u91cf\u540d<\/li>\n\n\n\n<li>\u4efb\u610f\u51fd\u6570\u7684\u4f5c\u7528\u57df, \u5b83\u4ece\u6700\u63a5\u8fd1\u7684\u4f5c\u7528\u57df\u5f00\u59cb\u641c\u7d22, \u5305\u62ec\u975e\u5c40\u90e8\u7684, \u4f46\u4e5f\u662f\u975e\u5168\u5c40\u7684\u540d\u5b57<\/li>\n\n\n\n<li>\u7d27\u90bb\u6700\u540e\u7684\u4f5c\u7528\u57df\u5305\u542b\u4e86\u5f53\u524d\u6a21\u5757\u7684\u5168\u5c40\u53d8\u91cf<\/li>\n\n\n\n<li>\u6700\u5916\u5c42\u7684\u4f5c\u7528\u57df (\u6700\u540e\u641c\u7d22) \u662f\u5305\u542b\u5185\u7f6e\u540d\u5b57\u7684\u547d\u540d\u7a7a\u95f4<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u679c\u4e00\u4e2a\u540d\u5b57\u5728\u5168\u5c40\u58f0\u660e, \u90a3\u4e48\u6240\u6709\u7684\u5f15\u7528\u548c\u8d4b\u503c\u90fd\u76f4\u63a5\u5230\u8fd9\u4e2a\u6a21\u5757\u7684\u5168\u5c40\u540d\u4e2d. \u4e3a\u4e86\u5728\u6700\u5185\u90e8\u4f5c\u7528\u57df\u4e2d\u91cd\u65b0\u7ed1\u5b9a\u53d8\u91cf,&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#nonlocal\"><code>nonlocal<\/code><\/a>&nbsp;\u8bed\u53e5\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86; \u5982\u679c\u6ca1\u6709\u58f0\u660e&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#nonlocal\"><code>nonlocal<\/code><\/a>&nbsp;, \u90a3\u4e9b\u53d8\u91cf\u53ea\u662f\u53ea\u8bfb (\u5c1d\u8bd5\u7ed9\u8fd9\u6837\u7684\u53d8\u91cf\u8d4b\u503c, \u53ea\u662f\u4f1a\u7b80\u5355\u7684\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5c40\u90e8\u53d8\u91cf, \u800c\u5916\u90e8\u7684\u5e76\u6ca1\u6709\u4ec0\u4e48\u6539\u53d8)\u91cd\u65b0\u7ed1\u5b9a.<\/p>\n\n\n\n<p>\u4e00\u822c\u6765\u8bf4, \u5c40\u90e8\u4f5c\u7528\u57df\u5f15\u7528\u5f53\u524d\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u540d. \u5728\u51fd\u6570\u5916\u90e8, \u5c40\u90e8\u53d8\u91cf\u5f15\u7528\u548c\u5168\u5c40\u4f5c\u7528\u57df\u76f8\u540c\u7684\u547d\u540d\u7a7a\u95f4: \u6a21\u5757\u7684\u547d\u540d\u7a7a\u95f4. \u7c7b\u5b9a\u4e49\u53c8\u653e\u7f6e\u4e86\u53e6\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4.<\/p>\n\n\n\n<p>\u610f\u8bc6\u5230\u4f5c\u7528\u57df\u662f\u5728\u7ed3\u6784\u4e0a\u88ab\u51b3\u5b9a\u7684\u8fd9\u5f88\u91cd\u8981. \u4e00\u4e2a\u5b9a\u4e49\u5728\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u7684\u5168\u5c40\u4f5c\u7528\u57df, \u5c31\u662f\u6a21\u5757\u7684\u547d\u540d\u7a7a\u95f4, \u65e0\u8bba\u5b83\u4ece\u54ea\u91cc\u88ab\u8bbf\u95ee. \u53e6\u4e00\u4e2a\u65b9\u9762, \u641c\u5bfb\u540d\u5b57\u7684\u8fc7\u7a0b\u662f\u52a8\u6001\u5b8c\u6210\u7684, \u5728\u8fd0\u884c\u65f6 \u2014 \u4f46\u662f, \u8bed\u8a00\u7684\u5b9a\u4e49\u4e00\u822c\u662f\u9759\u6001\u7684, \u5728 \u201c\u7f16\u8bd1\u201d \u65f6\u5b8c\u6210, \u6240\u4ee5\u4e0d\u8981\u4f9d\u8d56\u52a8\u6001\u547d\u540d! (\u4e8b\u5b9e\u4e0a, \u5c40\u90e8\u53d8\u91cf\u90fd\u662f\u9759\u6001\u7684\u88ab\u51b3\u5b9a\u7684.)<\/p>\n\n\n\n<p>Python \u7684\u4e00\u4e2a\u602a\u4e8b\u5c31\u662f \u2013 \u5982\u679c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#global\"><code>global<\/code><\/a>&nbsp;\u8bed\u53e5\u6ca1\u6709\u8d77\u6548\u679c \u2013 \u8d4b\u503c\u603b\u662f\u4f1a\u4f7f\u7528\u6700\u91cc\u5c42\u4f5c\u7528\u57df\u7684\u503c. \u8d4b\u503c\u5e76\u6ca1\u6709\u62f7\u8d1d\u6570\u636e \u2014 \u5b83\u4eec\u4ec5\u4ec5\u662f\u7ed1\u5b9a\u540d\u5b57\u5230\u5bf9\u8c61\u4e0a. \u5220\u9664\u4e5f\u662f\u5982\u6b64:&nbsp;<code>del&nbsp;x<\/code>&nbsp;\u79fb\u9664\u4e86&nbsp;<code>x<\/code>&nbsp;\u4ece\u5c40\u90e8\u4f5c\u7528\u57df\u7684\u7ed1\u5b9a. \u4e8b\u5b9e\u4e0a, \u6240\u6709\u64cd\u4f5c\u5f15\u5165\u65b0\u7684\u540d\u5b57\u90fd\u4f7f\u7528\u5c40\u90e8\u4f5c\u7528\u57df: \u7279\u522b\u7684,&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#import\"><code>import<\/code><\/a>&nbsp;\u8bed\u53e5, \u548c\u51fd\u6570\u5b9a\u4e49\u90fd\u5c06\u6a21\u5757\u6216\u51fd\u6570\u7ed1\u5b9a\u5230\u4e86\u5f53\u524d\u4f5c\u7528\u57df.<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#global\"><code>global<\/code><\/a>&nbsp;\u8bed\u53e5\u53ef\u4ee5\u7528\u4e8e\u6307\u793a, \u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u53ef\u4ee5\u5728\u8fd9\u91cc\u91cd\u65b0\u7ed1\u5b9a;&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#nonlocal\"><code>nonlocal<\/code><\/a>&nbsp;\u5219\u8868\u793a\u5728\u4e00\u4e2a\u95ed\u5408\u7684\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u53ef\u4ee5\u5728\u6b64\u5904\u7ed1\u5b9a.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9.2.1. \u57df\u548c\u547d\u540d\u7a7a\u95f4\u7684\u4f8b\u5b50<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-scopeexample\"><\/a><\/h3>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50\u7528\u4e8e\u8bf4\u660e\u5982\u4f55\u5f15\u7528\u4e0d\u540c\u7684\u4f5c\u7528\u57df\u548c\u547d\u540d\u7a7a\u95f4,&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#global\"><code>global<\/code><\/a>&nbsp;\u548c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#nonlocal\"><code>nonlocal<\/code><\/a>&nbsp;\u5982\u4f55\u5f71\u54cd\u53d8\u91cf\u7ed1\u5b9a:<strong>def<\/strong> scope_test(): <strong>def<\/strong> do_local(): spam = &#8220;local spam&#8221; <strong>def<\/strong> do_nonlocal(): <strong>nonlocal<\/strong> spam spam = &#8220;nonlocal spam&#8221; <strong>def<\/strong> do_global(): <strong>global<\/strong> spam spam = &#8220;global spam&#8221; spam = &#8220;test spam&#8221; do_local() print(&#8220;After local assignment:&#8221;, spam) do_nonlocal() print(&#8220;After nonlocal assignment:&#8221;, spam) do_global() print(&#8220;After global assignment:&#8221;, spam) scope_test() print(&#8220;In global scope:&#8221;, spam)<\/p>\n\n\n\n<p>\u8f93\u51fa\u7684\u7ed3\u679c\u662f:After local assignment: test spam After <strong>nonlocal<\/strong> assignment: <strong>nonlocal<\/strong> spam After <strong>global<\/strong> assignment: <strong>nonlocal<\/strong> spam In <strong>global<\/strong> scope: <strong>global<\/strong> spam<\/p>\n\n\n\n<p>\u6ce8\u610f\u5c40\u90e8\u7684\u8d4b\u503c (\u9ed8\u8ba4) \u5e76\u6ca1\u6709\u6539\u53d8&nbsp;<em>scope_test<\/em>&nbsp;\u7ed1\u5b9a\u7684&nbsp;<em>spam<\/em>. \u800c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#nonlocal\"><code>nonlocal<\/code><\/a>&nbsp;\u5219\u6539\u53d8\u4e86&nbsp;<em>scope_test<\/em>&nbsp;\u4e2d\u7684&nbsp;<em>spam<\/em>, \u800c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#global\"><code>global<\/code><\/a>&nbsp;\u5219\u6539\u53d8\u4e86\u6a21\u5757\u7ea7\u522b\u7684\u7ed1\u5b9a.<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u770b\u5230\u5728&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#global\"><code>global<\/code><\/a>&nbsp;\u8d4b\u503c\u4e4b\u524d\u5e76\u6ca1\u6709\u7ed1\u5b9a&nbsp;<em>spam<\/em>&nbsp;\u7684\u503c.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9.3. \u7c7b\u7684\u521d\u5370\u8c61<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-firstclasses\"><\/a><\/h2>\n\n\n\n<p>\u7c7b\u5f15\u5165\u4e86\u4e00\u4e9b\u65b0\u7684\u8bed\u6cd5, \u4e09\u79cd\u65b0\u7684\u5bf9\u8c61\u7c7b\u578b, \u548c\u4e00\u4e9b\u65b0\u7684\u8bed\u4e49.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9.3.1. \u7c7b\u5b9a\u4e49\u7684\u8bed\u6cd5<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-classdefinition\"><\/a><\/h3>\n\n\n\n<p>\u6700\u7b80\u5355\u7684\u7c7b\u7684\u5b9a\u4e49\u5f62\u5f0f\u770b\u8d77\u6765\u50cf\u8fd9\u6837:<strong>class<\/strong><strong>ClassName<\/strong>: &lt;statement-1&gt; . . . &lt;statement-N&gt;<\/p>\n\n\n\n<p>\u7c7b\u7684\u5b9a\u4e49, \u548c\u51fd\u6570\u5b9a\u4e49 (<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#def\"><code>def<\/code><\/a>&nbsp;\u8bed\u53e5) \u4e00\u6837\u5fc5\u987b\u5728\u4f7f\u7528\u5b83\u4eec\u524d\u6267\u884c. (\u4f60\u53ef\u4ee5\u5c06\u4e00\u4e2a\u7c7b\u5b9a\u4e49\u653e\u7f6e\u4e8e&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#if\"><code>if<\/code><\/a>&nbsp;\u8bed\u53e5\u7684\u5206\u652f\u4e2d, \u6216\u4e00\u4e2a\u51fd\u6570\u4e2d.)<\/p>\n\n\n\n<p>\u4e8b\u5b9e\u4e0a, \u7c7b\u5b9a\u4e49\u5185\u90e8\u7684\u8bed\u53e5\u4e00\u822c\u662f\u51fd\u6570\u7684\u5b9a\u4e49, \u4f46\u5176\u4ed6\u7684\u8bed\u53e5\u4e5f\u662f\u5141\u8bb8\u7684, \u800c\u4e14\u8fd8\u5f88\u6709\u7528 \u2014 \u6211\u4eec\u5728\u540e\u9762\u5c06\u4f1a\u7ee7\u7eed\u8ba8\u8bba\u8be5\u95ee\u9898. \u7c7b\u5185\u7684\u51fd\u6570\u5b9a\u4e49\u4e00\u822c\u6709\u4e00\u4e2a\u7279\u6b8a\u5f62\u5f0f\u7684\u53c2\u6570\u5217\u8868, \u4e60\u60ef\u4e0a\u79f0\u4e4b\u4e3a\u65b9\u6cd5 \u2014 \u540c\u6837, \u4e5f\u5c06\u5728\u540e\u9762\u89e3\u91ca.<\/p>\n\n\n\n<p>\u5f53\u8fdb\u5165\u4e00\u4e2a\u7c7b\u5b9a\u4e49, \u65b0\u7684\u547d\u540d\u7a7a\u95f4\u5c31\u88ab\u521b\u5efa\u4e86, \u8fd9\u4e00\u822c\u4f5c\u4e3a\u5c40\u90e8\u7684\u4f5c\u7528\u57df \u2014 \u56e0\u6b64, \u6240\u6709\u7684\u5c40\u90e8\u53d8\u91cf\u90fd\u5728\u8fd9\u4e2a\u65b0\u7684\u4f5c\u7528\u57df\u4e2d. \u7279\u522b\u662f, \u51fd\u6570\u5b9a\u4e49\u4f1a\u7ed1\u5b9a.<\/p>\n\n\n\n<p>\u5f53\u79bb\u5f00\u4e00\u4e2a\u7c7b\u5b9a\u4e49\u540e, \u4e00\u4e2a&nbsp;<em>class object<\/em>&nbsp;\u5c31\u88ab\u521b\u5efa. \u901a\u8fc7\u7c7b\u7684\u5b9a\u4e49, \u5c31\u5c06\u8fd9\u4e2a\u547d\u540d\u7a7a\u95f4\u5305\u88c5\u4e86\u8d77\u6765; \u6211\u4eec\u5c06\u5728\u540e\u9762\u5b66\u5230\u66f4\u591a\u5173\u4e8e\u7c7b\u5bf9\u8c61\u7684\u77e5\u8bc6. \u539f\u6765\u7684\u5c40\u90e8\u4f5c\u7528\u57df (\u5728\u8fdb\u5165\u4e00\u4e2a\u7c7b\u5b9a\u4e49\u524d\u7684\u4f5c\u7528\u57df) \u5c06\u4f1a\u590d\u4f4d, \u800c\u7c7b\u5bf9\u8c61\u5c31\u4f1a\u5728\u8fd9\u91cc\u7ed1\u5b9a, \u5e76\u4e14\u547d\u540d\u4e3a\u7c7b\u5b9a\u4e49\u65f6\u7684\u540d\u5b57 (\u5728\u6b64\u4f8b\u4e2d\u662f&nbsp;<code>ClassName<\/code>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9.3.2. \u7c7b\u5bf9\u8c61<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-classobjects\"><\/a><\/h3>\n\n\n\n<p>\u7c7b\u5bf9\u8c61\u652f\u6301\u4e24\u79cd\u64cd\u4f5c: \u5c5e\u6027\u5f15\u7528\u548c\u5b9e\u4f8b\u5316.<\/p>\n\n\n\n<p><em>\u5c5e\u6027\u5f15\u7528<\/em>&nbsp;\u4f7f\u7528\u7684\u8bed\u6cd5\u548c Python \u4e2d\u6240\u6709\u7684\u5c5e\u6027\u5f15\u7528\u4e00\u6837. \u5408\u6cd5\u7684\u5c5e\u6027\u540d\u662f\u90a3\u4e9b\u5728\u7c7b\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u540d\u5b57. \u6240\u4ee5\u4e00\u4e2a\u7c7b\u5b9a\u4e49\u5982\u679c\u662f\u8fd9\u6837:<strong>class<\/strong><strong>MyClass<\/strong>: <em>&#8220;&#8221;&#8221;A simple example class&#8221;&#8221;&#8221;<\/em> i = 12345 <strong>def<\/strong> f(self): <strong>return<\/strong> &#8216;hello world&#8217;<\/p>\n\n\n\n<p>\u90a3\u4e48,&nbsp;<code>MyClass.i<\/code>&nbsp;\u548c&nbsp;<code>MyClass.f<\/code>&nbsp;\u5c31\u662f\u5408\u6cd5\u7684\u5c5e\u6027\u5f15\u7528, \u5206\u522b\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u548c\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61. \u7c7b\u5c5e\u6027\u4e5f\u53ef\u4ee5\u88ab\u6307\u5b9a, \u6240\u4ee5\u4f60\u53ef\u4ee5\u7ed9&nbsp;<code>MyClass.i<\/code>&nbsp;\u8d4b\u503c\u4ee5\u6539\u53d8\u5176\u6570\u503c.&nbsp;<code>__doc__<\/code>&nbsp;\u4e5f\u662f\u4e00\u4e2a\u5408\u6cd5\u7684\u5c5e\u6027, \u8fd4\u56de\u5c5e\u4e8e\u8fd9\u4e2a\u7c7b\u7684 docstring :&nbsp;<code>\"A&nbsp;simple&nbsp;example&nbsp;class\"<\/code>.<\/p>\n\n\n\n<p>\u7c7b\u7684&nbsp;<em>\u5b9e\u4f8b\u5316<\/em>&nbsp;\u4f7f\u7528\u51fd\u6570\u7684\u5f62\u5f0f. \u53ea\u8981\u5f53\u4f5c\u4e00\u4e2a\u65e0\u53c2\u7684\u51fd\u6570\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u5c31\u53ef\u4ee5\u4e86. \u6bd4\u5982 (\u5047\u8bbe\u6709\u524d\u9762\u7684\u7c7b\u4e86):x = MyClass()<\/p>\n\n\n\n<p>\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b, \u5e76\u4e14\u5c06\u5176\u6307\u5b9a\u7ed9\u5c40\u90e8\u53d8\u91cf&nbsp;<code>x<\/code>.<\/p>\n\n\n\n<p>\u5b9e\u4f8b\u5316\u7684\u64cd\u4f5c (\u201c\u8c03\u7528\u201d \u4e00\u4e2a\u7c7b\u5bf9\u8c61) \u521b\u5efa\u4e86\u7a7a\u7684\u5bf9\u8c61. \u5728\u521b\u5efa\u5b9e\u4f8b\u65f6, \u5f88\u591a\u7c7b\u53ef\u80fd\u90fd\u9700\u8981\u6709\u7279\u5b9a\u7684\u521d\u59cb\u72b6\u6001. \u6240\u4ee5\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7279\u6b8a\u7684\u65b9\u6cd5, \u79f0\u4e3a&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/datamodel.html#object.__init__\"><code>__init__()<\/code><\/a>, \u50cf\u8fd9\u6837:<strong>def<\/strong> __init__(self): self.data = []<\/p>\n\n\n\n<p>\u5f53\u4e00\u4e2a\u7c7b\u5b9a\u4e49\u4e86&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/datamodel.html#object.__init__\"><code>__init__()<\/code><\/a>&nbsp;\u65b9\u6cd5, \u7c7b\u5728\u5b9e\u4f8b\u5316\u65f6\u4f1a\u81ea\u52a8\u8c03\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/datamodel.html#object.__init__\"><code>__init__()<\/code><\/a>&nbsp;\u65b9\u6cd5, \u7528\u4e8e\u521b\u5efa\u65b0\u7684\u7c7b\u5b9e\u4f8b. \u6240\u4ee5\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d, \u4e00\u4e2a\u65b0\u7684\u521d\u59cb\u5316\u8fc7\u7684\u5b9e\u4f8b\u88ab\u521b\u5efa:x = MyClass()<\/p>\n\n\n\n<p>\u5f53\u7136, \u4e3a\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027, \u65b9\u6cd5&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/datamodel.html#object.__init__\"><code>__init__()<\/code><\/a>&nbsp;\u53ef\u4ee5\u6709\u66f4\u591a\u7684\u53c2\u6570. \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b, \u7ed9\u7c7b\u7684\u53c2\u6570\u4f1a\u4f20\u7ed9&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/datamodel.html#object.__init__\"><code>__init__()<\/code><\/a>. \u4f8b\u5982,<strong>&gt;&gt;&gt; <\/strong><strong>class<\/strong><strong>Complex<\/strong>: <strong>&#8230; <\/strong><strong>def<\/strong> __init__(self, realpart, imagpart): <strong>&#8230;<\/strong> self.r = realpart <strong>&#8230;<\/strong> self.i = imagpart <strong>&#8230;<\/strong><strong>&gt;&gt;&gt; <\/strong>x = Complex(3.0, -4.5) <strong>&gt;&gt;&gt; <\/strong>x.r, x.i (3.0, -4.5)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9.3.3. \u5b9e\u4f8b\u5bf9\u8c61<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-instanceobjects\"><\/a><\/h3>\n\n\n\n<p>\u90a3\u4e48\u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u5bf9\u5b9e\u4f8b\u5bf9\u8c61\u505a\u4ec0\u4e48? \u5b9e\u4f8b\u5bf9\u8c61\u552f\u4e00\u80fd\u7406\u89e3\u7684\u64cd\u4f5c\u5c31\u662f\u5c5e\u6027\u5f15\u7528. \u6709\u4e24\u79cd\u5408\u6cd5\u7684\u5c5e\u6027, \u6570\u636e\u5c5e\u6027\u548c\u65b9\u6cd5.<\/p>\n\n\n\n<p><em>data attribute<\/em>&nbsp;\u5728 Smalltalk \u4e2d\u76f8\u5e94\u4e8e \u201cinstance variable\u201d, \u5728 C++ \u4e2d\u76f8\u5e94\u4e8e \u201cdata member\u201d. \u6570\u636e\u5c5e\u6027\u4e0d\u9700\u8981\u58f0\u660e; \u50cf\u5c40\u90e8\u53d8\u91cf, \u5f53\u5b83\u4eec\u7b2c\u4e00\u6b21\u6307\u5b9a\u65f6\u5c31\u4f1a\u88ab\u5f15\u5165. \u6bd4\u5982, \u5982\u679c&nbsp;<code>x<\/code>&nbsp;\u662f\u524d\u9762\u521b\u5efa\u7684&nbsp;<code>MyClass<\/code>&nbsp;\u7684\u5b9e\u4f8b, \u90a3\u4e48\u4e0b\u9762\u7684\u4f8b\u5b50\u5c31\u4f1a\u6253\u5370\u51fa&nbsp;<code>16<\/code>, \u800c\u4e0d\u4f1a\u6709\u95ee\u9898:x.counter = 1 <strong>while<\/strong> x.counter &lt; 10: x.counter = x.counter * 2 print(x.counter) <strong>del<\/strong> x.counter<\/p>\n\n\n\n<p>\u5b9e\u4f8b\u5c5e\u6027\u5f15\u7528\u7684\u53e6\u4e00\u79cd\u662f\u65b9\u6cd5. \u4e00\u4e2a\u65b9\u6cd5\u5c31\u662f \u201c\u5c5e\u4e8e\u201d \u4e00\u4e2a\u5bf9\u8c61\u7684\u51fd\u6570. (\u5728 Python \u4e2d, \u65b9\u6cd5\u7684\u6982\u5ff5\u5e76\u4e0d\u662f\u7c7b\u5b9e\u4f8b\u6240\u7279\u6709: \u5176\u4ed6\u5bf9\u8c61\u7c7b\u578b\u4e5f\u53ef\u4ee5\u6709\u65b9\u6cd5. \u4f8b\u5982, \u5217\u8868\u5bf9\u8c61\u6709 append, insert, remove, sort, \u53ca\u7b49\u7b49\u7684\u65b9\u6cd5. \u4f46\u662f, \u5728\u4e0b\u9762\u7684\u8ba8\u8bba\u4e2d, \u6211\u4eec\u6307\u7684\u5c31\u662f\u7c7b\u5b9e\u4f8b\u5bf9\u8c61\u7684\u65b9\u6cd5, \u9664\u975e\u7279\u522b\u6307\u51fa.)<\/p>\n\n\n\n<p id=\"index-0\">\u5408\u6cd5\u7684\u65b9\u6cd5\u540d\u4f9d\u8d56\u4e8e\u5b9e\u4f8b\u7684\u7c7b. \u5728\u5b9a\u4e49\u4e2d, \u7c7b\u7684\u5c5e\u6027\u5982\u679c\u662f\u90a3\u4e9b\u5b9a\u4e49\u7684\u51fd\u6570\u5bf9\u8c61, \u800c\u8fd9\u4e5f\u5c31\u662f\u5b9e\u4f8b\u7684\u65b9\u6cd5. \u6240\u4ee5\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d,&nbsp;<code>x.f<\/code>&nbsp;\u662f\u4e00\u4e2a\u5408\u6cd5\u7684\u65b9\u6cd5\u5f15\u7528, \u56e0\u4e3a&nbsp;<code>MyClass.f<\/code>&nbsp;\u662f\u4e00\u4e2a\u51fd\u6570, \u4f46\u662f&nbsp;<code>x.i<\/code>&nbsp;\u5c31\u4e0d\u662f, \u56e0\u4e3a&nbsp;<code>MyClass.i<\/code>&nbsp;\u5c31\u4e0d\u662f. \u4f46\u662f&nbsp;<code>x.f<\/code>&nbsp;\u548c&nbsp;<code>MyClass.f<\/code>&nbsp;\u5e76\u4e0d\u4e00\u6837 \u2014 \u5b83\u662f\u4e00\u4e2a&nbsp;<em>method object<\/em>, \u800c\u4e0d\u662f&nbsp;<em>function object<\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9.3.4. \u65b9\u6cd5\u5bf9\u8c61<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-methodobjects\"><\/a><\/h3>\n\n\n\n<p>\u901a\u5e38, \u4e00\u4e2a\u65b9\u6cd5\u5728\u5176\u7ed1\u5b9a\u540e\u5c31\u53ef\u4ee5\u8c03\u7528\u4e86:x.f()<\/p>\n\n\n\n<p>\u5728&nbsp;<code>MyClass<\/code>&nbsp;\u8fd9\u4e2a\u4f8b\u5b50\u4e2d, \u8fd9\u5c06\u4f1a\u8fd4\u56de\u5b57\u7b26\u4e32&nbsp;<code>'hello&nbsp;world'<\/code>. \u4f46\u662f, \u50cf\u8fd9\u6837\u7684\u8c03\u7528\u5e76\u4e0d\u662f\u5fc5\u987b\u7684:&nbsp;<code>x.f<\/code>&nbsp;\u662f\u4e00\u4e2a\u65b9\u6cd5\u5bf9\u8c61, \u5b83\u53ef\u4ee5\u88ab\u4fdd\u5b58\u8d77\u6765\u4ee5\u4f9b\u4e0b\u6b21\u8c03\u7528. \u4f8b\u5982:xf = x.f <strong>while<\/strong><strong>True<\/strong>: print(xf())<\/p>\n\n\n\n<p>\u5c06\u4f1a\u6301\u7eed\u7684\u6253\u5370&nbsp;<code>'hello&nbsp;world'<\/code>.<\/p>\n\n\n\n<p>\u90a3\u4e48\u5728\u65b9\u6cd5\u8c03\u7528\u662f\u53d1\u751f\u4e86\u4ec0\u4e48? \u4f60\u53ef\u80fd\u6ce8\u610f\u5230&nbsp;<code>x.f()<\/code>&nbsp;\u8c03\u7528\u65f6\u5e76\u6ca1\u6709\u53c2\u6570, \u5c3d\u7ba1&nbsp;<code>f()<\/code>&nbsp;\u5b9a\u4e49\u65f6\u662f\u6709\u4e00\u4e2a\u53c2\u6570\u7684. \u90a3\u4e48\u8fd9\u4e2a\u53c2\u6570\u600e\u4e48\u4e86? \u5f53\u7136, Python \u5728\u4e00\u4e2a\u53c2\u6570\u7f3a\u5c11\u65f6\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u662f\u4f1a\u53d1\u751f\u5f02\u5e38\u7684 \u2014 \u5c31\u7b97\u8fd9\u4e2a\u53c2\u6570\u6ca1\u6709\u771f\u6b63\u7528\u5230&#8230;<\/p>\n\n\n\n<p>\u4e8b\u5b9e\u4e0a, \u4f60\u4f1a\u731c\u60f3\u5230: \u5173\u4e8e\u65b9\u6cd5, \u7279\u6b8a\u7684\u4e1c\u897f\u5c31\u662f, \u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u4e86\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570. \u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d,&nbsp;<code>x.f()<\/code>&nbsp;\u662f\u4e25\u683c\u7b49\u4ef7\u4e8e&nbsp;<code>MyClass.f(x)<\/code>. \u5728\u591a\u6570\u60c5\u51b5\u4e0b, \u8c03\u7528\u4e00\u4e2a\u65b9\u6cd5 (\u6709\u4e2a&nbsp;<em>n<\/em>&nbsp;\u4e2a\u53c2\u6570), \u548c\u8c03\u7528\u76f8\u5e94\u7684\u51fd\u6570 (\u4e5f\u6709\u90a3 n \u4e2a\u53c2\u6570, \u4f46\u662f\u518d\u989d\u5916\u52a0\u5165\u4e00\u4e2a\u4f7f\u7528\u8be5\u65b9\u6cd5\u7684\u5bf9\u8c61), \u662f\u7b49\u4ef7\u7684.<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u4ecd\u7136\u4e0d\u77e5\u9053\u65b9\u6cd5\u5982\u4f55\u5de5\u4f5c, \u90a3\u4e48\u770b\u770b\u5b9e\u73b0\u6216\u8bb8\u4f1a\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898. \u5f53\u4e00\u4e2a\u5b9e\u4f8b\u5c5e\u6027\u88ab\u5f15\u7528\u65f6, \u4f46\u662f\u4e0d\u662f\u6570\u636e\u5c5e\u6027, \u90a3\u4e48\u5b83\u7684\u7c7b\u5c06\u88ab\u641c\u7d22. \u5982\u679c\u8be5\u540d\u5b57\u4ee3\u8868\u4e00\u4e2a\u5408\u6cd5\u7684\u7c7b\u5c5e\u6027\u5e76\u4e14\u662f\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61, \u4e00\u4e2a\u65b9\u6cd5\u5bf9\u8c61\u5c31\u4f1a\u88ab\u521b\u5efa, \u901a\u8fc7\u5305\u88c5 (\u6307\u5411) \u5b9e\u4f8b\u5bf9\u8c61, \u800c\u51fd\u6570\u5bf9\u8c61\u4ecd\u7136\u53ea\u662f\u5728\u62bd\u8c61\u7684\u5bf9\u8c61\u4e2d: \u8fd9\u5c31\u662f\u65b9\u6cd5\u5bf9\u8c61. \u5f53\u65b9\u6cd5\u5bf9\u8c61\u7528\u4e00\u4e2a\u53c2\u6570\u5217\u8868\u8c03\u7528, \u65b0\u7684\u53c2\u6570\u5217\u8868\u4f1a\u4ece\u5b9e\u4f8b\u5bf9\u8c61\u4e2d\u91cd\u65b0\u6784\u5efa, \u7136\u540e\u51fd\u6570\u5bf9\u8c61\u5219\u8c03\u7528\u65b0\u7684\u53c2\u6570\u5217\u8868.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9.4. \u968f\u673a\u5907\u6ce8<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-remarks\"><\/a><\/h2>\n\n\n\n<p>\u6570\u636e\u5c5e\u6027\u8986\u5199\u4e86\u540c\u540d\u7684\u65b9\u6cd5\u5c5e\u6027; \u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u5076\u7136\u7684\u540d\u5b57\u51b2\u7a81, \u5728\u5927\u578b\u7684\u7a0b\u5e8f\u4e2d\u8fd9\u4f1a\u5bfc\u81f4\u5f88\u96be\u5bfb\u627e\u7684 bug, \u4f7f\u7528\u67d0\u4e9b\u547d\u540d\u7ea6\u5b9a\u662f\u975e\u5e38\u660e\u667a\u7684, \u8fd9\u6837\u53ef\u4ee5\u6700\u5c0f\u7684\u907f\u514d\u51b2\u7a81. \u53ef\u80fd\u7684\u7ea6\u5b9a\u5305\u62ec\u5927\u5199\u65b9\u6cd5\u540d\u79f0, \u5728\u6570\u636e\u7c7b\u578b\u524d\u589e\u52a0\u7279\u6b8a\u7684\u524d\u7f00 (\u6216\u8005\u5c31\u662f\u4e00\u4e2a\u4e0b\u5212\u7ebf), \u6216\u5bf9\u4e8e\u65b9\u6cd5\u4f7f\u7528\u52a8\u8bcd, \u800c\u6570\u636e\u6210\u5458\u5219\u4f7f\u7528\u540d\u8bcd.<\/p>\n\n\n\n<p>\u6570\u636e\u5c5e\u6027\u53ef\u4ee5\u88ab\u8be5\u7c7b\u7684\u65b9\u6cd5\u6216\u8005\u666e\u901a\u7684\u7528\u6237 (\u201c\u5ba2\u6237\u201d) \u5f15\u7528. \u6362\u53e5\u8bdd\u8bf4, \u7c7b\u662f\u4e0d\u80fd\u5b9e\u73b0\u5b8c\u5168\u7684\u62bd\u8c61\u6570\u636e\u7c7b\u578b. \u4e8b\u5b9e\u4e0a, \u5728 Python \u4e2d\u6ca1\u6709\u4efb\u4f55\u4e1c\u897f\u662f\u5f3a\u5236\u9690\u85cf\u7684 \u2014 \u8fd9\u5b8c\u5168\u662f\u57fa\u4e8e\u7ea6\u5b9a. (\u5728\u53e6\u4e00\u65b9\u9762, Python \u662f\u7528 C \u5b9e\u73b0\u7684, \u8fd9\u6837\u5c31\u53ef\u4ee5\u5b9e\u73b0\u7ec6\u8282\u7684\u9690\u85cf\u548c\u63a7\u5236\u8bbf\u95ee; \u8fd9\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199 Python \u7684\u6269\u5c55\u5b9e\u73b0.)<\/p>\n\n\n\n<p>\u5ba2\u6237\u9700\u8981\u5c0f\u5fc3\u5730\u4f7f\u7528\u6570\u636e\u5c5e\u6027 \u2014 \u5ba2\u6237\u4f1a\u5f04\u4e71\u88ab\u65b9\u6cd5\u63a7\u5236\u7684\u4e0d\u53d8\u91cf, \u901a\u8fc7\u4f7f\u7528\u5b83\u4eec\u81ea\u5df1\u7684\u65b9\u6cd5\u5c5e\u6027. \u6ce8\u610f\u7528\u6237\u53ef\u4ee5\u589e\u52a0\u5b83\u4eec\u81ea\u5df1\u7684\u6570\u636e\u5230\u5b9e\u4f8b\u5bf9\u8c61\u4e0a, \u800c\u6ca1\u6709\u68c0\u67e5\u6709\u6ca1\u6709\u5f71\u54cd\u65b9\u6cd5\u7684\u6709\u6548\u6027, \u53ea\u8981\u907f\u514d\u540d\u5b57\u51b2\u7a81 \u2013 \u5728\u8bf4\u4e00\u6b21, \u547d\u540d\u7ea6\u5b9a\u53ef\u4ee5\u907f\u514d\u5f88\u591a\u8fd9\u6837\u4ee4\u4eba\u5934\u75bc\u7684\u95ee\u9898.<\/p>\n\n\n\n<p>\u5728\u5f15\u7528\u6570\u636e\u5c5e\u6027 (\u6216\u5176\u4ed6\u65b9\u6cd5 !) \u5e76\u6ca1\u6709\u5feb\u901f\u7684\u65b9\u6cd5. \u6211\u53d1\u73b0\u8fd9\u7684\u786e\u589e\u52a0\u4e86\u65b9\u6cd5\u7684\u53ef\u8bfb\u6027: \u8fd9\u6837\u5c31\u4e0d\u4f1a\u88ab\u5c40\u90e8\u53d8\u91cf\u548c\u5b9e\u4f8b\u4e2d\u7684\u53d8\u91cf\u6240\u56f0\u60d1, \u7279\u522b\u662f\u5728\u968f\u4fbf\u770b\u770b\u4e00\u4e2a\u65b9\u6cd5\u65f6.<\/p>\n\n\n\n<p>\u901a\u5e38, \u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u79f0\u4e3a&nbsp;<code>self<\/code>. \u8fd9\u66f4\u591a\u7684\u53ea\u662f\u7ea6\u5b9a:&nbsp;<code>self<\/code>&nbsp;\u5bf9\u4e8e Python \u6765\u8bf4\u6ca1\u6709\u4efb\u4f55\u610f\u4e49. \u4f46\u6ce8\u610f, \u5982\u679c\u4e0d\u9075\u5faa\u8fd9\u4e2a\u7ea6\u5b9a, \u5bf9\u4e8e\u5176\u4ed6\u7684\u7a0b\u5e8f\u5458\u6765\u8bf4\u5c31\u6bd4\u8f83\u96be\u4ee5\u7406\u89e3\u4e86, \u4e00\u4e2a&nbsp;<em>class browser<\/em>&nbsp;\u7a0b\u5e8f\u53ef\u80fd\u4f1a\u4f9d\u8d56\u6b64\u7ea6\u5b9a.<\/p>\n\n\n\n<p>\u4f5c\u4e3a\u7c7b\u5c5e\u6027\u7684\u4efb\u4f55\u51fd\u6570\u5bf9\u8c61, \u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b9\u6cd5\u7528\u4e8e\u90a3\u4e2a\u7c7b\u7684\u5b9e\u4f8b. \u51fd\u6570\u662f\u5426\u5728\u4e00\u4e2a\u7c7b\u4f53\u4e2d\u5176\u5b9e\u5e76\u4e0d\u91cd\u8981: \u6307\u5b9a\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\u7ed9\u7c7b\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\u4e5f\u662f\u53ef\u4ee5\u7684. \u4f8b\u5982:<em># Function defined outside the class<\/em><strong>def<\/strong> f1(self, x, y): <strong>return<\/strong> min(x, x+y) <strong>class<\/strong><strong>C<\/strong>: f = f1 <strong>def<\/strong> g(self): <strong>return<\/strong> &#8216;hello world&#8217; h = g<\/p>\n\n\n\n<p>\u73b0\u5728&nbsp;<code>f<\/code>,&nbsp;<code>g<\/code>&nbsp;\u548c&nbsp;<code>h<\/code>&nbsp;\u90fd\u662f\u7c7b&nbsp;<code>C<\/code>&nbsp;\u7684\u5c5e\u6027, \u5e76\u4e14\u6307\u5411\u51fd\u6570\u5bf9\u8c61, \u800c\u4e14\u90fd\u662f\u7c7b&nbsp;<code>C<\/code>&nbsp;\u5b9e\u4f8b\u7684\u65b9\u6cd5 \u2014&nbsp;<code>h<\/code>&nbsp;\u548c&nbsp;<code>g<\/code>&nbsp;\u662f\u7b49\u4ef7\u7684. \u6ce8\u610f\u8fd9\u4e2a\u53ea\u4f1a\u662f\u8bfb\u8005\u611f\u5230\u56f0\u60d1.<\/p>\n\n\n\n<p>\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528&nbsp;<code>self<\/code>&nbsp;\u53c2\u6570\u8c03\u7528\u5176\u4ed6\u7684\u65b9\u6cd5:<strong>class<\/strong><strong>Bag<\/strong>: <strong>def<\/strong> __init__(self): self.data = [] <strong>def<\/strong> add(self, x): self.data.append(x) <strong>def<\/strong> addtwice(self, x): self.add(x) self.add(x)<\/p>\n\n\n\n<p>\u65b9\u6cd5\u53ef\u4ee5\u5f15\u7528\u5168\u5c40\u53d8\u91cf, \u5c31\u50cf\u666e\u901a\u51fd\u6570\u4e2d\u90a3\u6837. \u4e0e\u8fd9\u4e2a\u65b9\u6cd5\u76f8\u5173\u7684\u5168\u5c40\u4f5c\u7528\u57df, \u662f\u5305\u542b\u90a3\u4e2a\u7c7b\u5b9a\u4e49\u7684\u6a21\u5757. (\u7c7b\u672c\u8eab\u6c38\u8fdc\u4e0d\u4f1a\u4f5c\u4e3a\u5168\u5c40\u4f5c\u7528\u57df\u4f7f\u7528.) \u5982\u679c\u7684\u786e\u9700\u8981\u5728\u65b9\u6cd5\u4e2d\u4f7f\u7528\u5168\u5c40\u6570\u636e, \u90a3\u4e48\u9700\u8981\u5408\u6cd5\u7684\u4f7f\u7528: \u9996\u5148\u4e00\u4ef6\u4e8b, \u88ab\u5bfc\u5165\u5168\u5c40\u4f5c\u7528\u57df\u7684\u51fd\u6570\u548c\u6a21\u5757\u53ef\u4ee5\u88ab\u65b9\u6cd5\u4f7f\u7528, \u5c31\u5982\u5b9a\u4e49\u5728\u91cc\u9762\u7684\u51fd\u6570\u548c\u7c7b\u4e00\u6837. \u901a\u5e38\u6765\u8bf4, \u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d, \u5305\u542b\u65b9\u6cd5\u7684\u7c7b\u662f\u5b83\u81ea\u5df1\u672c\u8eab, \u5e76\u4e14\u5728\u540e\u9762\u6211\u4eec\u4f1a\u77e5\u9053\u4e3a\u4f55\u65b9\u6cd5\u5e94\u8be5\u5f15\u7528\u81ea\u5df1\u7684\u7c7b.<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u503c\u90fd\u662f\u4e00\u4e2a\u5bf9\u8c61, \u6240\u4ee5\u5bf9\u4e8e&nbsp;<em>class<\/em>&nbsp;(\u6216\u79f0\u4e3a\u5b83\u7684&nbsp;<em>type<\/em>) \u4e5f\u662f\u8fd9\u6837. \u5b83\u5b58\u4e8e&nbsp;<code>object.__class__<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9.5. \u7ee7\u627f<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-inheritance\"><\/a><\/h2>\n\n\n\n<p>\u5f53\u7136, \u4e00\u4e2a\u6709 \u201cclass\u201d \u7684\u8bed\u8a00\u5982\u679c\u6ca1\u6709\u7ee7\u627f\u5c31\u6ca1\u6709\u591a\u5927\u7684\u4ef7\u503c\u4e86. \u6d3e\u751f\u7c7b\u7684\u5b9a\u4e49\u5982\u4e0b:<strong>class<\/strong><strong>DerivedClassName<\/strong>(BaseClassName): &lt;statement-1&gt; . . . &lt;statement-N&gt;<\/p>\n\n\n\n<p><code>BaseClassName<\/code>&nbsp;\u7684\u5b9a\u4e49\u5bf9\u4e8e\u6d3e\u751f\u7c7b\u800c\u8a00\u5fc5\u987b\u662f\u53ef\u89c1\u7684. \u5728\u57fa\u7c7b\u7684\u5730\u65b9, \u4efb\u610f\u7684\u8868\u8fbe\u5f0f\u90fd\u662f\u5141\u8bb8\u7684. \u8fd9\u5c31\u4f1a\u975e\u5e38\u6709\u7528, \u6bd4\u5982\u57fa\u7c7b\u5b9a\u4e49\u5728\u53e6\u4e00\u4e2a\u6a21\u5757:<strong>class<\/strong><strong>DerivedClassName<\/strong>(modname.BaseClassName):<\/p>\n\n\n\n<p>\u6d3e\u751f\u7c7b\u5c31\u53ef\u4ee5\u50cf\u57fa\u7c7b\u4e00\u6837\u4f7f\u7528. \u5f53\u4e00\u4e2a\u7c7b\u88ab\u6784\u5efa, \u90a3\u4e48\u5b83\u5c31\u4f1a\u8bb0\u4e0b\u57fa\u7c7b. \u8fd9\u662f\u7528\u4e8e\u89e3\u51b3\u5c5e\u6027\u5f15\u7528\u7684\u95ee\u9898: \u5f53\u4e00\u4e2a\u5c5e\u6027\u5728\u8fd9\u4e2a\u7c7b\u4e2d\u6ca1\u6709\u88ab\u627e\u5230, \u90a3\u4e48\u5c31\u4f1a\u53bb\u57fa\u7c7b\u4e2d\u5bfb\u627e. \u7136\u540e\u641c\u7d22\u5c31\u4f1a\u9012\u5f52, \u56e0\u4e3a\u5982\u679c\u57fa\u7c7b\u672c\u8eab\u4e5f\u662f\u4ece\u5176\u4ed6\u7684\u6d3e\u751f.<\/p>\n\n\n\n<p>\u5b9e\u4f8b\u5316\u4e00\u4e2a\u6d3e\u751f\u7c7b\u6ca1\u6709\u4ec0\u4e48\u7279\u522b:&nbsp;<code>DerivedClassName()<\/code>&nbsp;\u4f1a\u521b\u5efa\u8fd9\u4e2a\u7c7b\u7684\u65b0\u5b9e\u4f8b. \u65b9\u6cd5\u7684\u5f15\u7528\u5982\u4e0b: \u76f8\u5e94\u7684\u7c7b\u7684\u5c5e\u6027\u4f1a\u88ab\u641c\u5bfb, \u5982\u679c\u9700\u8981\u56de\u53bb\u641c\u5bfb\u57fa\u7c7b, \u5982\u679c\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61, \u90a3\u4e48\u8fd9\u4e2a\u5f15\u7528\u5c31\u662f\u5408\u6cd5\u7684.<\/p>\n\n\n\n<p>\u6d3e\u751f\u7c7b\u4f1a\u8986\u5199\u57fa\u7c7b\u7684\u65b9\u6cd5. \u56e0\u4e3a\u5f53\u8c03\u7528\u540c\u6837\u7684\u5bf9\u8c61\u7684\u5176\u4ed6\u65b9\u6cd5\u65f6\u65b9\u6cd5\u5e76\u6ca1\u6709\u4ec0\u4e48\u7279\u522b\u7684, \u57fa\u7c7b\u7684\u65b9\u6cd5\u4f1a\u56e0\u4e3a\u5148\u8c03\u7528\u6d3e\u751f\u7c7b\u7684\u65b9\u6cd5\u800c\u88ab\u8986\u5199. (\u5bf9\u4e8e C++ \u7a0b\u5e8f\u5458: \u6240\u6709\u7684\u65b9\u6cd5\u5728 Python \u4e2d\u90fd\u662f&nbsp;<code>vitual<\/code>&nbsp;\u7684.)<\/p>\n\n\n\n<p>\u4e00\u4e2a\u5728\u6d3e\u751f\u7c7b\u4e2d\u8986\u5199\u7684\u65b9\u6cd5\u53ef\u80fd\u9700\u8981\u57fa\u7c7b\u7684\u65b9\u6cd5. \u6700\u7b80\u5355\u7684\u65b9\u5f0f\u5c31\u662f\u76f4\u63a5\u8c03\u7528\u57fa\u7c7b\u7684\u65b9\u6cd5: \u8c03\u7528&nbsp;<code>BaseClassName.methodname(self,&nbsp;arguments)<\/code>. \u8fd9\u5bf9\u4e8e\u53ef\u7eed\u6765\u8bf4\u4e5f\u662f\u5f88\u65b9\u4fbf\u7684. (\u8fd9\u4ec5\u5728&nbsp;<code>BaseClassName<\/code>&nbsp;\u53ef\u8bbf\u95ee\u65f6\u624d\u6709\u6548.)<\/p>\n\n\n\n<p>Python \u6709\u4e24\u4e2a\u5185\u7f6e\u51fd\u6570\u7528\u4e8e\u7ee7\u627f:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#isinstance\"><code>isinstance()<\/code><\/a>&nbsp;\u68c0\u67e5\u5b9e\u4f8b\u7684\u7c7b\u578b:&nbsp;<code>isinstance(obj,&nbsp;int)<\/code>&nbsp;\u53ea\u6709\u5728&nbsp;<code>obj.__class__<\/code>&nbsp;\u662f&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#int\"><code>int<\/code><\/a>&nbsp;\u6216\u5176\u6d3e\u751f\u7c7b\u65f6\u624d\u4e3a&nbsp;<code>True<\/code>.<\/li>\n\n\n\n<li>\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#issubclass\"><code>issubclass()<\/code><\/a>&nbsp;\u7528\u4e8e\u68c0\u67e5\u7c7b\u7684\u7ee7\u627f\u5173\u7cfb:&nbsp;<code>issubclass(bool,&nbsp;int)<\/code>&nbsp;\u4f1a\u8fd4\u56de&nbsp;<code>True<\/code>, \u56e0\u4e3a&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#bool\"><code>bool<\/code><\/a>&nbsp;\u662f&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#int\"><code>int<\/code><\/a>&nbsp;\u7684\u6d3e\u751f\u7c7b. \u4f46\u662f,&nbsp;<code>issubclass(float,&nbsp;int)<\/code>&nbsp;\u4f1a\u662f&nbsp;<code>False<\/code>&nbsp;\u56e0\u4e3a&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#float\"><code>float<\/code><\/a>&nbsp;\u5e76\u4e0d\u662f&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#int\"><code>int<\/code><\/a>&nbsp;\u7684\u6d3e\u751f\u7c7b.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">9.5.1. \u591a\u91cd\u7ee7\u627f<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-multiple\"><\/a><\/h3>\n\n\n\n<p>Python \u652f\u6301\u591a\u91cd\u7ee7\u627f. \u4e00\u4e2a\u591a\u91cd\u7ee7\u627f\u7684\u7c7b\u5b9a\u4e49\u770b\u8d77\u6765\u50cf\u8fd9\u6837:<strong>class<\/strong><strong>DerivedClassName<\/strong>(Base1, Base2, Base3): &lt;statement-1&gt; . . . &lt;statement-N&gt;<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u5927\u591a\u6570\u76ee\u7684, \u5728\u6700\u7b80\u5355\u7684\u60c5\u51b5\u4e0b, \u4f60\u53ef\u4ee5\u5c06\u5c5e\u6027\u641c\u5bfb\u7684\u65b9\u5f0f\u662f, \u4ece\u4e0b\u81f3\u4e0a, \u4ece\u5de6\u5230\u53f3, \u5728\u7ee7\u627f\u4f53\u7cfb\u4e2d, \u540c\u6837\u7684\u7c7b\u53ea\u4f1a\u88ab\u641c\u5bfb\u4e00\u6b21. \u5982\u679c\u4e00\u4e2a\u5c5e\u6027\u5728&nbsp;<code>DerivedClassName<\/code>&nbsp;\u4e2d\u6ca1\u6709\u88ab\u627e\u5230, \u5b83\u5c31\u4f1a\u641c\u5bfb&nbsp;Base1, \u7136\u540e (\u9012\u5f52\u5730) \u641c\u5bfb&nbsp;<code>Base1<\/code>&nbsp;\u7684\u57fa\u7c7b, \u7136\u540e\u5982\u679c\u8fd8\u662f\u6ca1\u6709\u627e\u5230, \u90a3\u4e48\u5c31\u4f1a\u641c\u7d22&nbsp;<code>Base2<\/code>, \u7b49\u7b49.<\/p>\n\n\n\n<p>\u4e8b\u5b9e\u4e0a, \u8fd9\u66f4\u52a0\u7684\u590d\u6742; \u65b9\u6cd5\u7684\u641c\u5bfb\u987a\u5e8f\u4f1a\u6839\u636e\u8c03\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#super\"><code>super()<\/code><\/a>&nbsp;\u800c\u53d8\u5316. \u8fd9\u4e2a\u65b9\u6cd5\u5728\u67d0\u4e9b\u5176\u4ed6\u591a\u91cd\u7ee7\u627f\u7684\u8bed\u8a00\u4e2d\u4ee5 call-next-method \u88ab\u719f\u77e5, \u800c\u4e14\u6bd4\u5355\u7ee7\u627f\u7684\u8bed\u8a00\u4e2d\u8981\u6709\u7528.<\/p>\n\n\n\n<p>\u52a8\u6001\u7684\u987a\u5e8f\u662f\u5f88\u6709\u5fc5\u8981\u7684, \u56e0\u4e3a\u5728\u90a3\u4e9b\u5904\u4e8e\u83f1\u5f62\u7ee7\u627f\u4f53\u7cfb\u4e2d (\u8fd9\u91cc\u81f3\u5c11\u6709\u4e2a\u7236\u7c7b\u88ab\u591a\u6b21\u6d3e\u751f). \u6bd4\u5982, \u6240\u6709\u7684\u7c7b\u90fd\u4ece&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#object\"><code>object<\/code><\/a>&nbsp;\u6d3e\u751f, \u6240\u4ee5\u5230\u8fbe&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#object\"><code>object<\/code><\/a>&nbsp;\u7684\u8def\u5f84\u4e0d\u6b62\u4e00\u6761. \u4e3a\u4e86\u9632\u6b62\u57fa\u7c7b\u88ab\u591a\u6b21\u8bbf\u95ee, \u52a8\u6001\u7684\u7b97\u6cd5\u7ebf\u6027\u5316\u4e86\u641c\u5bfb\u7684\u8def\u5f84, \u5148\u4ece\u5de6\u81f3\u53f3\u641c\u7d22\u6307\u5b9a\u7684\u7c7b, \u7136\u540e\u8fd9\u6837\u5c31\u53ef\u4ee5\u8ba9\u6bcf\u4e2a\u7236\u7c7b\u53ea\u641c\u5bfb\u4e00\u6b21, \u5e76\u4e14\u5355\u4e00 (\u8fd9\u5c31\u610f\u5473\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u88ab\u6d3e\u751f, \u4f46\u662f\u4e0d\u4f1a\u5f71\u54cd\u5176\u7236\u7c7b\u7684\u641c\u5bfb\u8def\u5f84. \u4f7f\u7528\u4e86\u8fd9\u4e9b, \u5c31\u4f7f\u5f97\u4ee5\u591a\u91cd\u7ee7\u627f\u8bbe\u8ba1\u7684\u7c7b\u66f4\u53ef\u9760\u548c\u53ef\u6269\u5c55. \u5177\u4f53\u53c2\u8003http:\/\/www.python.org\/download\/releases\/2.3\/mro\/.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9.6. \u79c1\u6709\u53d8\u91cf<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-private\"><\/a><\/h2>\n\n\n\n<p>\u5728 Python \u4e4b\u4e2d, \u5e76\u4e0d\u5b58\u5728\u90a3\u79cd\u65e0\u6cd5\u8bbf\u95ee\u7684 \u201c\u79c1\u6709\u201d \u53d8\u91cf. \u4f46\u662f, \u5728\u591a\u6570\u7684 Python \u4ee3\u7801\u4e2d\u6709\u4e2a\u7ea6\u5b9a: \u4ee5\u4e00\u4e2a\u4e0b\u5212\u7ebf\u5e26\u5934\u7684\u540d\u5b57 (\u5982&nbsp;<code>_spam<\/code>) \u5e94\u8be5\u4f5c\u4e3a\u975e\u516c\u5171\u7684 API (\u4e0d\u7ba1\u662f\u51fd\u6570, \u65b9\u6cd5\u6216\u8005\u6570\u636e\u6210\u5458). \u8fd9\u5e94\u8be5\u4f5c\u4e3a\u5177\u4f53\u7684\u5b9e\u73b0, \u800c\u4e14\u53d8\u5316\u5b83\u4e5f\u65e0\u987b\u63d0\u9192.<\/p>\n\n\n\n<p>\u56e0\u4e3a\u6709\u4e00\u4e2a\u5408\u6cd5\u7684\u60c5\u51b5\u7528\u4e8e\u4f7f\u7528\u79c1\u6709\u7684\u6210\u5458 (\u540d\u4e49\u4e0a\u662f\u8bf4\u5728\u6d3e\u751f\u7c7b\u4e2d\u907f\u514d\u540d\u5b57\u7684\u51b2\u7a81), \u56e0\u6b64\u5c31\u6709\u8fd9\u6837\u7684\u4e00\u79cd\u673a\u5236\u79f0\u4e3a&nbsp;<em>name mangling<\/em>. \u4efb\u4f55\u5982&nbsp;<code>__spam<\/code>&nbsp;\u5f62\u5f0f\u7684\u6807\u8bc6\u7b26, (\u5728\u5f00\u5934\u81f3\u5c11\u6709\u4e24\u4e2a\u4e0b\u5212\u7ebf) \u5c06\u88ab\u66ff\u6362\u4e3a&nbsp;<code>_classname__spam<\/code>, \u6b64\u5904\u7684&nbsp;<code>classname<\/code>&nbsp;\u5c31\u662f\u5f53\u524d\u7684\u7c7b. \u8fd9\u6837\u7684\u5904\u7406\u65e0\u987b\u5173\u6ce8\u6807\u8bc6\u7b26\u7684\u53e5\u6cd5\u4e0a\u7684\u4f4d\u7f6e, \u5c3d\u7ba1\u5b83\u662f\u5728\u4e00\u4e2a\u7c7b\u7684\u5b9a\u4e49\u4e2d.<\/p>\n\n\n\n<p>\u6ce8\u610f, \u8fd9\u6837\u7684\u89c4\u5219\u53ea\u662f\u7528\u4e8e\u9632\u6b62\u51b2\u7a81; \u5b83\u4ecd\u7136\u53ef\u4ee5\u8bbf\u95ee\u6216\u4fee\u6539, \u5c3d\u7ba1\u8ba4\u4e3a\u8fd9\u662f\u4e00\u4e2a\u79c1\u6709\u53d8\u91cf. \u5728\u67d0\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b, \u5982\u6d4b\u8bd5\u7b49, \u662f\u6709\u7528\u7684.<\/p>\n\n\n\n<p>\u6ce8\u610f, \u4f20\u9012\u7ed9&nbsp;<code>exec()<\/code>&nbsp;\u6216&nbsp;<code>eval()<\/code>&nbsp;\u7684\u4ee3\u7801\u5e76\u4e0d\u4f1a\u8003\u8651\u88ab\u8c03\u7528\u7c7b\u7684\u7c7b\u540d\u662f\u5f53\u524d\u7684\u7c7b; \u8fd9\u4e2a\u548c&nbsp;<code>global<\/code>&nbsp;\u8bed\u53e5\u7684\u6548\u679c\u4e00\u6837, \u5b57\u8282\u7f16\u8bd1\u7684\u4ee3\u7801\u4e5f\u6709\u540c\u6837\u7684\u9650\u5236. \u800c\u5bf9\u4e8e&nbsp;<code>getattr()<\/code>,&nbsp;<code>setattr()<\/code>&nbsp;\u548c&nbsp;<code>delattr()<\/code>&nbsp;\u4e5f\u6709\u8fd9\u79cd\u9650\u5236, \u76f4\u63a5\u8bbf\u95ee&nbsp;<code>__dict__<\/code>&nbsp;\u4e5f\u662f\u6709\u8fd9\u6837\u7684\u95ee\u9898.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9.7. \u6742\u7269<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-odds\"><\/a><\/h2>\n\n\n\n<p>\u6709\u4e9b\u65f6\u5019, \u6709\u7c7b\u4f3c\u4e8e Pascal \u7684 \u201crecord\u201d \u6216 C \u7684 \u201cstruct\u201d \u8fd9\u6837\u7684\u6570\u636e\u7c7b\u578b\u975e\u5e38\u6709\u7528, \u7ed1\u5b9a\u4e00\u4e9b\u547d\u540d\u7684\u6570\u636e. \u4e00\u4e2a\u7a7a\u7684\u7c7b\u5b9a\u4e49\u5c31\u5c06\u5f88\u597d:<strong>class<\/strong><strong>Employee<\/strong>: <strong>pass<\/strong> john = Employee() <em># Create an empty employee record<\/em><em># Fill the fields of the record<\/em> john.name = &#8216;John Doe&#8217; john.dept = &#8216;computer lab&#8217; john.salary = 1000<\/p>\n\n\n\n<p>\u4e00\u6bb5 Python \u4ee3\u7801\u4e2d\u5982\u679c\u5e0c\u671b\u4e00\u4e2a\u62bd\u8c61\u7684\u6570\u636e\u7c7b\u578b, \u90a3\u4e48\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u7c7b\u7ed9\u90a3\u4e2a\u65b9\u6cd5, \u5c31\u597d\u50cf\u6709\u4e86\u90a3\u4e2a\u6570\u636e\u7c7b\u578b\u4e00\u6837.<\/p>\n\n\n\n<p>\u4f8b\u5982, \u5982\u679c\u4f60\u6709\u4e00\u4e2a\u51fd\u6570\u7528\u4e8e\u683c\u5f0f\u5316\u67d0\u4e9b\u4ece\u6587\u4ef6\u5bf9\u8c61\u4e2d\u8bfb\u53d6\u7684\u6570\u636e, \u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7c7b, \u7136\u540e\u6709\u65b9\u6cd5&nbsp;<code>read()<\/code>&nbsp;\u548c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/readline.html#module-readline\"><code>readline()<\/code><\/a>&nbsp;\u7528\u4e8e\u8bfb\u53d6\u6570\u636e, \u7136\u540e\u5c06\u8fd9\u4e2a\u7c7b\u4f5c\u4e3a\u4e00\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9\u90a3\u4e2a\u51fd\u6570.<\/p>\n\n\n\n<p>\u5b9e\u4f8b\u65b9\u6cd5\u5bf9\u8c61\u4e5f\u6709\u5c5e\u6027:&nbsp;<code>m.__self__<\/code>&nbsp;\u5c31\u662f\u4e00\u4e2a\u65b9\u6cd5&nbsp;<code>m()<\/code>&nbsp;\u7684\u5b9e\u4f8b\u5bf9\u8c61, \u800c&nbsp;<code>m.__func__<\/code>&nbsp;\u662f\u76f8\u5e94\u4e8e\u8be5\u65b9\u6cd5\u7684\u51fd\u6570\u5bf9\u8c61.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9.8. \u5f02\u5e38\u4e5f\u662f\u7c7b<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-exceptionclasses\"><\/a><\/h2>\n\n\n\n<p>\u7528\u6237\u5b9a\u4e49\u7684\u5f02\u5e38\u5176\u5b9e\u4e5f\u662f\u7c7b. \u4f7f\u7528\u8fd9\u4e2a\u673a\u5236, \u5c31\u53ef\u4ee5\u521b\u5efa\u53ef\u6269\u5c55\u7684\u5f02\u5e38\u7ee7\u627f\u4f53\u7cfb.<\/p>\n\n\n\n<p>\u6709\u4e24\u79cd\u5408\u6cd5\u7684\u5f62\u5f0f\u7528\u4e8e&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#raise\"><code>raise<\/code><\/a>&nbsp;\u8bed\u53e5:<strong>raise<\/strong> Class <strong>raise<\/strong> Instance<\/p>\n\n\n\n<p>\u5728\u7b2c\u4e00\u79cd\u5f62\u5f0f\u4e0b,&nbsp;<code>Class<\/code>&nbsp;\u5fc5\u987b\u662f&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#type\"><code>type<\/code><\/a>&nbsp;\u7684\u5b9e\u4f8b\u6216\u8005\u5176\u6d3e\u751f. \u7b2c\u4e00\u79cd\u5f62\u5f0f\u53ef\u4ee5\u7b80\u5316\u4e3a\u8fd9\u6837\u8fd9\u6837:<strong>raise<\/strong> Class()<\/p>\n\n\n\n<p>\u4e00\u4e2a\u5728&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#except\"><code>except<\/code><\/a>&nbsp;\u4e2d\u7684\u7c7b, \u53ef\u4ee5\u4e0e\u4e00\u4e2a\u5f02\u5e38\u76f8\u5bb9, \u5982\u679c\u8be5\u5f02\u5e38\u662f\u540c\u6837\u7684\u7c7b, \u6216\u662f\u5b83\u7684\u57fa\u7c7b (\u4f46\u662f\u5e76\u4e0d\u662f\u53e6\u4e00\u79cd \u2013 \u4e00\u4e2a except \u8bed\u53e5\u5217\u51fa\u7684\u6d3e\u751f\u7c7b\u4e0e\u5176\u57fa\u7c7b\u5e76\u4e0d\u76f8\u5bb9). \u5982\u4e0b\u9762\u7684\u4ee3\u7801, \u4ee5\u90a3\u79cd\u987a\u5e8f\u6253\u5370\u51fa B, C, D:<strong>class<\/strong><strong>B<\/strong>(Exception): <strong>pass<\/strong><strong>class<\/strong><strong>C<\/strong>(B): <strong>pass<\/strong><strong>class<\/strong><strong>D<\/strong>(C): <strong>pass<\/strong><strong>for<\/strong> c <strong>in<\/strong> [B, C, D]: <strong>try<\/strong>: <strong>raise<\/strong> c() <strong>except<\/strong> D: print(&#8220;D&#8221;) <strong>except<\/strong> C: print(&#8220;C&#8221;) <strong>except<\/strong> B: print(&#8220;B&#8221;)<\/p>\n\n\n\n<p>\u4f46\u662f\u6ce8\u610f, \u5982\u679c except \u8bed\u53e5\u662f\u53cd\u7740\u7684 (\u5148\u7528&nbsp;<code>except&nbsp;B<\/code>), \u90a3\u4e48\u6253\u5370\u7684\u7ed3\u679c\u5c06\u662f B, B, B \u2013 \u7b2c\u4e00\u4e2a\u603b\u662f\u5339\u914d.<\/p>\n\n\n\n<p>\u5f53\u56e0\u4e3a\u4e00\u4e2a\u672a\u5904\u7406\u7684\u5f02\u5e38\u53d1\u751f\u65f6, \u9519\u8bef\u4fe1\u606f\u5c06\u88ab\u6253\u5370, \u5f02\u5e38\u7684\u7c7b\u540d\u5c06\u88ab\u6253\u5370, \u7136\u540e\u662f\u4e00\u4e2a\u5192\u53f7\u548c\u7a7a\u683c, \u6700\u540e\u662f\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#str\"><code>str()<\/code><\/a>&nbsp;\u8f6c\u6362\u540e\u7684\u5b9e\u4f8b.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9.9. \u8fed\u4ee3\u5668<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-iterators\"><\/a><\/h2>\n\n\n\n<p>\u5230\u76ee\u524d\u4e3a\u6b62, \u4f60\u53ef\u80fd\u6ce8\u610f\u5230, \u5927\u591a\u6570\u7684\u5bb9\u5668\u5bf9\u8c61\u90fd\u53ef\u4ee5\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>&nbsp;\u6765\u8fed\u4ee3:<strong>for<\/strong> element <strong>in<\/strong> [1, 2, 3]: print(element) <strong>for<\/strong> element <strong>in<\/strong> (1, 2, 3): print(element) <strong>for<\/strong> key <strong>in<\/strong> {&#8216;one&#8217;:1, &#8216;two&#8217;:2}: print(key) <strong>for<\/strong> char <strong>in<\/strong> &#8220;123&#8221;: print(char) <strong>for<\/strong> line <strong>in<\/strong> open(&#8220;myfile.txt&#8221;): print(line)<\/p>\n\n\n\n<p>\u8fd9\u79cd\u5f62\u5f0f\u7b80\u6d01, \u660e\u4e86\u5e76\u4e14\u65b9\u4fbf. \u8fed\u4ee3\u5668\u7684\u4f7f\u7528\u904d\u5e03\u4e8e Python \u4e4b\u4e2d. \u5728\u8fd9\u4e2a\u5916\u8868\u4e4b\u4e0b,&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>&nbsp;\u8bed\u53e5\u5bf9\u5bb9\u5668\u5bf9\u8c61\u8c03\u7528\u4e86&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#iter\"><code>iter()<\/code><\/a>. \u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61, \u5b83\u5b9a\u4e49\u4e86&nbsp;<code>__next__()<\/code>&nbsp;\u65b9\u6cd5, \u7528\u4ee5\u5728\u6bcf\u6b21\u8bbf\u95ee\u65f6\u5f97\u5230\u4e00\u4e2a\u5143\u7d20. \u5f53\u6ca1\u6709\u4efb\u4f55\u5143\u7d20\u65f6,&nbsp;<code>__next__()<\/code>&nbsp;\u5c06\u4ea7\u751f&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/exceptions.html#StopIteration\"><code>StopIteration<\/code><\/a>&nbsp;\u5f02\u5e38, \u5b83\u544a\u8bc9&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>&nbsp;\u505c\u6b62\u8fed\u4ee3. \u4f60\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#next\"><code>next()<\/code><\/a>&nbsp;\u6765\u8c03\u7528&nbsp;<code>__next__()<\/code>&nbsp;\u65b9\u6cd5; \u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5b83\u5982\u4f55\u5de5\u4f5c:<strong>&gt;&gt;&gt; <\/strong>s = &#8216;abc&#8217; <strong>&gt;&gt;&gt; <\/strong>it = iter(s) <strong>&gt;&gt;&gt; <\/strong>it &lt;iterator object at 0x00A1DB50&gt; <strong>&gt;&gt;&gt; <\/strong>next(it) &#8216;a&#8217; <strong>&gt;&gt;&gt; <\/strong>next(it) &#8216;b&#8217; <strong>&gt;&gt;&gt; <\/strong>next(it) &#8216;c&#8217; <strong>&gt;&gt;&gt; <\/strong>next(it) Traceback (most recent call last): File &#8220;&lt;stdin&gt;&#8221;, line 1, in ? next(it) StopIteration<\/p>\n\n\n\n<p>\u5728\u770b\u5230\u8fed\u4ee3\u5668\u7684\u673a\u5236\u4e4b\u540e, \u5c31\u53ef\u4ee5\u5f88\u7b80\u5355\u7684\u5c06\u8fed\u4ee3\u884c\u4e3a\u589e\u52a0\u5230\u4f60\u7684\u7c7b\u4e2d. \u5b9a\u4e49\u4e00\u4e2a&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/datamodel.html#object.__iter__\"><code>__iter__()<\/code><\/a>&nbsp;\u65b9\u6cd5\u7528\u4ee5\u8fd4\u56de\u4e00\u4e2a\u5177\u6709&nbsp;<code>__next__()<\/code>&nbsp;\u7684\u5bf9\u8c61. \u5982\u679c\u8fd9\u4e2a\u7c7b\u5b9a\u4e49\u4e86&nbsp;<code>__next__()<\/code>&nbsp;, \u90a3\u4e48&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/datamodel.html#object.__iter__\"><code>__iter__()<\/code><\/a>&nbsp;\u4ec5\u9700\u8981\u8fd4\u56de&nbsp;<code>self<\/code>:<strong>class<\/strong><strong>Reverse<\/strong>: &#8220;Iterator for looping over a sequence backwards&#8221; <strong>def<\/strong> __init__(self, data): self.data = data self.index = len(data) <strong>def<\/strong> __iter__(self): <strong>return<\/strong> self <strong>def<\/strong> __next__(self): <strong>if<\/strong> self.index == 0: <strong>raise<\/strong> StopIteration self.index = self.index &#8211; 1 <strong>return<\/strong> self.data[self.index] &gt;&gt;&gt; rev = Reverse(&#8216;spam&#8217;) &gt;&gt;&gt; iter(rev) &lt;__main__.Reverse object at 0x00A1DB50&gt; &gt;&gt;&gt; <strong>for<\/strong> char <strong>in<\/strong> rev: &#8230; print(char) &#8230; m a p s<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9.10. \u53d1\u751f\u5668<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-generators\"><\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/glossary.html#term-generator\">Generator<\/a>&nbsp;(\u751f\u6210\u5668) \u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u8fed\u4ee3\u5668\u7b80\u5355\u800c\u4e14\u5f3a\u5927\u7684\u5de5\u5177. \u5b83\u4eec\u548c\u666e\u901a\u7684\u51fd\u6570\u5f88\u50cf, \u4f46\u662f\u5f53\u5b83\u4eec\u9700\u8981\u8fd4\u56de\u503c\u65f6, \u5219\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#yield\"><code>yield<\/code><\/a>&nbsp;\u8bed\u53e5. \u6bcf\u6b21&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#next\"><code>next()<\/code><\/a>&nbsp;\u88ab\u8c03\u7528\u65f6, \u751f\u6210\u5668\u4f1a\u4ece\u5b83\u4e0a\u6b21\u79bb\u5f00\u7684\u5730\u65b9\u7ee7\u7eed\u6267\u884c ( \u5b83\u4f1a\u8bb0\u4f4f\u6240\u6709\u7684\u6570\u636e\u503c\u548c\u6700\u540e\u4e00\u6b21\u6267\u884c\u7684\u8bed\u53e5). \u4e00\u4e2a\u4f8b\u5b50\u7528\u4ee5\u5c55\u793a\u5982\u4f55\u521b\u5efa\u751f\u6210\u5668:<strong>def<\/strong> reverse(data): <strong>for<\/strong> index <strong>in<\/strong> range(len(data)-1, -1, -1): <strong>yield<\/strong> data[index] &gt;&gt;&gt; <strong>for<\/strong> char <strong>in<\/strong> reverse(&#8216;golf&#8217;): &#8230; print(char) &#8230; f l o g<\/p>\n\n\n\n<p>\u4efb\u4f55\u53ef\u7528\u751f\u6210\u5668\u5b9e\u73b0\u7684\u4e1c\u897f\u90fd\u80fd\u7528\u57fa\u4e8e\u8fed\u4ee3\u5668\u7684\u7c7b\u5b9e\u73b0, \u8fd9\u4e2a\u5728\u524d\u9762\u6709\u6240\u63cf\u8ff0. \u8ba9\u751f\u6210\u5668\u770b\u8d77\u6765\u5f88\u7d27\u5bc6\u7684\u539f\u56e0\u662f\u5b83\u81ea\u52a8\u521b\u5efa\u4e86&nbsp;<code>__iter()<\/code>&nbsp;\u548c&nbsp;<code>__next__()<\/code>.<\/p>\n\n\n\n<p>\u53e6\u4e00\u4e2a\u5173\u952e\u7684\u7279\u6027\u5728\u4e8e, \u5c40\u90e8\u53d8\u91cf\u548c\u6267\u884c\u72b6\u6001\u90fd\u88ab\u81ea\u52a8\u4fdd\u5b58\u4e0b\u6765. \u8fd9\u5c31\u4f7f\u51fd\u6570\u66f4\u5bb9\u6613\u7f16\u5199\u5e76\u4e14\u66f4\u52a0\u6e05\u6670, \u76f8\u5bf9\u4e8e\u4f7f\u7528\u5b9e\u4f8b\u7684\u53d8\u91cf, \u5982&nbsp;<code>self.index<\/code>&nbsp;\u548c&nbsp;<code>self.data<\/code>.<\/p>\n\n\n\n<p>\u9664\u4e86\u81ea\u52a8\u521b\u5efa\u65b9\u6cd5\u548c\u4fdd\u5b58\u7a0b\u5e8f\u72b6\u6001, \u5f53\u751f\u6210\u5668\u7ec8\u6b62\u65f6, \u5b83\u4eec\u4f1a\u81ea\u52a8\u4ea7\u751f&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/exceptions.html#StopIteration\"><code>StopIteration<\/code><\/a>&nbsp;\u5f02\u5e38. \u5728\u8fd9\u4e9b\u7ed3\u5408\u8d77\u6765\u540e, \u8fd9\u5c31\u4f7f\u5f97\u80fd\u591f\u5f88\u7b80\u5355\u7684\u521b\u5efa\u8fed\u4ee3\u5668, \u9664\u4e86\u4ec5\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u51fd\u6570.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9.11. \u751f\u6210\u5668\u8868\u8fbe\u5f0f<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-genexps\"><\/a><\/h2>\n\n\n\n<p>\u6709\u4e9b\u7b80\u5355\u7684\u751f\u6210\u5668\u53ef\u4ee5\u7b80\u6d01\u7684\u5199\u51fa\u6765, \u800c\u4e14\u548c\u5217\u8868\u63a8\u5bfc\u5f88\u7c7b\u4f3c, \u4ec5\u4ec5\u662f\u5c06\u65b9\u62ec\u53f7\u6362\u6210\u4e86\u5706\u62ec\u53f7. \u8fd9\u4e9b\u8868\u8fbe\u5f0f\u8bbe\u8ba1\u7528\u4e8e\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\u6b63\u597d\u53ef\u4ee5\u7528\u751f\u6210\u5668\u7684\u60c5\u51b5. \u751f\u6210\u5668\u8868\u8fbe\u5f0f\u66f4\u52a0\u7d27\u5bc6, \u4f46\u662f\u529f\u80fd\u76f8\u5bf9\u6765\u8bf4\u4e5f\u5c11\u70b9, \u5e76\u4e14\u4e0e\u540c\u6837\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u8bf4\u66f4\u8282\u7ea6\u5185\u5b58.<\/p>\n\n\n\n<p>\u4f8b\u5b50:<strong>&gt;&gt;&gt; <\/strong>sum(i*i <strong>for<\/strong> i <strong>in<\/strong> range(10)) <em># sum of squares<\/em> 285 <strong>&gt;&gt;&gt; <\/strong>xvec = [10, 20, 30] <strong>&gt;&gt;&gt; <\/strong>yvec = [7, 5, 3] <strong>&gt;&gt;&gt; <\/strong>sum(x*y <strong>for<\/strong> x,y <strong>in<\/strong> zip(xvec, yvec)) <em># dot product<\/em> 260 <strong>&gt;&gt;&gt; <\/strong><strong>from<\/strong><strong>math<\/strong><strong>import<\/strong> pi, sin <strong>&gt;&gt;&gt; <\/strong>sine_table = {x: sin(x*pi\/180) <strong>for<\/strong> x <strong>in<\/strong> range(0, 91)} <strong>&gt;&gt;&gt; <\/strong>unique_words = set(word <strong>for<\/strong> line <strong>in<\/strong> page <strong>for<\/strong> word <strong>in<\/strong> line.split()) <strong>&gt;&gt;&gt; <\/strong>valedictorian = max((student.gpa, student.name) <strong>for<\/strong> student <strong>in<\/strong> graduates) <strong>&gt;&gt;&gt; <\/strong>data = &#8216;golf&#8217; <strong>&gt;&gt;&gt; <\/strong>list(data[i] <strong>for<\/strong> i <strong>in<\/strong> range(len(data)-1, -1, -1)) [&#8216;f&#8217;, &#8216;l&#8217;, &#8216;o&#8217;, &#8216;g&#8217;]<\/p>\n\n\n\n<p>Footnotes<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#id10\">[1]<\/a><\/td><td>\u9664\u4e86\u4e00\u79cd\u60c5\u51b5. Module \u5bf9\u8c61\u6709\u4e00\u4e2a\u79c1\u6709\u7684\u53ea\u8bfb\u5c5e\u6027, \u540d\u4e3a&nbsp;<code>__dict__<\/code>, \u5b83\u8fd4\u56de\u5b9e\u73b0\u8fd9\u4e2a\u6a21\u5757\u547d\u540d\u7a7a\u95f4\u7684\u5b57\u5178. \u663e\u7136, \u4f7f\u7528\u8fd9\u4f1a\u8fdd\u53cd\u547d\u540d\u7a7a\u95f4\u5b9e\u73b0\u7684\u62bd\u8c61, \u800c\u53ea\u5e94\u5f53\u9650\u4e8e\u5728\u5982 post-mortem debuggers \u7684\u4e8b\u60c5\u4e2d\u4f7f\u7528.<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u540c\u522b\u7684\u7f16\u7a0b\u8bed\u8a00\u76f8\u6bd4, Python \u7684\u7c7b\u673a\u5236\u4e2d\u589e\u52a0\u4e86\u5c11\u91cf\u65b0\u7684\u8bed\u6cd5\u548c\u8bed\u4e49. \u5b83\u662f C++ \u7684\u7c7b\u673a\u5236\u548c Modul [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-204","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":"","_links":{"self":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=204"}],"version-history":[{"count":3,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":254,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/204\/revisions\/254"}],"wp:attachment":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}