{"id":115663,"date":"2026-10-12T23:24:49","date_gmt":"2026-10-12T15:24:49","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/115663.html"},"modified":"2026-10-12T23:24:49","modified_gmt":"2026-10-12T15:24:49","slug":"%e7%ac%ac%e4%b8%83%e7%ab%a0-%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/115663.html","title":{"rendered":"\u7b2c\u4e03\u7ae0 \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1"},"content":{"rendered":"<h2>\u7b2c\u4e03\u7ae0 \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1<\/h2>\n<h3>\u76ee\u5f55<\/h3>\n<p>\u7b2c\u4e03\u7ae0  \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1<br \/>\n\u2502<br \/>\n\u251c\u2500\u2500 1. \u9762\u5411\u5bf9\u8c61\u6982\u8ff0     \u2192 \u9762\u5411\u8fc7\u7a0b vs \u9762\u5411\u5bf9\u8c61 \/ \u7c7b\u4e0e\u5bf9\u8c61 \/ \u4e09\u5927\u7279\u5f81<br \/>\n\u251c\u2500\u2500 2. \u7c7b\u7684\u5b9a\u4e49\u548c\u4f7f\u7528   \u2192 class \/ __init__ \/ self \/ \u5b9e\u4f8b\u5c5e\u6027\u4e0e\u7c7b\u5c5e\u6027<br \/>\n\u251c\u2500\u2500 3. \u5c5e\u6027 property    \u2192 \u79c1\u6709\u5c5e\u6027 \/ &#064;property \/ setter \/ \u53ea\u8bfb\u5c5e\u6027<br \/>\n\u251c\u2500\u2500 4. \u7ee7\u627f             \u2192 \u5355\u7ee7\u627f \/ \u65b9\u6cd5\u91cd\u5199 \/ super \/ \u591a\u7ee7\u627f\u4e0e MRO \/ \u591a\u6001<br \/>\n\u251c\u2500\u2500 5. \u5b9e\u6218             \u2192 \u94f6\u884c\u8d26\u6237 \/ \u8d2d\u7269\u8f66 \/ \u56fe\u5f62\u8ba1\u7b97\u5668 \/ \u5b66\u751f\u7ba1\u7406\u91cd\u6784<br \/>\n\u2514\u2500\u2500 6. \u5c0f\u7ed3             \u2192 \u77e5\u8bc6\u6e05\u5355 &#043; \u907f\u5751\u6e05\u5355 &#043; 5 \u9053\u9898 &#043; \u4e0b\u4e00\u7ae0\u9884\u544a<\/p>\n<p>\u7167\u65e7\u5148\u5efa\u4e2a\u7b2c\u4e03\u7ae0\u76ee\u5f55\u3002\u8fd9\u7ae0\u7684\u6982\u5ff5\u6bd4\u8bed\u6cd5\u91cd\u8981&#xff0c;\u6982\u5ff5\u6ca1\u641e\u61c2&#xff0c;\u8bed\u6cd5\u518d\u719f\u5199\u51fa\u6765\u7684\u7c7b\u4e5f\u662f\u56db\u4e0d\u50cf\u3002<\/p>\n<hr \/>\n<h3>1. \u9762\u5411\u5bf9\u8c61\u6982\u8ff0<\/h3>\n<h4>1.1 \u5148\u8bf4\u6e05\u695a&#xff0c;\u4ec0\u4e48\u662f\u9762\u5411\u8fc7\u7a0b<\/h4>\n<p>\u4f60\u73b0\u5728\u5199\u7684\u4ee3\u7801&#xff0c;\u57fa\u672c\u90fd\u662f\u9762\u5411\u8fc7\u7a0b\u7684\u3002\u60f3\u505a\u4e00\u4ef6\u4e8b&#xff0c;\u5c31\u628a\u5b83\u62c6\u6210\u4e00\u4e32\u6b65\u9aa4&#xff0c;\u4e00\u4e2a\u51fd\u6570\u4e00\u6b65&#xff0c;\u6309\u987a\u5e8f\u8d70\u5b8c\u3002<\/p>\n<p>\u4e3e\u4e2a\u5177\u4f53\u7684&#xff0c;\u628a\u4e00\u4e32\u5b66\u751f\u6570\u636e\u8bfb\u8fdb\u6765\u3001\u7b97\u5e73\u5747\u5206\u3001\u6253\u5370\u51fa\u6765\u3002<\/p>\n<p># \u5148\u9020\u4e2a\u6570\u636e\u6587\u4ef6&#xff0c;\u7701\u5f97\u4f60\u624b\u52a8\u5efa<br \/>\nwith open(&#034;scores.txt&#034;, &#034;w&#034;, encoding&#061;&#034;utf-8&#034;) as f:<br \/>\n    f.write(&#034;\u5f20\u4e09,88\\\\n\u674e\u56db,95\\\\n\u738b\u4e94,72\\\\n\u8d75\u516d,61\\\\n\u5b59\u4e03,90\\\\n&#034;)<\/p>\n<p>def read_scores(path):<br \/>\n    &#034;&#034;&#034;\u8bfb\u6587\u4ef6&#xff0c;\u8fd4\u56de [(\u59d3\u540d, \u5206\u6570), &#8230;]&#034;&#034;&#034;<br \/>\n    rows &#061; []<br \/>\n    with open(path, encoding&#061;&#034;utf-8&#034;) as f:<br \/>\n        for line in f:<br \/>\n            name, score &#061; line.strip().split(&#034;,&#034;)<br \/>\n            rows.append((name, int(score)))<br \/>\n    return rows<\/p>\n<p>def average(rows):<br \/>\n    &#034;&#034;&#034;\u7b97\u5e73\u5747\u5206&#034;&#034;&#034;<br \/>\n    return sum(score for _, score in rows) \/ len(rows)<\/p>\n<p>def top_n(rows, n&#061;3):<br \/>\n    &#034;&#034;&#034;\u53d6\u5206\u6570\u6700\u9ad8\u7684 n \u4e2a&#034;&#034;&#034;<br \/>\n    return sorted(rows, key&#061;lambda row: row[1], reverse&#061;True)[:n]<\/p>\n<p>def report(rows, n&#061;3):<br \/>\n    &#034;&#034;&#034;\u6c47\u603b\u8f93\u51fa&#034;&#034;&#034;<br \/>\n    print(&#034;\u5e73\u5747\u5206 %.1f&#034; % average(rows))<br \/>\n    for name, score in top_n(rows, n):<br \/>\n        print(name, score)<\/p>\n<p>rows &#061; read_scores(&#034;scores.txt&#034;)<br \/>\nreport(rows)<\/p>\n<p>\u8dd1\u51fa\u6765\u662f\u8fd9\u6837\u3002<\/p>\n<p>\u5e73\u5747\u5206 81.2<br \/>\n\u674e\u56db 95<br \/>\n\u5b59\u4e03 90<br \/>\n\u5f20\u4e09 88<\/p>\n<p>\u8fd9\u5957\u5199\u6cd5\u987a\u5f97\u5f88&#xff0c;\u903b\u8f91\u6e05\u695a&#xff0c;\u51fd\u6570\u5404\u7ba1\u4e00\u644a\u3002\u95ee\u9898\u51fa\u5728\u6570\u636e\u4e0a\u3002<\/p>\n<p>rows \u662f\u4e00\u4e2a\u88c5\u7740\u5143\u7ec4\u7684\u5217\u8868&#xff0c;\u6570\u636e\u7684\u542b\u4e49\u5168\u9760\u4e0b\u6807\u548c\u4f4d\u7f6e\u7ea6\u5b9a&#xff0c;row[0] \u662f\u59d3\u540d&#xff0c;row[1] \u662f\u5206\u6570\u3002\u8fd9\u4e2a\u7ea6\u5b9a\u5199\u5728\u4f60\u8111\u5b50\u91cc&#xff0c;\u6ca1\u5199\u5728\u4ee3\u7801\u91cc\u3002\u54ea\u5929\u8981\u52a0\u4e00\u5217\u73ed\u7ea7&#xff0c;row[1] \u5230\u5e95\u662f\u5206\u6570\u8fd8\u662f\u73ed\u7ea7&#xff0c;\u5f97\u628a\u6240\u6709\u7528\u5230\u4e0b\u6807\u7684\u5730\u65b9\u5168\u627e\u4e00\u904d\u3002<\/p>\n<p>\u66f4\u8981\u547d\u7684\u662f&#xff0c;\u6570\u636e\u81ea\u5df1\u4e0d\u4f1a\u4fdd\u62a4\u81ea\u5df1\u3002\u5206\u6570\u672c\u6765\u8be5\u662f 0 \u5230 100 \u7684\u6574\u6570&#xff0c;\u53ef\u8c01\u90fd\u80fd\u585e\u4e2a\u8d1f\u6570\u6216\u8005\u5b57\u7b26\u4e32\u8fdb\u53bb&#xff0c;average \u7167\u7b97\u4e0d\u8bef\u3002<\/p>\n<h4>1.2 \u9762\u5411\u5bf9\u8c61\u6362\u4e86\u4e2a\u601d\u8def<\/h4>\n<p>\u9762\u5411\u5bf9\u8c61\u628a\u7740\u773c\u70b9\u4ece&#034;\u6b65\u9aa4&#034;\u632a\u5230\u4e86&#034;\u4e1c\u897f&#034;\u4e0a\u3002\u5b66\u751f\u662f\u4e2a\u4e1c\u897f&#xff0c;\u5b83\u6709\u540d\u5b57\u3001\u6709\u5206\u6570\u3001\u6709\u52a0\u5206\u6570\u7684\u65b9\u6cd5\u3002\u522b\u518d\u62ff\u4e00\u5806\u51fd\u6570\u53bb\u642c\u8fd9\u4e9b\u6570\u636e&#xff0c;\u8ba9\u6570\u636e\u81ea\u5df1\u5e26\u7740\u80fd\u5bf9\u5b83\u505a\u7684\u52a8\u4f5c\u3002<\/p>\n<p>\u9762\u5411\u8fc7\u7a0b\u7684\u601d\u8def                     \u9762\u5411\u5bf9\u8c61\u7684\u601d\u8def<\/p>\n<p>\u6570\u636e \u2500\u2500\u2510                           \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n       \u2502  \u4ea4\u7ed9\u51fd\u6570                  \u2502   \u5b66\u751f\u5bf9\u8c61    \u2502<br \/>\n\u51fd\u6570 \u2500\u2500\u253c\u2500\u2500\u25ba \u5904\u7406 \u2500\u2500\u25ba \u7ed3\u679c           \u2502  \u59d3\u540d &#061; \u5f20\u4e09  \u2502<br \/>\n       \u2502                           \u2502  \u5206\u6570 &#061; 88    \u2502<br \/>\n\u6570\u636e \u2500\u2500\u2518                           \u2502  \u52a0\u5206()       \u2502<br \/>\n                                   \u2502  \u8bc4\u7ea7()       \u2502<br \/>\n  \u6570\u636e\u548c\u64cd\u4f5c\u662f\u5206\u5f00\u7684                \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n                                     \u6570\u636e\u548c\u64cd\u4f5c\u5728\u4e00\u8d77<\/p>\n<p>\u4e24\u79cd\u5199\u6cd5\u6ca1\u6709\u8c01\u5bf9\u8c01\u9519&#xff0c;\u5404\u6709\u9002\u5408\u7684\u573a\u666f&#xff0c;1.5 \u8282\u6211\u4f1a\u628a\u6211\u7684\u53d6\u820d\u6807\u51c6\u5199\u51fa\u6765\u3002<\/p>\n<h4>1.3 \u7c7b\u3001\u5bf9\u8c61\u3001\u5b9e\u4f8b&#xff0c;\u4e09\u4e2a\u8bcd\u4e00\u6b21\u8bf4\u6e05<\/h4>\n<p>\u8fd9\u4e09\u4e2a\u8bcd\u5728\u4e66\u91cc\u53cd\u590d\u51fa\u73b0&#xff0c;\u6df7\u7740\u7528\u7684\u4eba\u4e0d\u5c11\u3002<\/p>\n<table>\n<tr>\u6982\u5ff5\u662f\u4ec0\u4e48\u7c7b\u6bd4<\/tr>\n<tbody>\n<tr>\n<td>\u7c7b&#xff08;Class&#xff09;<\/td>\n<td>\u4e00\u7c7b\u4e1c\u897f\u7684\u6a21\u677f\u3001\u56fe\u7eb8<\/td>\n<td>\u6c7d\u8f66\u7684\u8bbe\u8ba1\u56fe\u7eb8<\/td>\n<\/tr>\n<tr>\n<td>\u5bf9\u8c61&#xff08;Object&#xff09;<\/td>\n<td>\u6309\u7167\u56fe\u7eb8\u505a\u51fa\u6765\u7684\u5b9e\u4f53<\/td>\n<td>\u4f60\u5bb6\u90a3\u8f86\u5177\u4f53\u7684\u8f66<\/td>\n<\/tr>\n<tr>\n<td>\u5b9e\u4f8b&#xff08;Instance&#xff09;<\/td>\n<td>\u5c31\u662f\u5bf9\u8c61&#xff0c;\u6362\u4e2a\u8bf4\u6cd5\u5f3a\u8c03\u5b83\u662f\u67d0\u7c7b\u9020\u51fa\u6765\u7684<\/td>\n<td>\u8fd9\u8f86\u8f66\u662f&#034;\u6c7d\u8f66&#034;\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e25\u683c\u8bb2\u5bf9\u8c61\u548c\u5b9e\u4f8b\u662f\u4e00\u56de\u4e8b&#xff0c;\u65e5\u5e38\u8bf4\u8bdd\u91cc&#034;\u5bf9\u8c61&#034;\u7528\u5f97\u591a&#xff0c;\u8bb2\u7ee7\u627f\u5173\u7cfb\u7684\u65f6\u5019\u7231\u8bf4&#034;\u5b9e\u4f8b&#034;\u3002<\/p>\n<p>   \u7c7b Student&#xff08;\u56fe\u7eb8&#xff0c;\u53ea\u6709\u4e00\u4efd&#xff09;<br \/>\n        \u2502<br \/>\n        \u2502  \u7528\u7c7b\u540d\u52a0\u62ec\u53f7&#xff0c;\u9020\u51fa\u4e09\u4e2a\u5404\u81ea\u72ec\u7acb\u7684\u5bf9\u8c61<br \/>\n        \u251c\u2500\u2500\u25ba stu1  \u59d3\u540d&#061;\u5f20\u4e09  \u5206\u6570&#061;88<br \/>\n        \u251c\u2500\u2500\u25ba stu2  \u59d3\u540d&#061;\u674e\u56db  \u5206\u6570&#061;95<br \/>\n        \u2514\u2500\u2500\u25ba stu3  \u59d3\u540d&#061;\u738b\u4e94  \u5206\u6570&#061;72<\/p>\n<p>   \u4e09\u4e2a\u5bf9\u8c61\u7ed3\u6784\u4e00\u6837&#xff0c;\u503c\u5404\u81ea\u72ec\u7acb\u3002\u6539 stu1 \u7684\u5206\u6570&#xff0c;stu2 \u4e0d\u53d7\u5f71\u54cd<\/p>\n<h4>1.4 \u4e09\u5927\u7279\u5f81&#xff0c;\u522b\u80cc\u5b9a\u4e49&#xff0c;\u770b\u5b83\u89e3\u51b3\u4e86\u4ec0\u4e48\u95ee\u9898<\/h4>\n<p>\u4e66\u4e0a\u90fd\u4f1a\u5199\u5c01\u88c5\u3001\u7ee7\u627f\u3001\u591a\u6001\u3002\u6211\u6309&#034;\u5b83\u5230\u5e95\u89e3\u51b3\u4e86\u4ec0\u4e48\u95ee\u9898&#034;\u6765\u8bb2\u3002<\/p>\n<p>\u5c01\u88c5\u3002\u628a\u6570\u636e\u548c\u5bf9\u6570\u636e\u7684\u64cd\u4f5c\u5305\u5728\u4e00\u8d77&#xff0c;\u5916\u9762\u53ea\u7559\u51e0\u4e2a\u53e3\u5b50\u3002\u597d\u5904\u662f\u6570\u636e\u4e0d\u4f1a\u88ab\u968f\u624b\u6539\u574f\u3002\u94f6\u884c\u8d26\u6237\u7684\u4f59\u989d\u4f60\u4e0d\u5e0c\u671b\u8c01\u90fd\u80fd account.balance &#061; 1000000 \u5427&#xff0c;\u5c01\u88c5\u4e4b\u540e\u4f59\u989d\u53ea\u80fd\u901a\u8fc7 deposit\u3001withdraw \u8fd9\u4e9b\u65b9\u6cd5\u6765\u52a8&#xff0c;\u65b9\u6cd5\u91cc\u80fd\u52a0\u6821\u9a8c\u3002<\/p>\n<p>\u7ee7\u627f\u3002\u6709\u4e86\u4e00\u6279\u76f8\u4f3c\u7684\u4e1c\u897f&#xff0c;\u628a\u5171\u540c\u7684\u90e8\u5206\u62bd\u5230\u7236\u7c7b\u91cc&#xff0c;\u5404\u81ea\u53ea\u5199\u4e0d\u4e00\u6837\u7684\u90e8\u5206\u3002\u732b\u548c\u72d7\u90fd\u4f1a\u53eb\u3001\u90fd\u6709\u540d\u5b57&#xff0c;\u90a3\u5c31\u628a\u8fd9\u4e9b\u653e\u8fdb Animal&#xff0c;Dog \u548c Cat \u53ea\u5199\u81ea\u5df1\u90a3\u58f0\u53eb\u3002\u7701\u4ee3\u7801\u662f\u4e00\u65b9\u9762&#xff0c;\u66f4\u91cd\u8981\u7684\u662f&#034;\u732b\u662f\u4e00\u79cd\u52a8\u7269&#034;\u8fd9\u5c42\u5173\u7cfb\u88ab\u5199\u8fdb\u4e86\u4ee3\u7801\u7ed3\u6784\u91cc\u3002<\/p>\n<p>\u591a\u6001\u3002\u540c\u6837\u4e00\u4e2a\u52a8\u4f5c&#xff0c;\u4f5c\u7528\u5728\u4e0d\u540c\u5bf9\u8c61\u4e0a\u4f1a\u6709\u4e0d\u540c\u7684\u8868\u73b0&#xff0c;\u800c\u8c03\u7528\u65b9\u4e0d\u7528\u5173\u5fc3\u5177\u4f53\u662f\u54ea\u4e2a\u5bf9\u8c61\u3002\u4f60\u558a\u4e00\u58f0 speak()&#xff0c;\u732b\u55b5\u55b5\u3001\u72d7\u6c6a\u6c6a&#xff0c;\u5199\u5faa\u73af\u7684\u90a3\u884c\u4ee3\u7801\u6839\u672c\u4e0d\u9700\u8981 if isinstance\u3002<\/p>\n<p>\u8fd9\u4e09\u4e2a\u8bcd\u540e\u9762\u51e0\u8282\u4f1a\u4e00\u4e2a\u4e2a\u5c55\u5f00&#xff0c;\u73b0\u5728\u7559\u4e2a\u5370\u8c61\u5c31\u884c\u3002<\/p>\n<h4>1.5 \u4ec0\u4e48\u65f6\u5019\u4e0d\u8be5\u7528\u7c7b<\/h4>\n<p>\u8fd9\u6bb5\u662f\u6211\u81ea\u5df1\u7684\u770b\u6cd5&#xff0c;\u6559\u79d1\u4e66\u4e0a\u4e00\u822c\u4e0d\u5199\u3002<\/p>\n<p>Python \u4e0d\u662f Java&#xff0c;\u5b83\u6ca1\u6709&#034;\u6240\u6709\u4ee3\u7801\u90fd\u5fc5\u987b\u5199\u5728\u7c7b\u91cc&#034;\u7684\u89c4\u77e9\u3002\u6211\u5728\u5b9e\u9645\u9879\u76ee\u91cc\u7684\u53d6\u820d\u662f\u8fd9\u51e0\u6761\u3002<\/p>\n<table>\n<tr>\u573a\u666f\u6211\u7684\u9009\u62e9<\/tr>\n<tbody>\n<tr>\n<td>\u4e00\u6b21\u6027\u811a\u672c&#xff0c;\u51e0\u5341\u884c\u641e\u5b9a<\/td>\n<td>\u7eaf\u51fd\u6570&#xff0c;\u522b\u641e\u7c7b<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u6709\u56fa\u5b9a\u5b57\u6bb5&#xff0c;\u4e14\u8981\u53cd\u590d\u4f20\u9012<\/td>\n<td>\u7c7b<\/td>\n<\/tr>\n<tr>\n<td>\u5b57\u6bb5\u9700\u8981\u6821\u9a8c\u3001\u8ba1\u7b97\u6216\u8005\u53ea\u8bfb<\/td>\n<td>\u7c7b\u52a0 property<\/td>\n<\/tr>\n<tr>\n<td>\u4e00\u7ec4\u4e1c\u897f\u884c\u4e3a\u76f8\u4f3c&#xff0c;\u53ea\u6709\u7ec6\u8282\u4e0d\u540c<\/td>\n<td>\u7c7b\u52a0\u7ee7\u627f<\/td>\n<\/tr>\n<tr>\n<td>\u53ea\u662f\u60f3\u5b58\u51e0\u4e2a\u503c\u4e00\u8d77\u4f20<\/td>\n<td>namedtuple \u6216\u8005\u5b57\u5178\u5c31\u591f<\/td>\n<\/tr>\n<tr>\n<td>\u53ea\u6709\u4e00\u4e24\u4e2a\u65b9\u6cd5&#xff0c;\u4e14\u6ca1\u6709\u5171\u4eab\u72b6\u6001<\/td>\n<td>\u51fd\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u89c1\u8fc7\u6700\u5178\u578b\u7684\u8fc7\u5ea6\u8bbe\u8ba1\u662f&#xff0c;\u4e3a\u4e86\u5b58\u4e00\u4e2a\u914d\u7f6e\u9879\u5199\u4e86\u4e2a Config \u7c7b&#xff0c;\u91cc\u9762\u4e00\u4e2a __init__ \u52a0\u4e00\u4e2a get&#xff0c;\u660e\u660e\u4e00\u4e2a\u5b57\u5178\u80fd\u89e3\u51b3\u7684\u4e8b\u3002\u7c7b\u662f\u62ff\u6765\u7ba1\u590d\u6742\u5ea6\u7684&#xff0c;\u4e0d\u662f\u62ff\u6765\u663e\u6446\u7684\u3002<\/p>\n<p>\u6211\u81ea\u5df1\u8e29\u8fc7\u7684\u53e6\u4e00\u4e2a\u5751\u662f&#xff0c;\u628a\u4e00\u5806\u672c\u6765\u4e92\u76f8\u72ec\u7acb\u7684\u529f\u80fd\u786c\u585e\u8fdb\u4e00\u4e2a\u5927\u7c7b&#xff0c;\u6700\u540e\u8fd9\u4e2a\u7c7b\u4e24\u5343\u884c&#xff0c;\u6539\u4e00\u5904\u8981\u8bfb\u534a\u5929\u3002\u7c7b\u592a\u5927\u662f\u574f\u5473\u9053&#xff0c;\u8be5\u62c6\u5c31\u62c6\u3002<\/p>\n<hr \/>\n<h3>2. \u7c7b\u7684\u5b9a\u4e49\u548c\u4f7f\u7528<\/h3>\n<h4>2.1 \u5199\u51fa\u7b2c\u4e00\u4e2a\u7c7b<\/h4>\n<p>\u5148\u628a 1.1 \u90a3\u4e2a\u5b66\u751f\u6570\u636e\u6539\u6210\u7c7b\u3002<\/p>\n<p>&gt;&gt;&gt; class Student:<br \/>\n&#8230;     def __init__(self, name, score):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;         self.score &#061; score<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; stu &#061; Student(&#034;\u5f20\u4e09&#034;, 88)<br \/>\n&gt;&gt;&gt; stu.name<br \/>\n&#039;\u5f20\u4e09&#039;<br \/>\n&gt;&gt;&gt; stu.score<br \/>\n88<\/p>\n<p>\u4e09\u884c\u4ee3\u7801\u5c31\u5b8c\u4e8b\u4e86\u3002\u5bf9\u6bd4\u4e00\u4e0b\u7528\u5b83\u548c\u7528\u5143\u7ec4\u7684\u533a\u522b\u3002<\/p>\n<p>&gt;&gt;&gt; stu &#061; Student(&#034;\u5f20\u4e09&#034;, 88)<br \/>\n&gt;&gt;&gt; print(stu.name, &#034;\u8003\u4e86&#034;, stu.score)<br \/>\n\u5f20\u4e09 \u8003\u4e86 88<br \/>\n&gt;&gt;&gt; row &#061; (&#034;\u5f20\u4e09&#034;, 88)<br \/>\n&gt;&gt;&gt; print(row[0], &#034;\u8003\u4e86&#034;, row[1])<br \/>\n\u5f20\u4e09 \u8003\u4e86 88<\/p>\n<p>\u5355\u770b\u6253\u5370\u5dee\u4e0d\u591a&#xff0c;\u53ef stu.name \u548c row[0] \u7684\u53ef\u8bfb\u6027\u5dee\u4e86\u4e00\u4e2a\u91cf\u7ea7\u3002\u534a\u5e74\u540e\u56de\u6765\u6539\u4ee3\u7801&#xff0c;\u524d\u8005\u4e00\u773c\u770b\u61c2&#xff0c;\u540e\u8005\u5f97\u7ffb\u56de\u53bb\u67e5\u7ea6\u5b9a\u3002<\/p>\n<h4>2.2 class \u7684\u8bed\u6cd5\u62c6\u5f00\u770b<\/h4>\n<p>class \u7c7b\u540d:                          \u2190 \u5927\u9a7c\u5cf0&#xff0c;\u9996\u5b57\u6bcd\u5927\u5199<br \/>\n    \u2502<br \/>\n    \u251c\u2500\u2500 def __init__(self, \u53c2\u6570):     \u2190 \u6784\u9020\u65b9\u6cd5&#xff0c;\u521b\u5efa\u5bf9\u8c61\u65f6\u81ea\u52a8\u8c03\u7528<br \/>\n    \u2502       self.\u5c5e\u6027 &#061; \u503c            \u2190 \u7ed9\u8fd9\u4e2a\u5bf9\u8c61\u7ed1\u4e0a\u6570\u636e<br \/>\n    \u2502<br \/>\n    \u251c\u2500\u2500 def \u65b9\u6cd5\u540d(self, \u53c2\u6570):       \u2190 \u5b9e\u4f8b\u65b9\u6cd5&#xff0c;\u7b2c\u4e00\u4e2a\u53c2\u6570\u5fc5\u987b\u662f self<br \/>\n    \u2502       \u65b9\u6cd5\u4f53<br \/>\n    \u2502<br \/>\n    \u2514\u2500\u2500 \u7c7b\u5c5e\u6027 &#061; \u503c                   \u2190 \u76f4\u63a5\u5199\u5728\u7c7b\u91cc\u3001\u4e0d\u5e26 self \u7684\u53d8\u91cf<\/p>\n<p>\u4e09\u6761\u547d\u540d\u89c4\u77e9\u3002<\/p>\n<table>\n<tr>\u4e1c\u897f\u547d\u540d\u98ce\u683c\u4f8b\u5b50<\/tr>\n<tbody>\n<tr>\n<td>\u7c7b\u540d<\/td>\n<td>\u5927\u9a7c\u5cf0<\/td>\n<td>Student\u3001BankAccount<\/td>\n<\/tr>\n<tr>\n<td>\u65b9\u6cd5\u540d<\/td>\n<td>\u5c0f\u5199\u52a0\u4e0b\u5212\u7ebf<\/td>\n<td>get_score\u3001save_money<\/td>\n<\/tr>\n<tr>\n<td>\u79c1\u6709\u5c5e\u6027<\/td>\n<td>\u4e0b\u5212\u7ebf\u5f00\u5934<\/td>\n<td>_balance\u3001__money<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Python \u4e0d\u5f3a\u5236&#xff0c;\u5199\u9519\u4e86\u80fd\u8dd1&#xff0c;\u4f46\u63a5\u624b\u4f60\u4ee3\u7801\u7684\u4eba\u4f1a\u96be\u53d7\u3002<\/p>\n<h4>2.3 self \u5230\u5e95\u662f\u4ec0\u4e48&#xff0c;\u8fd9\u8282\u503c\u5f97\u6162\u6162\u770b<\/h4>\n<p>\u6211\u5e26\u8fc7\u51e0\u4e2a\u521a\u5b66 Python \u7684\u4eba&#xff0c;\u5361\u4f4f\u7684\u7b2c\u4e00\u5904\u57fa\u672c\u90fd\u662f self\u3002\u5b83\u770b\u7740\u50cf\u51ed\u7a7a\u5192\u51fa\u6765\u7684\u89c4\u77e9&#xff0c;\u5176\u5b9e\u5c31\u4e00\u53e5\u8bdd&#xff0c;self \u662f\u8fd9\u4e2a\u5bf9\u8c61\u81ea\u5df1\u3002<\/p>\n<p>\u505a\u4e2a\u5b9e\u9a8c\u5c31\u660e\u767d\u4e86\u3002\u8ba9\u65b9\u6cd5\u6253\u5370\u4e00\u4e0b\u5b83\u6536\u5230\u7684 self \u662f\u8c01\u3002<\/p>\n<p>&gt;&gt;&gt; class Demo:<br \/>\n&#8230;     def compare(self, other):<br \/>\n&#8230;         print(&#034;self \u548c\u4f20\u8fdb\u6765\u7684\u5bf9\u8c61\u662f\u540c\u4e00\u4e2a\u5417 -&gt;&#034;, self is other)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; d &#061; Demo()<br \/>\n&gt;&gt;&gt; d.compare(d)<br \/>\nself \u548c\u4f20\u8fdb\u6765\u7684\u5bf9\u8c61\u662f\u540c\u4e00\u4e2a\u5417 -&gt; True<br \/>\n&gt;&gt;&gt; other &#061; Demo()<br \/>\n&gt;&gt;&gt; d.compare(other)<br \/>\nself \u548c\u4f20\u8fdb\u6765\u7684\u5bf9\u8c61\u662f\u540c\u4e00\u4e2a\u5417 -&gt; False<\/p>\n<p>d.compare(d) \u91cc\u7684 self \u5c31\u662f d \u81ea\u5df1\u3002<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u5b9e\u9a8c\u66f4\u80fd\u8bf4\u660e\u95ee\u9898\u3002self \u662f Python \u81ea\u52a8\u5e2e\u4f60\u4f20\u8fdb\u53bb\u7684&#xff0c;\u4f60\u5199 d.hello()&#xff0c;Python \u5b9e\u9645\u6267\u884c\u7684\u662f Demo.hello(d)\u3002<\/p>\n<p>&gt;&gt;&gt; class Demo:<br \/>\n&#8230;     def hello(self):<br \/>\n&#8230;         print(&#034;\u4f60\u597d&#034;)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; d &#061; Demo()<br \/>\n&gt;&gt;&gt; d.hello()<br \/>\n\u4f60\u597d<br \/>\n&gt;&gt;&gt; Demo.hello(d)<br \/>\n\u4f60\u597d<\/p>\n<p>\u4e24\u79cd\u5199\u6cd5\u6548\u679c\u4e00\u6837\u3002\u7b2c\u4e8c\u79cd\u770b\u7740\u522b\u626d&#xff0c;\u4f46\u5b83\u628a&#034;\u65b9\u6cd5\u5176\u5b9e\u5c31\u662f\u6302\u5728\u7c7b\u4e0a\u7684\u666e\u901a\u51fd\u6570&#xff0c;\u53ea\u4e0d\u8fc7\u7b2c\u4e00\u4e2a\u53c2\u6570\u6536\u7684\u662f\u5bf9\u8c61&#034;\u8fd9\u4ef6\u4e8b\u644a\u5f00\u4e86\u3002<\/p>\n<p>\u65e2\u7136\u662f\u81ea\u52a8\u4f20\u7684&#xff0c;\u90a3\u5b9a\u4e49\u7684\u65f6\u5019\u6f0f\u5199 self \u4f1a\u51fa\u4e8b\u3002<\/p>\n<p>&gt;&gt;&gt; class Bad:<br \/>\n&#8230;     def hello():<br \/>\n&#8230;         print(&#034;\u4f60\u597d&#034;)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; b &#061; Bad()<br \/>\n&gt;&gt;&gt; b.hello()<br \/>\nTypeError: Bad.hello() takes 0 positional arguments but 1 was given<\/p>\n<p>\u62a5\u9519\u8bf4\u7684\u662f&#034;\u8fd9\u4e2a\u51fd\u6570\u8981 0 \u4e2a\u53c2\u6570&#xff0c;\u5374\u7ed9\u4e86 1 \u4e2a&#034;\u3002\u591a\u51fa\u6765\u7684\u90a3\u4e2a\u5c31\u662f Python \u786c\u585e\u8fdb\u53bb\u7684 self\u3002\u8fd9\u4e2a\u62a5\u9519\u6211\u89c1\u8fc7\u4e0d\u4e0b\u4e8c\u5341\u6b21&#xff0c;\u51e0\u4e4e\u5168\u662f\u6f0f\u5199 self \u9020\u6210\u7684\u3002<\/p>\n<p>\u8fd8\u6709\u4e2a\u51b7\u77e5\u8bc6&#xff0c;self \u4e0d\u662f\u5173\u952e\u5b57&#xff0c;\u6539\u6210\u522b\u7684\u540d\u5b57\u7167\u6837\u8dd1\u3002<\/p>\n<p>&gt;&gt;&gt; class Weird:<br \/>\n&#8230;     def __init__(this, name):<br \/>\n&#8230;         this.name &#061; name<br \/>\n&#8230;     def get(this):<br \/>\n&#8230;         return this.name<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; w &#061; Weird(&#034;\u6d4b\u8bd5&#034;)<br \/>\n&gt;&gt;&gt; w.get()<br \/>\n&#039;\u6d4b\u8bd5&#039;<\/p>\n<p>\u80fd\u8dd1&#xff0c;\u4f46\u5343\u4e07\u522b\u8fd9\u4e48\u5199\u3002\u5168\u4e16\u754c\u7684 Python \u4ee3\u7801\u90fd\u7528 self&#xff0c;\u4f60\u5199 this \u53ea\u4f1a\u8ba9\u522b\u4eba\u8bfb\u7684\u65f6\u5019\u5fc3\u91cc\u4e00\u54af\u5654\u3002<\/p>\n<p>d.hello()           Python \u80cc\u540e\u505a\u7684\u4e8b<br \/>\n    \u2502<br \/>\n    \u25bc<br \/>\nDemo.hello(d)  \u2500\u2500\u25ba  hello(self&#061;d)  \u2500\u2500\u25ba  print(&#034;\u4f60\u597d&#034;)<br \/>\n                          \u2502<br \/>\n                          \u2514\u2500 self \u5c31\u662f\u8c03\u7528\u5b83\u7684\u90a3\u4e2a\u5bf9\u8c61<\/p>\n<h4>2.4 __init__ \u4e0d\u662f\u521b\u5efa\u5bf9\u8c61&#xff0c;\u662f\u521d\u59cb\u5316<\/h4>\n<p>\u5f88\u591a\u4eba\u4ee5\u4e3a __init__ \u662f\u5728\u9020\u5bf9\u8c61\u3002\u4e0d\u5b8c\u5168\u5bf9&#xff0c;\u9020\u5bf9\u8c61\u7684\u662f\u53e6\u4e00\u4e2a\u65b9\u6cd5 __new__&#xff0c;__init__ \u53ea\u8d1f\u8d23\u5f80\u9020\u597d\u7684\u7a7a\u58f3\u91cc\u586b\u6570\u636e\u3002<\/p>\n<p>&gt;&gt;&gt; class Trace:<br \/>\n&#8230;     def __new__(cls, *args):<br \/>\n&#8230;         print(&#034;__new__ \u9020\u5bf9\u8c61&#xff0c;\u6536\u5230&#034;, args)<br \/>\n&#8230;         return super().__new__(cls)<br \/>\n&#8230;     def __init__(self, value):<br \/>\n&#8230;         print(&#034;__init__ \u521d\u59cb\u5316&#xff0c;value &#061;&#034;, value)<br \/>\n&#8230;         self.value &#061; value<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; t &#061; Trace(5)<br \/>\n__new__ \u9020\u5bf9\u8c61&#xff0c;\u6536\u5230 (5,)<br \/>\n__init__ \u521d\u59cb\u5316&#xff0c;value &#061; 5<\/p>\n<p>\u987a\u5e8f\u4e00\u6e05\u4e8c\u695a\u3002\u5e73\u65f6\u4f60\u6839\u672c\u7528\u4e0d\u7740\u5199 __new__&#xff0c;\u77e5\u9053\u6709\u8fd9\u4e48\u4e2a\u73af\u8282\u5c31\u884c&#xff0c;\u7b49\u54ea\u5929\u8981\u5199\u5355\u4f8b\u6a21\u5f0f\u6216\u8005\u4e0d\u53ef\u53d8\u7c7b\u578b\u7684\u65f6\u5019\u81ea\u7136\u4f1a\u518d\u9047\u4e0a\u3002<\/p>\n<p>\u5b8c\u6574\u7684\u521b\u5efa\u5bf9\u8c61\u6d41\u7a0b\u662f\u8fd9\u6837\u3002<\/p>\n<p>stu &#061; Student(&#034;\u5f20\u4e09&#034;, 88)<br \/>\n        \u2502<br \/>\n        \u25bc<br \/>\n\u2460 __new__ \u9020\u4e00\u4e2a\u7a7a\u5bf9\u8c61\u51fa\u6765<br \/>\n        \u2502<br \/>\n        \u25bc<br \/>\n\u2461 \u7a7a\u5bf9\u8c61\u5f53 self \u4f20\u8fdb __init__&#xff0c;\u7ed1\u4e0a name \u548c score<br \/>\n        \u2502<br \/>\n        \u25bc<br \/>\n\u2462 \u8fd9\u4e2a\u5bf9\u8c61\u8fd4\u56de\u51fa\u6765&#xff0c;\u8d4b\u503c\u5230 stu<\/p>\n<h4>2.5 \u5c5e\u6027\u7684\u589e\u5220\u6539\u67e5<\/h4>\n<p>\u5bf9\u8c61\u8eab\u4e0a\u7684\u5c5e\u6027\u5b58\u5728\u4e00\u4e2a\u5c0f\u5b57\u5178\u91cc&#xff0c;\u80fd\u52a0\u80fd\u5220\u80fd\u67e5\u3002<\/p>\n<p>&gt;&gt;&gt; stu &#061; Student(&#034;\u5f20\u4e09&#034;, 88)<br \/>\n&gt;&gt;&gt; stu.age &#061; 18<br \/>\n&gt;&gt;&gt; stu.__dict__<br \/>\n{&#039;name&#039;: &#039;\u5f20\u4e09&#039;, &#039;score&#039;: 88, &#039;age&#039;: 18}<br \/>\n&gt;&gt;&gt; getattr(stu, &#034;name&#034;)<br \/>\n&#039;\u5f20\u4e09&#039;<br \/>\n&gt;&gt;&gt; getattr(stu, &#034;gender&#034;, &#034;\u672a\u77e5&#034;)<br \/>\n&#039;\u672a\u77e5&#039;<br \/>\n&gt;&gt;&gt; hasattr(stu, &#034;gender&#034;)<br \/>\nFalse<br \/>\n&gt;&gt;&gt; del stu.age<br \/>\n&gt;&gt;&gt; stu.__dict__<br \/>\n{&#039;name&#039;: &#039;\u5f20\u4e09&#039;, &#039;score&#039;: 88}<\/p>\n<p>__dict__ \u662f\u5bf9\u8c61\u5b58\u5c5e\u6027\u7684\u90a3\u4e2a\u5b57\u5178&#xff0c;\u5e73\u65f6\u7528\u4e0d\u4e0a&#xff0c;\u8c03\u8bd5\u7684\u65f6\u5019\u5f80\u91cc\u9762\u770b\u4e00\u773c\u6bd4\u4ec0\u4e48\u90fd\u5feb\u3002<\/p>\n<p>getattr(\u5bf9\u8c61, \u540d\u5b57, \u9ed8\u8ba4\u503c) \u8fd9\u4e2a\u5f62\u5f0f\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u65f6\u5019\u5f88\u987a\u624b&#xff0c;\u5b57\u6bb5\u540d\u4e0d\u786e\u5b9a\u4e5f\u4e0d\u4f1a\u629b\u5f02\u5e38\u3002<\/p>\n<p>\u4e0d\u8fc7\u60f3\u52a0\u5c31\u52a0\u8fd9\u4e2a\u7279\u6027\u633a\u5371\u9669\u7684\u3002\u62fc\u5199\u9519\u4e86\u4e5f\u4e0d\u62a5\u9519&#xff0c;\u51ed\u7a7a\u591a\u51fa\u4e00\u4e2a\u5c5e\u6027&#xff0c;\u6392\u67e5\u8d77\u6765\u8981\u547d\u3002\u60f3\u9650\u5236\u4f4f&#xff0c;\u7528 __slots__\u3002<\/p>\n<p>&gt;&gt;&gt; class Point:<br \/>\n&#8230;     __slots__ &#061; (&#034;x&#034;, &#034;y&#034;)<br \/>\n&#8230;     def __init__(self, x, y):<br \/>\n&#8230;         self.x &#061; x<br \/>\n&#8230;         self.y &#061; y<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; p &#061; Point(1, 2)<br \/>\n&gt;&gt;&gt; p.x<br \/>\n1<br \/>\n&gt;&gt;&gt; p.z &#061; 3<br \/>\nAttributeError: &#039;Point&#039; object has no attribute &#039;z&#039;<\/p>\n<p>__slots__ \u4e00\u5199&#xff0c;\u8fd9\u4e2a\u7c7b\u5c31\u53ea\u80fd\u6709 x \u548c y \u4e24\u4e2a\u5c5e\u6027&#xff0c;\u591a\u4e00\u4e2a\u5c31\u62a5\u9519\u3002\u987a\u5e26\u7684\u597d\u5904\u662f\u7701\u5185\u5b58&#xff0c;\u5c5e\u6027\u591a\u3001\u5bf9\u8c61\u591a\u7684\u573a\u666f\u4e0b\u5dee\u8ddd\u4e0d\u5c0f\u3002<\/p>\n<h4>2.6 \u7c7b\u5c5e\u6027\u548c\u5b9e\u4f8b\u5c5e\u6027&#xff0c;\u4e24\u7801\u4e8b<\/h4>\n<p>\u8fd9\u5757\u7684\u5751\u6211\u8e29\u8fc7&#xff0c;\u800c\u4e14\u8e29\u5f97\u5f88\u75bc\u3002<\/p>\n<p>\u5199\u5728\u7c7b\u91cc\u3001\u4e0d\u5e26 self \u7684\u53d8\u91cf\u662f\u7c7b\u5c5e\u6027&#xff0c;\u5168\u7c7b\u5171\u7528\u4e00\u4efd\u3002\u5199\u5728 __init__ \u91cc\u3001\u5e26 self. \u7684\u662f\u5b9e\u4f8b\u5c5e\u6027&#xff0c;\u6bcf\u4e2a\u5bf9\u8c61\u5404\u6709\u4e00\u4efd\u3002<\/p>\n<p>&gt;&gt;&gt; class Dog:<br \/>\n&#8230;     kind &#061; &#034;\u72ac\u79d1&#034;          # \u7c7b\u5c5e\u6027&#xff0c;\u6240\u6709\u72d7\u5171\u7528<br \/>\n&#8230;     def __init__(self, name):<br \/>\n&#8230;         self.name &#061; name   # \u5b9e\u4f8b\u5c5e\u6027&#xff0c;\u6bcf\u53ea\u72d7\u4e00\u4e2a<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; d1 &#061; Dog(&#034;\u5c0f\u9ed1&#034;)<br \/>\n&gt;&gt;&gt; d2 &#061; Dog(&#034;\u5c0f\u767d&#034;)<br \/>\n&gt;&gt;&gt; Dog.kind, d1.kind, d2.kind<br \/>\n(&#039;\u72ac\u79d1&#039;, &#039;\u72ac\u79d1&#039;, &#039;\u72ac\u79d1&#039;)<\/p>\n<p>\u6539\u7c7b\u5c5e\u6027&#xff0c;\u6240\u6709\u5bf9\u8c61\u5168\u90fd\u8ddf\u7740\u53d8\u3002<\/p>\n<p>&gt;&gt;&gt; Dog.kind &#061; &#034;\u54fa\u4e73\u52a8\u7269&#034;<br \/>\n&gt;&gt;&gt; d1.kind, d2.kind<br \/>\n(&#039;\u54fa\u4e73\u52a8\u7269&#039;, &#039;\u54fa\u4e73\u52a8\u7269&#039;)<\/p>\n<p>\u53cd\u8fc7\u6765\u4e0d\u884c&#xff0c;\u5b9e\u4f8b\u5c5e\u6027\u4e0d\u5c5e\u4e8e\u7c7b\u3002<\/p>\n<p>&gt;&gt;&gt; Dog.name<br \/>\nAttributeError: type object &#039;Dog&#039; has no attribute &#039;name&#039;<\/p>\n<p>\u67e5\u627e\u987a\u5e8f\u8981\u8bb0\u7262&#xff0c;\u5148\u627e\u5bf9\u8c61\u81ea\u5df1\u8eab\u4e0a\u6709\u6ca1\u6709&#xff0c;\u6ca1\u6709\u518d\u53bb\u7c7b\u91cc\u627e&#xff0c;\u7c7b\u91cc\u4e5f\u6ca1\u6709\u5c31\u62a5 AttributeError\u3002<\/p>\n<p>d1.kind<br \/>\n   \u2502<br \/>\n   \u251c\u2500 \u67e5 d1.__dict__  \u2192 \u6ca1\u6709 kind<br \/>\n   \u2502<br \/>\n   \u251c\u2500 \u67e5 Dog.__dict__ \u2192 \u627e\u5230\u4e86 &#034;\u72ac\u79d1&#034;<br \/>\n   \u2502<br \/>\n   \u2514\u2500 \u90fd\u6ca1\u6709 \u2192 AttributeError<\/p>\n<p>\u63a5\u4e0b\u6765\u662f\u90a3\u4e2a\u628a\u6211\u5751\u75bc\u7684\u4e1c\u897f\u3002\u5982\u679c\u7c7b\u5c5e\u6027\u662f\u4e2a\u53ef\u53d8\u5bf9\u8c61&#xff0c;\u6240\u6709\u5bf9\u8c61\u5171\u4eab\u7684\u662f\u540c\u4e00\u4e2a\u3002<\/p>\n<p>&gt;&gt;&gt; class Dog:<br \/>\n&#8230;     tricks &#061; []<br \/>\n&#8230;     def __init__(self, name):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;     def teach(self, trick):<br \/>\n&#8230;         self.tricks.append(trick)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; a &#061; Dog(&#034;\u5c0f\u9ed1&#034;)<br \/>\n&gt;&gt;&gt; b &#061; Dog(&#034;\u5c0f\u767d&#034;)<br \/>\n&gt;&gt;&gt; a.teach(&#034;\u5750\u4e0b&#034;)<br \/>\n&gt;&gt;&gt; a.tricks<br \/>\n[&#039;\u5750\u4e0b&#039;]<br \/>\n&gt;&gt;&gt; b.tricks<br \/>\n[&#039;\u5750\u4e0b&#039;]<\/p>\n<p>\u6211\u53ea\u6559\u4e86\u5c0f\u9ed1\u5750\u4e0b&#xff0c;\u5c0f\u767d\u4e5f\u5b66\u4f1a\u4e86\u3002<\/p>\n<p>Dog.tricks \u2500\u2500\u25ba []            \u2190 \u7c7b\u91cc\u53ea\u6709\u8fd9\u4e00\u4e2a\u5217\u8868<br \/>\n                 \u25b2<br \/>\n                 \u2502 \u540c\u4e00\u4e2a\u5bf9\u8c61<br \/>\n    a.tricks \u2500\u2500\u2500\u2500\u2524<br \/>\n                 \u2502<br \/>\n    b.tricks \u2500\u2500\u2500\u2500\u2518<\/p>\n<p>\u8bc1\u636e\u5f88\u597d\u62ff\u3002<\/p>\n<p>&gt;&gt;&gt; a.tricks is b.tricks<br \/>\nTrue<br \/>\n&gt;&gt;&gt; a.tricks is Dog.tricks<br \/>\nTrue<\/p>\n<p>\u8fd9\u4e2a\u75c5\u56e0\u5728\u7b2c\u516d\u7ae0 2.4 \u8bb2\u9ed8\u8ba4\u53c2\u6570\u65f6\u89c1\u8fc7\u4e00\u6b21&#xff0c;\u90fd\u662f&#034;\u53ef\u53d8\u5bf9\u8c61\u5728\u5b9a\u4e49\u65f6\u88ab\u521b\u5efa\u4e86\u4e00\u6b21&#xff0c;\u4e4b\u540e\u5927\u5bb6\u5171\u7528&#034;\u3002\u4fee\u6cd5\u4e5f\u4e00\u6837&#xff0c;\u628a\u5b83\u632a\u5230 __init__ \u91cc&#xff0c;\u8ba9\u6bcf\u4e2a\u5bf9\u8c61\u81ea\u5df1\u9020\u4e00\u4e2a\u3002<\/p>\n<p>&gt;&gt;&gt; class Dog:<br \/>\n&#8230;     def __init__(self, name):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;         self.tricks &#061; []<br \/>\n&#8230;     def teach(self, trick):<br \/>\n&#8230;         self.tricks.append(trick)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; a &#061; Dog(&#034;\u5c0f\u9ed1&#034;)<br \/>\n&gt;&gt;&gt; b &#061; Dog(&#034;\u5c0f\u767d&#034;)<br \/>\n&gt;&gt;&gt; a.teach(&#034;\u5750\u4e0b&#034;)<br \/>\n&gt;&gt;&gt; a.teach(&#034;\u63e1\u624b&#034;)<br \/>\n&gt;&gt;&gt; a.tricks<br \/>\n[&#039;\u5750\u4e0b&#039;, &#039;\u63e1\u624b&#039;]<br \/>\n&gt;&gt;&gt; b.tricks<br \/>\n[]<\/p>\n<p>\u8fd9\u4e0b\u5404\u5b66\u5404\u7684\u4e86\u3002<\/p>\n<p>\u7c7b\u5c5e\u6027\u4e5f\u4e0d\u662f\u6d2a\u6c34\u731b\u517d&#xff0c;\u6709\u4e2a\u7528\u6cd5\u5f88\u6b63\u7edf&#xff0c;\u7edf\u8ba1\u4e00\u5171\u521b\u5efa\u4e86\u591a\u5c11\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p>&gt;&gt;&gt; class User:<br \/>\n&#8230;     total &#061; 0<br \/>\n&#8230;     def __init__(self, name):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;         User.total &#043;&#061; 1<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; u1 &#061; User(&#034;\u5f20\u4e09&#034;)<br \/>\n&gt;&gt;&gt; u2 &#061; User(&#034;\u674e\u56db&#034;)<br \/>\n&gt;&gt;&gt; User.total<br \/>\n2<br \/>\n&gt;&gt;&gt; u1.total<br \/>\n2<\/p>\n<p>\u6ce8\u610f User.total &#043;&#061; 1 \u91cc\u5199\u7684\u662f\u7c7b\u540d\u3002\u5199\u6210 self.total &#043;&#061; 1 \u7684\u8bdd&#xff0c;\u7b2c\u4e00\u6b21\u8d4b\u503c\u4f1a\u5728\u5bf9\u8c61\u8eab\u4e0a\u65b0\u5efa\u4e00\u4e2a\u5b9e\u4f8b\u5c5e\u6027&#xff0c;\u7c7b\u5c5e\u6027\u6c38\u8fdc\u662f 0&#xff0c;\u8ba1\u6570\u5668\u5c31\u4e0d\u7075\u4e86\u3002\u8fd9\u4e2a\u533a\u522b\u503c\u5f97\u81ea\u5df1\u6572\u4e00\u904d\u4f53\u4f1a\u3002<\/p>\n<h4>2.7 \u8ba9\u5bf9\u8c61\u6253\u5370\u51fa\u4eba\u80fd\u770b\u7684\u4e1c\u897f<\/h4>\n<p>\u76f4\u63a5\u6253\u5370\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u51fa\u6765\u7684\u662f\u4e00\u5806\u5730\u5740\u3002<\/p>\n<p>&gt;&gt;&gt; class Point:<br \/>\n&#8230;     def __init__(self, x, y):<br \/>\n&#8230;         self.x &#061; x<br \/>\n&#8230;         self.y &#061; y<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; p &#061; Point(3, 4)<br \/>\n&gt;&gt;&gt; print(p)<br \/>\n&lt;__main__.Point object at 0x000001E4A5B6C7D8&gt;<\/p>\n<p>\u8c03\u8bd5\u7684\u65f6\u5019\u8fd9\u4e32\u4e1c\u897f\u6beb\u65e0\u4fe1\u606f\u91cf\u3002\u52a0\u4e2a __str__ \u5c31\u597d\u3002<\/p>\n<p>&gt;&gt;&gt; class Point:<br \/>\n&#8230;     def __init__(self, x, y):<br \/>\n&#8230;         self.x &#061; x<br \/>\n&#8230;         self.y &#061; y<br \/>\n&#8230;     def __str__(self):<br \/>\n&#8230;         return &#034;(%d, %d)&#034; % (self.x, self.y)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; p &#061; Point(3, 4)<br \/>\n&gt;&gt;&gt; print(p)<br \/>\n(3, 4)<br \/>\n&gt;&gt;&gt; str(p)<br \/>\n&#039;(3, 4)&#039;<\/p>\n<p>\u4f46\u76f4\u63a5\u6572 p \u56de\u8f66\u8fd8\u662f\u90a3\u4e32\u5730\u5740\u3002<\/p>\n<p>&gt;&gt;&gt; p<br \/>\n&lt;__main__.Point object at 0x000001E4A5B6C7D8&gt;<\/p>\n<p>\u56e0\u4e3a\u4ea4\u4e92\u73af\u5883\u56de\u663e\u7528\u7684\u662f __repr__&#xff0c;\u4e0d\u662f __str__\u3002\u4e24\u4e2a\u90fd\u5199\u4e0a\u624d\u7b97\u5b8c\u6574\u3002<\/p>\n<p>&gt;&gt;&gt; class Point:<br \/>\n&#8230;     def __init__(self, x, y):<br \/>\n&#8230;         self.x &#061; x<br \/>\n&#8230;         self.y &#061; y<br \/>\n&#8230;     def __repr__(self):<br \/>\n&#8230;         return &#034;Point(%d, %d)&#034; % (self.x, self.y)<br \/>\n&#8230;     def __str__(self):<br \/>\n&#8230;         return &#034;(%d, %d)&#034; % (self.x, self.y)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; p &#061; Point(3, 4)<br \/>\n&gt;&gt;&gt; print(p)<br \/>\n(3, 4)<br \/>\n&gt;&gt;&gt; p<br \/>\nPoint(3, 4)<\/p>\n<p>\u4e24\u8005\u7684\u5206\u5de5\u662f\u8fd9\u6837&#xff0c;__str__ \u9762\u5411\u7528\u6237&#xff0c;\u597d\u770b\u5c31\u884c&#xff1b;__repr__ \u9762\u5411\u5f00\u53d1\u8005&#xff0c;\u6700\u597d\u662f\u90a3\u79cd\u590d\u5236\u56de\u4ee3\u7801\u91cc\u80fd\u91cd\u5efa\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u3002\u771f\u4e0d\u60f3\u5199\u4e24\u4e2a&#xff0c;\u5c31\u53ea\u5199 __repr__&#xff0c;Python \u4f1a\u5728\u6ca1 __str__ \u7684\u65f6\u5019\u62ff\u5b83\u9876\u4e0a\u3002<\/p>\n<h4>2.8 \u65b9\u6cd5\u5176\u5b9e\u4e5f\u662f\u5c5e\u6027<\/h4>\n<p>\u65e2\u7136\u4e07\u7269\u7686\u5bf9\u8c61&#xff0c;\u65b9\u6cd5\u5f53\u7136\u4e5f\u662f\u3002<\/p>\n<p>&gt;&gt;&gt; class Calc:<br \/>\n&#8230;     def add(self, a, b):<br \/>\n&#8230;         return a &#043; b<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; c &#061; Calc()<br \/>\n&gt;&gt;&gt; type(Calc.add)<br \/>\n&lt;class &#039;function&#039;&gt;<br \/>\n&gt;&gt;&gt; type(c.add)<br \/>\n&lt;class &#039;method&#039;&gt;<br \/>\n&gt;&gt;&gt; Calc.add(c, 1, 2)<br \/>\n3<br \/>\n&gt;&gt;&gt; c.add(1, 2)<br \/>\n3<br \/>\n&gt;&gt;&gt; f &#061; c.add<br \/>\n&gt;&gt;&gt; f(3, 4)<br \/>\n7<\/p>\n<p>\u4ece\u7c7b\u4e0a\u53d6\u51fa\u6765\u7684\u662f\u666e\u901a\u51fd\u6570 function&#xff0c;\u4ece\u5bf9\u8c61\u4e0a\u53d6\u51fa\u6765\u7684\u662f\u5df2\u7ecf\u7ed1\u597d self \u7684 method\u3002f &#061; c.add \u628a\u65b9\u6cd5\u5b58\u8fdb\u53d8\u91cf&#xff0c;\u4e4b\u540e f(3, 4) \u4e0d\u7528\u518d\u4f20 self&#xff0c;\u56e0\u4e3a\u5b83\u5df2\u7ecf\u7c98\u5728 c \u8eab\u4e0a\u4e86\u3002<\/p>\n<p>\u8fd9\u4e2a\u7279\u6027\u5728\u56de\u8c03\u3001\u4e8b\u4ef6\u7ed1\u5b9a\u91cc\u7279\u522b\u5e38\u89c1&#xff0c;\u7b2c\u516d\u7ae0\u8bb2\u8fc7\u7684\u90a3\u4e00\u5957\u5728\u8fd9\u513f\u4e00\u6837\u6210\u7acb\u3002<\/p>\n<h4>2.9 \u79c1\u6709\u5c5e\u6027&#xff0c;\u4ee5\u53ca\u5b83\u5176\u5b9e\u4e0d\u90a3\u4e48\u79c1\u6709<\/h4>\n<p>\u5c5e\u6027\u52a0\u4e24\u4e2a\u4e0b\u5212\u7ebf\u5f00\u5934&#xff0c;\u5c31\u6210\u4e86\u79c1\u6709\u5c5e\u6027\u3002<\/p>\n<p>&gt;&gt;&gt; class Wallet:<br \/>\n&#8230;     def __init__(self):<br \/>\n&#8230;         self.__money &#061; 0<br \/>\n&#8230;     def save(self, amount):<br \/>\n&#8230;         self.__money &#043;&#061; amount<br \/>\n&#8230;     def balance(self):<br \/>\n&#8230;         return self.__money<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; w &#061; Wallet()<br \/>\n&gt;&gt;&gt; w.save(100)<br \/>\n&gt;&gt;&gt; w.balance()<br \/>\n100<br \/>\n&gt;&gt;&gt; w.__money<br \/>\nAttributeError: &#039;Wallet&#039; object has no attribute &#039;__money&#039;<\/p>\n<p>\u5916\u9762\u8bbf\u95ee\u4e0d\u5230\u3002\u4e0d\u8fc7\u770b\u4e00\u773c __dict__&#xff0c;\u771f\u76f8\u5c31\u9732\u4e86\u3002<\/p>\n<p>&gt;&gt;&gt; w.__dict__<br \/>\n{&#039;_Wallet__money&#039;: 100}<br \/>\n&gt;&gt;&gt; w._Wallet__money<br \/>\n100<\/p>\n<p>Python \u6ca1\u6709\u771f\u6b63\u7684\u79c1\u6709&#xff0c;\u5b83\u53ea\u662f\u628a __money \u8fd9\u4e2a\u540d\u5b57\u6539\u6210\u4e86 _Wallet__money&#xff0c;\u672f\u8bed\u53eb\u540d\u5b57\u6539\u5199&#xff08;name mangling&#xff09;\u3002\u4f60\u975e\u8981\u7528\u5168\u540d\u53bb\u8bbf\u95ee&#xff0c;\u7167\u6837\u62ff\u5f97\u5230\u3002<\/p>\n<p>\u6240\u4ee5\u8fd9\u662f\u7ea6\u5b9a\u800c\u4e0d\u662f\u5f3a\u5236\u3002Python \u7684\u54f2\u5b66\u662f&#034;\u6211\u4eec\u90fd\u662f\u6210\u5e74\u4eba&#034;&#xff0c;\u5b83\u7ed9\u4f60\u4e00\u4e2a\u660e\u786e\u7684\u4fe1\u53f7&#xff0c;\u4f46\u4e0d\u4f1a\u62ff\u624b\u94d0\u94d0\u4f4f\u4f60\u3002\u771f\u8981\u9632\u7684\u662f\u8bef\u89e6&#xff0c;\u4e0d\u662f\u6076\u610f\u3002<\/p>\n<p>\u4e09\u79cd\u4e0b\u5212\u7ebf\u5f00\u5934\u7684\u5199\u6cd5&#xff0c;\u542b\u4e49\u4e0d\u4e00\u6837\u3002<\/p>\n<table>\n<tr>\u5199\u6cd5\u542b\u4e49\u80fd\u88ab\u8bbf\u95ee\u5417<\/tr>\n<tbody>\n<tr>\n<td>name<\/td>\n<td>\u516c\u5f00<\/td>\n<td>\u80fd<\/td>\n<\/tr>\n<tr>\n<td>_name<\/td>\n<td>\u7ea6\u5b9a\u4e0a\u7684\u5185\u90e8\u7528&#xff0c;\u522b\u4ece\u5916\u9762\u78b0<\/td>\n<td>\u80fd&#xff0c;\u4f46\u4e0d\u8be5<\/td>\n<\/tr>\n<tr>\n<td>__name<\/td>\n<td>\u540d\u5b57\u6539\u5199&#xff0c;\u907f\u514d\u5b50\u7c7b\u8bef\u8986\u76d6<\/td>\n<td>\u7528\u6539\u5199\u540e\u7684\u5168\u540d\u80fd<\/td>\n<\/tr>\n<tr>\n<td>__name__<\/td>\n<td>Python \u7684\u7279\u6b8a\u65b9\u6cd5<\/td>\n<td>\u522b\u81ea\u5df1\u53d1\u660e\u8fd9\u79cd\u540d\u5b57<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6700\u540e\u4e00\u884c\u90a3\u4e2a\u8981\u7559\u610f&#xff0c;__init__\u3001__str__ \u8fd9\u79cd\u524d\u540e\u90fd\u6709\u53cc\u4e0b\u5212\u7ebf\u7684\u662f Python \u9884\u7559\u7684&#xff0c;\u81ea\u5df1\u9020\u4e00\u4e2a __size__ \u51fa\u6765&#xff0c;\u5c06\u6765\u7248\u672c\u5347\u7ea7\u649e\u4e0a\u4e86\u5c31\u9ebb\u70e6\u3002<\/p>\n<h4>2.10 \u9759\u6001\u65b9\u6cd5\u548c\u7c7b\u65b9\u6cd5<\/h4>\n<p>\u524d\u9762\u5199\u7684\u90fd\u662f\u5b9e\u4f8b\u65b9\u6cd5&#xff0c;\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f self\u3002\u8fd8\u6709\u4e24\u79cd\u3002<\/p>\n<p>\u9759\u6001\u65b9\u6cd5\u7528 &#064;staticmethod \u88c5\u9970&#xff0c;\u4e0d\u9700\u8981 self&#xff0c;\u903b\u8f91\u4e0a\u5c5e\u4e8e\u8fd9\u4e2a\u7c7b\u4f46\u4e0d\u78b0\u4efb\u4f55\u5b9e\u4f8b\u6570\u636e\u3002<\/p>\n<p>&gt;&gt;&gt; class MathTool:<br \/>\n&#8230;     &#064;staticmethod<br \/>\n&#8230;     def add(a, b):<br \/>\n&#8230;         return a &#043; b<br \/>\n&#8230;     &#064;staticmethod<br \/>\n&#8230;     def is_even(n):<br \/>\n&#8230;         return n % 2 &#061;&#061; 0<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; MathTool.add(3, 5)<br \/>\n8<br \/>\n&gt;&gt;&gt; MathTool.is_even(4)<br \/>\nTrue<br \/>\n&gt;&gt;&gt; m &#061; MathTool()<br \/>\n&gt;&gt;&gt; m.add(1, 2)<br \/>\n3<\/p>\n<p>\u6ce8\u610f m.add(1, 2) \u4e5f\u80fd\u8dd1&#xff0c;\u9759\u6001\u65b9\u6cd5\u4e0d\u4f1a\u6536\u5230 self&#xff0c;\u5199\u6210 m.add(1, 2, 3) \u53cd\u800c\u62a5\u9519\u3002<\/p>\n<p>\u5224\u65ad\u8d77\u6765\u7b80\u5355&#xff0c;\u4e00\u4e2a\u65b9\u6cd5\u4ece\u5934\u5230\u5c3e\u6ca1\u7528\u8fc7 self&#xff0c;\u5c31\u8be5\u8003\u8651\u6539\u6210\u9759\u6001\u65b9\u6cd5\u3002\u5b83\u591a\u534a\u53ea\u662f\u6070\u597d\u8ddf\u8fd9\u4e2a\u7c7b\u6709\u5173\u8054\u3002<\/p>\n<p>\u7c7b\u65b9\u6cd5\u7528 &#064;classmethod \u88c5\u9970&#xff0c;\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u7c7b\u672c\u8eab&#xff0c;\u4e60\u60ef\u5199\u6210 cls\u3002<\/p>\n<p>&gt;&gt;&gt; class Date:<br \/>\n&#8230;     def __init__(self, year, month, day):<br \/>\n&#8230;         self.year &#061; year<br \/>\n&#8230;         self.month &#061; month<br \/>\n&#8230;         self.day &#061; day<br \/>\n&#8230;     &#064;classmethod<br \/>\n&#8230;     def from_string(cls, text):<br \/>\n&#8230;         y, m, d &#061; text.split(&#034;-&#034;)<br \/>\n&#8230;         return cls(int(y), int(m), int(d))<br \/>\n&#8230;     def __str__(self):<br \/>\n&#8230;         return &#034;%04d-%02d-%02d&#034; % (self.year, self.month, self.day)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; d &#061; Date.from_string(&#034;2026-10-07&#034;)<br \/>\n&gt;&gt;&gt; print(d)<br \/>\n2026-10-07<\/p>\n<p>cls(&#8230;) \u800c\u4e0d\u662f\u5199\u6b7b Date(&#8230;)&#xff0c;\u597d\u5904\u662f\u5b50\u7c7b\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u65f6\u9020\u51fa\u6765\u7684\u662f\u5b50\u7c7b\u5bf9\u8c61\u3002\u8fd9\u79cd\u6a21\u5f0f\u53eb\u5de5\u5382\u65b9\u6cd5&#xff0c;\u89e3\u6790\u5404\u79cd\u683c\u5f0f\u7684\u8f93\u5165\u65f6\u7279\u522b\u987a\u624b\u3002<\/p>\n<h4>2.11 \u7c7b\u7684\u9ad8\u9891\u62a5\u9519<\/h4>\n<table>\n<tr>\u62a5\u9519\u539f\u56e0<\/tr>\n<tbody>\n<tr>\n<td>takes 0 positional arguments but 1 was given<\/td>\n<td>\u65b9\u6cd5\u6f0f\u5199\u4e86 self<\/td>\n<\/tr>\n<tr>\n<td>missing 1 required positional argument: &#039;self&#039;<\/td>\n<td>\u7528\u7c7b\u540d\u76f4\u63a5\u8c03\u7528\u4e86\u5b9e\u4f8b\u65b9\u6cd5&#xff0c;\u5e94\u8be5\u5148\u5efa\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td>AttributeError: type object &#039;X&#039; has no attribute &#039;y&#039;<\/td>\n<td>\u62ff\u7c7b\u540d\u8bbf\u95ee\u5b9e\u4f8b\u5c5e\u6027<\/td>\n<\/tr>\n<tr>\n<td>\u6539\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027&#xff0c;\u53e6\u4e00\u4e2a\u4e5f\u53d8\u4e86<\/td>\n<td>\u7c7b\u5c5e\u6027\u662f\u53ef\u53d8\u5bf9\u8c61&#xff0c;\u88ab\u5171\u4eab\u4e86<\/td>\n<\/tr>\n<tr>\n<td>TypeError: __init__() takes &#8230;<\/td>\n<td>\u5efa\u5bf9\u8c61\u65f6\u4f20\u7684\u53c2\u6570\u4e2a\u6570\u8ddf __init__ \u5bf9\u4e0d\u4e0a<\/td>\n<\/tr>\n<tr>\n<td>\u62fc\u9519\u5c5e\u6027\u540d\u4e0d\u62a5\u9519<\/td>\n<td>Python \u5141\u8bb8\u52a8\u6001\u52a0\u5c5e\u6027&#xff0c;\u7528 __slots__ \u80fd\u6321\u4f4f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7b2c\u4e8c\u6761\u90a3\u4e2a&#xff0c;\u6211\u89c1\u8fc7\u6709\u4eba\u5199 Student.get_name() \u60f3\u7701\u6389\u5efa\u5bf9\u8c61\u7684\u6b65\u9aa4&#xff0c;\u62a5\u7684\u5c31\u662f\u8fd9\u4e2a\u3002<\/p>\n<hr \/>\n<h3>3. \u5c5e\u6027&#xff08;property&#xff09;<\/h3>\n<p>\u8fd9\u4e00\u8282\u8bb2\u4e00\u4e2a\u8ba9 Python \u7684\u7c7b\u6bd4\u522b\u7684\u8bed\u8a00\u8212\u670d\u4e0d\u5c11\u7684\u4e1c\u897f\u3002<\/p>\n<h4>3.1 \u4ece\u4e00\u6bb5\u7ffb\u8f66\u7684\u4ee3\u7801\u8bf4\u8d77<\/h4>\n<p>\u4e00\u5f00\u59cb\u6211\u5199\u7684\u5b66\u751f\u7c7b\u662f\u8fd9\u6837\u7684&#xff0c;\u7b80\u5355\u76f4\u63a5\u3002<\/p>\n<p>&gt;&gt;&gt; class Student:<br \/>\n&#8230;     def __init__(self, name, score):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;         self.score &#061; score<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; s &#061; Student(&#034;\u5f20\u4e09&#034;, 88)<br \/>\n&gt;&gt;&gt; s.score<br \/>\n88<\/p>\n<p>\u8fd0\u884c\u5f97\u597d\u597d\u7684&#xff0c;\u76f4\u5230\u6709\u4e00\u5929\u6559\u52a1\u5904\u6253\u7535\u8bdd\u8fc7\u6765&#xff0c;\u8bf4\u6709\u4e2a\u5b66\u751f\u7684\u5206\u6570\u662f 999\u3002\u67e5\u4e0b\u53bb\u53d1\u73b0\u662f\u5f55\u5165\u7684\u65f6\u5019\u624b\u6ed1\u591a\u6572\u4e86\u4e24\u4e2a 9&#xff0c;\u800c\u6211\u7684\u4ee3\u7801\u4ece\u5934\u5230\u5c3e\u6ca1\u6709\u4e00\u884c\u62e6\u5f97\u4f4f\u3002<\/p>\n<p>&gt;&gt;&gt; s.score &#061; 999<br \/>\n&gt;&gt;&gt; s.score &#061; -20<br \/>\n&gt;&gt;&gt; s.score<br \/>\n-20<\/p>\n<p>\u5c5e\u6027\u662f\u655e\u5f00\u7684&#xff0c;\u8c01\u90fd\u80fd\u6539&#xff0c;\u6539\u6210\u4ec0\u4e48\u90fd\u884c\u3002<\/p>\n<p>\u7b2c\u4e00\u53cd\u5e94\u662f\u52a0\u6821\u9a8c\u3002\u6700\u571f\u7684\u529e\u6cd5\u662f\u5199\u4e00\u5bf9 get \u548c set \u65b9\u6cd5&#xff0c;\u628a\u5c5e\u6027\u85cf\u8d77\u6765\u3002<\/p>\n<p>&gt;&gt;&gt; class Student:<br \/>\n&#8230;     def __init__(self, name, score):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;         self.set_score(score)<br \/>\n&#8230;     def get_score(self):<br \/>\n&#8230;         return self._score<br \/>\n&#8230;     def set_score(self, value):<br \/>\n&#8230;         if not 0 &lt;&#061; value &lt;&#061; 100:<br \/>\n&#8230;             raise ValueError(&#034;\u5206\u6570\u5fc5\u987b\u5728 0 \u5230 100 \u4e4b\u95f4&#034;)<br \/>\n&#8230;         self._score &#061; value<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; s &#061; Student(&#034;\u5f20\u4e09&#034;, 88)<br \/>\n&gt;&gt;&gt; s.get_score()<br \/>\n88<br \/>\n&gt;&gt;&gt; s.set_score(120)<br \/>\nValueError: \u5206\u6570\u5fc5\u987b\u5728 0 \u5230 100 \u4e4b\u95f4<\/p>\n<p>\u80fd\u62e6\u4f4f\u4e86&#xff0c;\u4ee3\u4ef7\u662f\u8c03\u7528\u5168\u53d8\u4e86\u3002\u9879\u76ee\u91cc\u51e1\u662f\u5199\u8fc7 s.score &#061; x \u7684\u5730\u65b9&#xff0c;\u5168\u5f97\u6539\u6210 s.set_score(x)&#xff0c;\u51e1\u662f\u8bfb\u8fc7 s.score \u7684\u5730\u65b9&#xff0c;\u5168\u5f97\u6539\u6210 s.get_score()\u3002\u4ece Java \u8f6c\u8fc7\u6765\u7684\u4eba\u5bf9\u8fd9\u5957\u719f\u5f97\u5f88&#xff0c;\u53ef Python \u91cc\u8fd9\u4e48\u5199\u5b9e\u5728\u5570\u55e6\u3002<\/p>\n<h4>3.2 property \u5c31\u662f\u6765\u89e3\u51b3\u8fd9\u4e2a\u7684<\/h4>\n<p>\u7528 &#064;property \u4e4b\u540e&#xff0c;\u5916\u9762\u8fd8\u662f s.score \u8fd9\u79cd\u5199\u6cd5&#xff0c;\u5e95\u4e0b\u5374\u5df2\u7ecf\u6362\u6210\u4e86\u65b9\u6cd5\u3002<\/p>\n<p>&gt;&gt;&gt; class Student:<br \/>\n&#8230;     def __init__(self, name, score):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;         self.score &#061; score<br \/>\n&#8230;     &#064;property<br \/>\n&#8230;     def score(self):<br \/>\n&#8230;         return self._score<br \/>\n&#8230;     &#064;score.setter<br \/>\n&#8230;     def score(self, value):<br \/>\n&#8230;         if not 0 &lt;&#061; value &lt;&#061; 100:<br \/>\n&#8230;             raise ValueError(&#034;\u5206\u6570\u5fc5\u987b\u5728 0 \u5230 100 \u4e4b\u95f4&#034;)<br \/>\n&#8230;         self._score &#061; value<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; s &#061; Student(&#034;\u5f20\u4e09&#034;, 88)<br \/>\n&gt;&gt;&gt; s.score<br \/>\n88<br \/>\n&gt;&gt;&gt; s.score &#061; 95<br \/>\n&gt;&gt;&gt; s.score<br \/>\n95<br \/>\n&gt;&gt;&gt; s.score &#061; 120<br \/>\nValueError: \u5206\u6570\u5fc5\u987b\u5728 0 \u5230 100 \u4e4b\u95f4<br \/>\n&gt;&gt;&gt; Student(&#034;\u674e\u56db&#034;, 999)<br \/>\nValueError: \u5206\u6570\u5fc5\u987b\u5728 0 \u5230 100 \u4e4b\u95f4<\/p>\n<p>\u770b\u6e05\u695a\u53d1\u751f\u4e86\u4ec0\u4e48&#xff0c;s.score &#061; 95 \u770b\u7740\u50cf\u666e\u901a\u8d4b\u503c&#xff0c;\u5b9e\u9645\u8c03\u7528\u7684\u662f\u90a3\u4e2a\u5e26 &#064;score.setter \u7684\u65b9\u6cd5\u3002\u800c s.score \u770b\u7740\u50cf\u8bfb\u5c5e\u6027&#xff0c;\u5b9e\u9645\u8d70\u7684\u662f &#064;property \u90a3\u4e2a\u3002<\/p>\n<p>\u8c03\u7528\u65b9\u7684\u4ee3\u7801\u4e00\u884c\u4e0d\u7528\u6539&#xff0c;\u6821\u9a8c\u5c31\u6084\u6084\u52a0\u4e0a\u4e86\u3002\u8fd9\u662f property \u6700\u5927\u7684\u4ef7\u503c&#xff0c;\u5728\u4e0d\u7834\u574f\u5df2\u6709\u63a5\u53e3\u7684\u524d\u63d0\u4e0b&#xff0c;\u628a\u88f8\u5c5e\u6027\u5347\u7ea7\u6210\u53d7\u63a7\u5c5e\u6027\u3002<\/p>\n<p>\u6ce8\u610f __init__ \u91cc\u5199\u7684\u662f self.score &#061; score \u800c\u4e0d\u662f self._score &#061; score&#xff0c;\u6545\u610f\u8ba9\u5b83\u8d70\u4e00\u904d setter&#xff0c;\u8fd9\u6837\u8fde\u521b\u5efa\u5bf9\u8c61\u65f6\u4f20\u8fdb\u6765\u7684\u810f\u6570\u636e\u4e5f\u62e6\u5f97\u4f4f&#xff0c;\u6700\u540e\u90a3\u4e2a Student(&#034;\u674e\u56db&#034;, 999) \u62a5\u9519\u5c31\u662f\u8bc1\u636e\u3002<\/p>\n<h4>3.3 \u8bed\u6cd5\u7ec6\u8282&#xff0c;\u4e09\u4e2a\u88c5\u9970\u5668\u662f\u4e00\u5957\u7684<\/h4>\n<p>&#064;property                    \u2190 \u88c5\u9970\u8bfb\u7684\u65b9\u6cd5&#xff0c;\u65b9\u6cd5\u540d\u5c31\u662f\u5c5e\u6027\u540d<br \/>\ndef score(self): &#8230;<\/p>\n<p>&#064;score.setter                \u2190 \u88c5\u9970\u5199\u7684\u65b9\u6cd5&#xff0c;\u5fc5\u987b\u53eb\u540c\u4e00\u4e2a\u540d\u5b57<br \/>\ndef score(self, value): &#8230;<\/p>\n<p>&#064;score.deleter               \u2190 \u88c5\u9970\u5220\u7684\u65b9\u6cd5&#xff0c;\u53ef\u9009<br \/>\ndef score(self): &#8230;<\/p>\n<p>\u4e09\u6761\u89c4\u77e9\u3002<\/p>\n<p>\u65b9\u6cd5\u540d\u5c31\u662f\u5bf9\u5916\u66b4\u9732\u7684\u5c5e\u6027\u540d&#xff0c;\u4e09\u4e2a\u88c5\u9970\u5668\u4e0b\u9762\u7684\u51fd\u6570\u540d\u5fc5\u987b\u4e00\u81f4&#xff0c;\u5199\u9519\u4e86\u5c31\u53d8\u6210\u4e24\u4e2a\u4e0d\u76f8\u5e72\u7684\u4e1c\u897f\u3002<\/p>\n<p>\u987a\u5e8f\u65e0\u6240\u8c13&#xff0c;\u4f46\u4e60\u60ef\u4e0a getter \u5199\u6700\u524d\u9762\u3002<\/p>\n<p>\u53ea\u6709 getter \u6ca1\u6709 setter&#xff0c;\u8fd9\u4e2a\u5c5e\u6027\u5c31\u662f\u53ea\u8bfb\u7684\u3002<\/p>\n<p>deleter \u7528\u5f97\u5c11&#xff0c;\u770b\u4e00\u773c\u77e5\u9053\u5b83\u5b58\u5728\u5c31\u884c\u3002<\/p>\n<p>&gt;&gt;&gt; class Demo:<br \/>\n&#8230;     def __init__(self):<br \/>\n&#8230;         self._x &#061; 1<br \/>\n&#8230;     &#064;property<br \/>\n&#8230;     def x(self):<br \/>\n&#8230;         return self._x<br \/>\n&#8230;     &#064;x.setter<br \/>\n&#8230;     def x(self, value):<br \/>\n&#8230;         self._x &#061; value<br \/>\n&#8230;     &#064;x.deleter<br \/>\n&#8230;     def x(self):<br \/>\n&#8230;         print(&#034;x \u88ab\u5220\u4e86&#034;)<br \/>\n&#8230;         del self._x<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; d &#061; Demo()<br \/>\n&gt;&gt;&gt; d.x &#061; 5<br \/>\n&gt;&gt;&gt; d.x<br \/>\n5<br \/>\n&gt;&gt;&gt; del d.x<br \/>\nx \u88ab\u5220\u4e86<br \/>\n&gt;&gt;&gt; d.__dict__<br \/>\n{}<\/p>\n<h4>3.4 \u53ea\u8bfb\u5c5e\u6027&#xff0c;\u4e0d\u7ed9 setter \u5c31\u884c<\/h4>\n<p>\u6709\u4e9b\u503c\u5929\u751f\u4e0d\u8be5\u88ab\u5916\u9762\u6539&#xff0c;\u6bd4\u5982\u5706\u7684\u9762\u79ef&#xff0c;\u5b83\u662f\u534a\u5f84\u7b97\u51fa\u6765\u7684&#xff0c;\u6539\u5b83\u6ca1\u6709\u610f\u4e49\u3002<\/p>\n<p>&gt;&gt;&gt; class Circle:<br \/>\n&#8230;     def __init__(self, radius):<br \/>\n&#8230;         self.radius &#061; radius<br \/>\n&#8230;     &#064;property<br \/>\n&#8230;     def area(self):<br \/>\n&#8230;         return 3.14159 * self.radius ** 2<br \/>\n&#8230;     &#064;property<br \/>\n&#8230;     def perimeter(self):<br \/>\n&#8230;         return 2 * 3.14159 * self.radius<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; c &#061; Circle(2)<br \/>\n&gt;&gt;&gt; c.area<br \/>\n12.56636<br \/>\n&gt;&gt;&gt; c.perimeter<br \/>\n12.56636<br \/>\n&gt;&gt;&gt; c.radius &#061; 3<br \/>\n&gt;&gt;&gt; c.area<br \/>\n28.27431<br \/>\n&gt;&gt;&gt; c.area &#061; 100<br \/>\nAttributeError: property &#039;area&#039; of &#039;Circle&#039; object has no setter<\/p>\n<p>\u6709\u610f\u601d\u7684\u662f\u5468\u957f\u548c\u9762\u79ef\u5728\u534a\u5f84\u4e3a 2 \u7684\u65f6\u5019\u6570\u503c\u4e00\u6837&#xff08;12.56636&#xff09;&#xff0c;\u8fd9\u4e0d\u662f\u7b14\u8bef&#xff0c;2\u03c0r \u548c \u03c0r\u00b2 \u5728 r&#061;2 \u65f6\u786e\u5b9e\u76f8\u7b49\u3002<\/p>\n<p>\u6539\u534a\u5f84&#xff0c;\u9762\u79ef\u8ddf\u7740\u53d8&#xff0c;\u56e0\u4e3a\u5b83\u662f\u6bcf\u6b21\u8bfb\u7684\u65f6\u5019\u73b0\u7b97\u7684\u3002\u8fd9\u79cd&#034;\u770b\u8d77\u6765\u662f\u5c5e\u6027&#xff0c;\u5176\u5b9e\u662f\u8ba1\u7b97&#034;\u7684\u5199\u6cd5\u6211\u6700\u7231\u7528&#xff0c;\u8c03\u7528\u65b9\u6839\u672c\u4e0d\u7528\u7ba1\u5b83\u80cc\u540e\u662f\u5b57\u6bb5\u8fd8\u662f\u7b97\u5f0f\u3002<\/p>\n<h4>3.5 \u4e00\u4e2a\u66f4\u65e5\u5e38\u7684\u4f8b\u5b50&#xff0c;\u7b97\u6298\u6263<\/h4>\n<p>&gt;&gt;&gt; class Goods:<br \/>\n&#8230;     def __init__(self, name, price, discount&#061;1.0):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;         self.price &#061; price<br \/>\n&#8230;         self.discount &#061; discount<br \/>\n&#8230;     &#064;property<br \/>\n&#8230;     def final_price(self):<br \/>\n&#8230;         &#034;&#034;&#034;\u6298\u540e\u4ef7&#xff0c;\u6253\u51e0\u6298\u4e58\u51e0&#034;&#034;&#034;<br \/>\n&#8230;         return round(self.price * self.discount, 2)<br \/>\n&#8230;     &#064;property<br \/>\n&#8230;     def saved(self):<br \/>\n&#8230;         &#034;&#034;&#034;\u7701\u4e86\u591a\u5c11&#034;&#034;&#034;<br \/>\n&#8230;         return round(self.price &#8211; self.final_price, 2)<br \/>\n&#8230;     def __str__(self):<br \/>\n&#8230;         return &#034;%s \u539f\u4ef7%.2f \u6298\u540e%.2f \u7701%.2f&#034; % (<br \/>\n&#8230;             self.name, self.price, self.final_price, self.saved)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; g &#061; Goods(&#034;\u673a\u68b0\u952e\u76d8&#034;, 299, 0.8)<br \/>\n&gt;&gt;&gt; g.final_price<br \/>\n239.2<br \/>\n&gt;&gt;&gt; g.saved<br \/>\n59.8<br \/>\n&gt;&gt;&gt; print(g)<br \/>\n\u673a\u68b0\u952e\u76d8 \u539f\u4ef7299.00 \u6298\u540e239.20 \u770159.80<br \/>\n&gt;&gt;&gt; g.discount &#061; 0.5<br \/>\n&gt;&gt;&gt; print(g)<br \/>\n\u673a\u68b0\u952e\u76d8 \u539f\u4ef7299.00 \u6298\u540e149.50 \u7701149.50<\/p>\n<p>\u6298\u6263\u4e00\u6539&#xff0c;\u6298\u540e\u4ef7\u548c\u7701\u7684\u94b1\u81ea\u52a8\u8ddf\u7740\u53d8\u3002\u8981\u662f\u628a final_price \u5199\u6210\u666e\u901a\u5c5e\u6027&#xff0c;\u6539\u5b8c\u6298\u6263\u8fd8\u5f97\u8bb0\u5f97\u624b\u52a8\u91cd\u7b97\u4e00\u6b21&#xff0c;\u6539\u7684\u5730\u65b9\u4e00\u591a\u8fdf\u65e9\u6f0f\u3002<\/p>\n<h4>3.6 \u8001\u4ee3\u7801\u91cc\u7684\u53e6\u4e00\u79cd\u5199\u6cd5&#xff0c;\u8ba4\u5f97\u5c31\u884c<\/h4>\n<p>&#064;property \u662f\u88c5\u9970\u5668\u8bed\u6cd5&#xff0c;\u5e95\u5c42\u5176\u5b9e\u662f\u4e2a\u5185\u7f6e\u51fd\u6570 property()\u3002\u65e9\u5e74\u7684\u4ee3\u7801\u5e38\u8fd9\u4e48\u5199\u3002<\/p>\n<p>&gt;&gt;&gt; class Celsius:<br \/>\n&#8230;     def __init__(self, temperature&#061;0):<br \/>\n&#8230;         self.temperature &#061; temperature<br \/>\n&#8230;     def get_temperature(self):<br \/>\n&#8230;         return self._temperature<br \/>\n&#8230;     def set_temperature(self, value):<br \/>\n&#8230;         if value &lt; -273.15:<br \/>\n&#8230;             raise ValueError(&#034;\u6e29\u5ea6\u4e0d\u80fd\u4f4e\u4e8e\u7edd\u5bf9\u96f6\u5ea6&#034;)<br \/>\n&#8230;         self._temperature &#061; value<br \/>\n&#8230;     temperature &#061; property(get_temperature, set_temperature)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; c &#061; Celsius(37)<br \/>\n&gt;&gt;&gt; c.temperature<br \/>\n37<br \/>\n&gt;&gt;&gt; c.temperature &#061; -300<br \/>\nValueError: \u6e29\u5ea6\u4e0d\u80fd\u4f4e\u4e8e\u7edd\u5bf9\u96f6\u5ea6<\/p>\n<p>property(\u8bfb, \u5199, \u5220, \u6587\u6863) \u56db\u4e2a\u53c2\u6570&#xff0c;\u8ddf\u4e09\u4e2a\u88c5\u9970\u5668\u4e00\u4e00\u5bf9\u5e94\u3002\u73b0\u5728\u5199\u65b0\u4ee3\u7801\u7528\u88c5\u9970\u5668\u5c31\u597d&#xff0c;\u770b\u5230\u8fd9\u79cd\u8001\u5199\u6cd5\u80fd\u8ba4\u51fa\u6765\u5c31\u884c\u3002<\/p>\n<h4>3.7 \u4ec0\u4e48\u65f6\u5019\u8be5\u7528 property<\/h4>\n<p>\u6211\u7528 property \u7684\u6807\u51c6\u5c31\u56db\u6761&#xff0c;\u547d\u4e2d\u4e00\u6761\u5c31\u503c\u5f97\u4e0a\u3002<\/p>\n<table>\n<tr>\u60c5\u51b5\u4f8b\u5b50<\/tr>\n<tbody>\n<tr>\n<td>\u503c\u9700\u8981\u6821\u9a8c<\/td>\n<td>\u5206\u6570 0 \u5230 100\u3001\u6e29\u5ea6\u4e0d\u4f4e\u4e8e\u7edd\u5bf9\u96f6\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>\u503c\u662f\u7b97\u51fa\u6765\u7684<\/td>\n<td>\u9762\u79ef\u3001\u6298\u540e\u4ef7\u3001\u5e74\u9f84&#xff08;\u4ece\u751f\u65e5\u7b97&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u60f3\u8ba9\u5b83\u53ea\u8bfb<\/td>\n<td>\u8ba2\u5355\u53f7\u3001\u521b\u5efa\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td>\u8001\u63a5\u53e3\u8981\u52a0\u6821\u9a8c\u53c8\u4e0d\u60f3\u6539\u8c03\u7528\u65b9<\/td>\n<td>\u88f8\u5c5e\u6027\u5347\u7ea7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u53cd\u8fc7\u6765\u7684\u60c5\u51b5\u4e5f\u4e00\u6837\u6e05\u695a&#xff0c;\u5982\u679c\u4e00\u4e2a\u503c\u5c31\u662f\u5355\u7eaf\u5b58\u7740&#xff0c;\u6ca1\u6709\u4efb\u4f55\u9650\u5236&#xff0c;\u90a3\u76f4\u63a5\u5199 self.name &#061; name \u5c31\u597d&#xff0c;\u5916\u9762 obj.name \u76f4\u63a5\u8bfb\u5199&#xff0c;\u522b\u4e3a\u4e86&#034;\u89c4\u8303&#034;\u628a\u6bcf\u4e2a\u5c5e\u6027\u90fd\u5305\u4e00\u5c42 property\u3002\u6211\u89c1\u8fc7\u6709\u4eba\u7ed9\u5341\u51e0\u4e8c\u5341\u4e2a\u5b57\u6bb5\u5168\u5199\u4e86 getter \u548c setter&#xff0c;\u91cc\u9762\u5c31\u4e00\u53e5 return self._x&#xff0c;\u7eaf\u5c5e\u7ed9\u81ea\u5df1\u627e\u4e8b\u3002<\/p>\n<p>Python \u7684\u597d\u5904\u5728\u4e8e&#xff0c;\u4f60\u4eca\u5929\u5199\u6210\u88f8\u5c5e\u6027&#xff0c;\u54ea\u5929\u771f\u9700\u8981\u6821\u9a8c\u4e86&#xff0c;\u52a0\u4e0a &#064;property \u5c31\u5b8c\u4e8b&#xff0c;\u8c03\u7528\u65b9\u6beb\u53d1\u65e0\u4f24\u3002\u522b\u7684\u8bed\u8a00\u6ca1\u8fd9\u4e2a\u540e\u6094\u836f&#xff0c;\u6240\u4ee5\u624d\u8981\u4e00\u5f00\u59cb\u5c31\u5168\u5199\u6210 get\/set\u3002<\/p>\n<h4>3.8 \u5199 property \u6700\u5bb9\u6613\u72af\u7684\u9519&#xff0c;\u65e0\u9650\u9012\u5f52<\/h4>\n<p>\u8fd9\u4e2a\u5751\u6211\u4ece\u7b2c\u4e00\u6b21\u5199 property \u5c31\u8e29\u4e86&#xff0c;\u800c\u4e14\u62a5\u9519\u4fe1\u606f\u957f\u5230\u8ba9\u4eba\u53d1\u61f5\u3002<\/p>\n<p>&gt;&gt;&gt; class Bad:<br \/>\n&#8230;     &#064;property<br \/>\n&#8230;     def x(self):<br \/>\n&#8230;         return self._x<br \/>\n&#8230;     &#064;x.setter<br \/>\n&#8230;     def x(self, value):<br \/>\n&#8230;         self.x &#061; value<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; b &#061; Bad()<br \/>\n&gt;&gt;&gt; b.x &#061; 1<br \/>\nRecursionError: maximum recursion depth exceeded<\/p>\n<p>\u770b setter \u90a3\u4e00\u884c&#xff0c;self.x &#061; value\u3002\u53ef x \u5df2\u7ecf\u662f property \u4e86&#xff0c;\u7ed9 self.x \u8d4b\u503c\u5c31\u7b49\u4e8e\u53c8\u8c03\u4e86\u4e00\u6b21\u8fd9\u4e2a setter&#xff0c;\u7136\u540e\u5b83\u53c8\u7ed9 self.x \u8d4b\u503c&#xff0c;\u65e0\u9650\u5faa\u73af&#xff0c;\u76f4\u5230 Python \u7684\u9012\u5f52\u4e0a\u9650\u628a\u5b83\u6390\u65ad\u3002<\/p>\n<p>b.x &#061; 1<br \/>\n   \u2502<br \/>\n   \u2514\u2500\u25ba x.setter(self, 1)<br \/>\n           \u2502<br \/>\n           \u2514\u2500\u25ba self.x &#061; 1   \u2190 \u53c8\u662f\u8d4b\u503c&#xff0c;\u53c8\u8c03 setter<br \/>\n                   \u2502<br \/>\n                   \u2514\u2500\u25ba x.setter(self, 1)  \u2192 \u2026\u2026\u65e0\u6b62\u5883<\/p>\n<p>\u4fee\u6cd5\u4e00\u53e5\u8bdd&#xff0c;setter \u91cc\u5fc5\u987b\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2a\u540d\u5b57&#xff0c;\u901a\u5e38\u662f\u52a0\u4e0b\u5212\u7ebf\u7684\u90a3\u4e2a\u3002<\/p>\n<p>&gt;&gt;&gt; class Good:<br \/>\n&#8230;     &#064;property<br \/>\n&#8230;     def x(self):<br \/>\n&#8230;         return self._x<br \/>\n&#8230;     &#064;x.setter<br \/>\n&#8230;     def x(self, value):<br \/>\n&#8230;         self._x &#061; value<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; g &#061; Good()<br \/>\n&gt;&gt;&gt; g.x &#061; 1<br \/>\n&gt;&gt;&gt; g.x<br \/>\n1<br \/>\n&gt;&gt;&gt; g.__dict__<br \/>\n{&#039;_x&#039;: 1}<\/p>\n<p>\u8bb0\u4f4f\u4e00\u6761\u94c1\u5f8b&#xff0c;&#064;property \u66b4\u9732\u51fa\u6765\u7684\u5c5e\u6027\u540d&#xff0c;\u548c\u5185\u90e8\u771f\u6b63\u5b58\u503c\u7684\u53d8\u91cf\u540d&#xff0c;\u7edd\u5bf9\u4e0d\u80fd\u662f\u540c\u4e00\u4e2a\u3002\u4e00\u65e6\u540c\u540d&#xff0c;getter \u6216 setter \u5c31\u4f1a\u8c03\u7528\u5b83\u81ea\u5df1\u3002<\/p>\n<p>\u518d\u8865\u4e00\u53e5&#xff0c;\u5982\u679c\u4f60\u5728\u8c03\u8bd5\u65f6\u770b\u5230 RecursionError&#xff0c;\u7b2c\u4e00\u4ef6\u4e8b\u5c31\u53bb\u67e5 property \u7684 getter \u548c setter \u91cc\u6709\u6ca1\u6709\u5199\u9519\u53d8\u91cf\u540d\u3002<\/p>\n<hr \/>\n<h3>4. \u7ee7\u627f<\/h3>\n<h4>4.1 \u5148\u770b\u6ca1\u6709\u7ee7\u627f\u7684\u65f6\u5019\u6709\u591a\u5570\u55e6<\/h4>\n<p>\u8981\u5199\u4e00\u4e2a\u732b\u7c7b\u548c\u4e00\u4e2a\u72d7\u7c7b&#xff0c;\u5b83\u4eec\u90fd\u6709\u540d\u5b57\u3001\u90fd\u4f1a\u7761\u3001\u90fd\u4f1a\u5403&#xff0c;\u53ea\u6709\u53eb\u58f0\u4e0d\u4e00\u6837\u3002<\/p>\n<p>&gt;&gt;&gt; class Dog:<br \/>\n&#8230;     def __init__(self, name):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;     def sleep(self):<br \/>\n&#8230;         print(self.name, &#034;\u8db4\u7740\u7761&#034;)<br \/>\n&#8230;     def eat(self, food):<br \/>\n&#8230;         print(self.name, &#034;\u5403&#034;, food)<br \/>\n&#8230;     def speak(self):<br \/>\n&#8230;         return &#034;\u6c6a\u6c6a&#034;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class Cat:<br \/>\n&#8230;     def __init__(self, name):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;     def sleep(self):<br \/>\n&#8230;         print(self.name, &#034;\u8db4\u7740\u7761&#034;)<br \/>\n&#8230;     def eat(self, food):<br \/>\n&#8230;         print(self.name, &#034;\u5403&#034;, food)<br \/>\n&#8230;     def speak(self):<br \/>\n&#8230;         return &#034;\u55b5\u55b5&#034;<br \/>\n&#8230;<\/p>\n<p>__init__\u3001sleep\u3001eat \u4e09\u4e2a\u65b9\u6cd5\u4e00\u6a21\u4e00\u6837&#xff0c;\u6284\u4e86\u4e24\u904d\u3002\u7b49\u8981\u52a0\u4e2a\u9e1f\u7c7b&#xff0c;\u518d\u6284\u7b2c\u4e09\u904d\u3002\u7b49\u8981\u6539 sleep \u7684\u903b\u8f91&#xff0c;\u4e09\u4e2a\u7c7b\u6328\u4e2a\u6539\u3002<\/p>\n<p>\u7ee7\u627f\u5c31\u662f\u628a\u91cd\u590d\u7684\u90e8\u5206\u5411\u4e0a\u62bd\u3002<\/p>\n<p>        Animal                \u2190 \u7236\u7c7b&#xff0c;\u653e\u5171\u540c\u7684\u90e8\u5206<br \/>\n        name \/ sleep \/ eat<br \/>\n             \u2502<br \/>\n      \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n      \u25bc      \u25bc      \u25bc<br \/>\n    Dog     Cat    Duck       \u2190 \u5b50\u7c7b&#xff0c;\u53ea\u5199\u81ea\u5df1\u4e0d\u4e00\u6837\u7684<br \/>\n    speak   speak  speak<\/p>\n<h4>4.2 \u7ee7\u627f\u7684\u5199\u6cd5&#xff0c;\u62ec\u53f7\u91cc\u5199\u7236\u7c7b<\/h4>\n<p>&gt;&gt;&gt; class Animal:<br \/>\n&#8230;     def __init__(self, name):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;     def sleep(self):<br \/>\n&#8230;         print(self.name, &#034;\u8db4\u7740\u7761&#034;)<br \/>\n&#8230;     def eat(self, food):<br \/>\n&#8230;         print(self.name, &#034;\u5403&#034;, food)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class Dog(Animal):<br \/>\n&#8230;     pass<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; d &#061; Dog(&#034;\u5c0f\u9ed1&#034;)<br \/>\n&gt;&gt;&gt; d.name<br \/>\n&#039;\u5c0f\u9ed1&#039;<br \/>\n&gt;&gt;&gt; d.sleep()<br \/>\n\u5c0f\u9ed1 \u8db4\u7740\u7761<br \/>\n&gt;&gt;&gt; d.eat(&#034;\u9aa8\u5934&#034;)<br \/>\n\u5c0f\u9ed1 \u5403 \u9aa8\u5934<\/p>\n<p>class Dog(Animal) \u8fd9\u4e00\u53e5&#xff0c;Dog \u5c31\u628a Animal \u91cc\u7684\u4e1c\u897f\u5168\u62ff\u4e86\u8fc7\u6765\u3002pass \u8868\u793a\u6682\u65f6\u4ec0\u4e48\u90fd\u4e0d\u52a0&#xff0c;\u4e00\u4e2a\u65b9\u6cd5\u6ca1\u5199&#xff0c;\u5374\u5df2\u7ecf\u80fd\u7528\u7236\u7c7b\u7684\u4e09\u4e2a\u65b9\u6cd5\u3002<\/p>\n<p>\u67e5\u4e00\u4e0b\u7ee7\u627f\u5173\u7cfb\u3002<\/p>\n<p>&gt;&gt;&gt; Dog.__bases__<br \/>\n(&lt;class &#039;__main__.Animal&#039;&gt;,)<br \/>\n&gt;&gt;&gt; Animal.__bases__<br \/>\n(&lt;class &#039;object&#039;&gt;,)<\/p>\n<p>\u524d\u9762\u90a3\u4e2a __main__ \u662f\u6a21\u5757\u540d&#xff0c;\u5728\u4ea4\u4e92\u7a97\u53e3\u91cc\u6572\u4ee3\u7801\u5c31\u5c5e\u4e8e __main__ \u6a21\u5757&#xff0c;\u5199\u8fdb .py \u6587\u4ef6\u518d\u8dd1&#xff0c;\u663e\u793a\u7684\u4f1a\u662f\u6587\u4ef6\u540d\u3002<\/p>\n<p>\u6700\u540e\u90a3\u884c\u503c\u5f97\u7559\u610f&#xff0c;\u6211\u4eec\u81ea\u5df1\u5199\u7684 Animal \u4e5f\u662f\u6709\u7236\u7c7b\u7684&#xff0c;\u5b83\u7684\u7236\u7c7b\u53eb object\u3002Python \u91cc\u6240\u6709\u7c7b\u6700\u7ec8\u90fd\u7ee7\u627f\u5230 object&#xff0c;\u5b83\u662f\u8001\u7956\u5b97\u3002<\/p>\n<h4>4.3 \u5b50\u7c7b\u52a0\u81ea\u5df1\u7684\u4e1c\u897f<\/h4>\n<p>\u5728\u5b50\u7c7b\u91cc\u5199\u65b0\u65b9\u6cd5&#xff0c;\u5c5e\u4e8e\u5b83\u72ec\u6709\u3002<\/p>\n<p>&gt;&gt;&gt; class Dog(Animal):<br \/>\n&#8230;     def speak(self):<br \/>\n&#8230;         return self.name &#043; &#034; \u6c6a\u6c6a&#034;<br \/>\n&#8230;     def fetch(self):<br \/>\n&#8230;         return self.name &#043; &#034; \u628a\u7403\u53fc\u56de\u6765\u4e86&#034;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; d &#061; Dog(&#034;\u5c0f\u9ed1&#034;)<br \/>\n&gt;&gt;&gt; d.speak()<br \/>\n&#039;\u5c0f\u9ed1 \u6c6a\u6c6a&#039;<br \/>\n&gt;&gt;&gt; d.fetch()<br \/>\n&#039;\u5c0f\u9ed1 \u628a\u7403\u53fc\u56de\u6765\u4e86&#039;<br \/>\n&gt;&gt;&gt; d.sleep()<br \/>\n\u5c0f\u9ed1 \u8db4\u7740\u7761<\/p>\n<p>\u81ea\u5df1\u7684\u65b9\u6cd5\u548c\u7ee7\u627f\u6765\u7684\u65b9\u6cd5\u6df7\u7740\u7528&#xff0c;\u8c03\u7528\u7684\u65f6\u5019\u770b\u4e0d\u51fa\u6765\u54ea\u4e2a\u662f\u54ea\u5c42\u7684&#xff0c;\u8fd9\u6b63\u662f\u7ee7\u627f\u60f3\u8981\u7684\u900f\u660e\u611f\u3002<\/p>\n<h4>4.4 \u65b9\u6cd5\u91cd\u5199&#xff0c;\u5b50\u7c7b\u8bf4\u4e86\u7b97<\/h4>\n<p>\u7236\u7c7b\u7684\u65b9\u6cd5\u4e0d\u5408\u7528&#xff0c;\u5b50\u7c7b\u53ef\u4ee5\u7167\u7740\u540d\u5b57\u91cd\u5199\u4e00\u4e2a\u3002<\/p>\n<p>&gt;&gt;&gt; class Cat(Animal):<br \/>\n&#8230;     def sleep(self):<br \/>\n&#8230;         print(self.name, &#034;\u8737\u5728\u732b\u7a9d\u91cc\u7761&#034;)<br \/>\n&#8230;     def speak(self):<br \/>\n&#8230;         return self.name &#043; &#034; \u55b5\u55b5&#034;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; c &#061; Cat(&#034;\u5c0f\u767d&#034;)<br \/>\n&gt;&gt;&gt; c.sleep()<br \/>\n\u5c0f\u767d \u8737\u5728\u732b\u7a9d\u91cc\u7761<br \/>\n&gt;&gt;&gt; d &#061; Dog(&#034;\u5c0f\u9ed1&#034;)<br \/>\n&gt;&gt;&gt; d.sleep()<br \/>\n\u5c0f\u9ed1 \u8db4\u7740\u7761<\/p>\n<p>\u540c\u6837\u662f sleep()&#xff0c;\u732b\u548c\u72d7\u7684\u8868\u73b0\u4e0d\u4e00\u6837\u3002\u67e5\u627e\u7684\u987a\u5e8f\u8ddf\u5c5e\u6027\u4e00\u6837&#xff0c;\u5148\u5728\u5b50\u7c7b\u627e&#xff0c;\u627e\u4e0d\u5230\u624d\u53bb\u7236\u7c7b\u3002<\/p>\n<p>c.sleep()<br \/>\n   \u2502<br \/>\n   \u251c\u2500 \u67e5 Cat \u91cc\u6709\u6ca1\u6709 sleep \u2192 \u6709&#xff0c;\u7528\u5b83<br \/>\n   \u2502<br \/>\n   \u2514\u2500&#xff08;\u5982\u679c\u6ca1\u6709&#xff09;\u518d\u67e5 Animal<\/p>\n<h4>4.5 super()&#xff0c;\u628a\u7236\u7c7b\u7684\u65b9\u6cd5\u53eb\u56de\u6765<\/h4>\n<p>\u91cd\u5199\u7684\u65f6\u5019\u7ecf\u5e38\u9047\u5230\u7684\u60c5\u51b5\u662f&#xff0c;\u7236\u7c7b\u90a3\u4e2a\u65b9\u6cd5\u5927\u90e8\u5206\u8fd8\u8981\u7528&#xff0c;\u53ea\u662f\u60f3\u524d\u540e\u52a0\u4e24\u53e5\u3002\u8fd9\u65f6\u5019\u522b\u628a\u4ee3\u7801\u6284\u4e00\u904d&#xff0c;\u7528 super()\u3002<\/p>\n<p>&gt;&gt;&gt; class Dog(Animal):<br \/>\n&#8230;     def sleep(self):<br \/>\n&#8230;         print(&#034;\u5148\u8f6c\u4e09\u5708&#034;)<br \/>\n&#8230;         super().sleep()<br \/>\n&#8230;         print(&#034;\u7761\u7740\u4e86&#034;)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; d &#061; Dog(&#034;\u5c0f\u9ed1&#034;)<br \/>\n&gt;&gt;&gt; d.sleep()<br \/>\n\u5148\u8f6c\u4e09\u5708<br \/>\n\u5c0f\u9ed1 \u8db4\u7740\u7761<br \/>\n\u7761\u7740\u4e86<\/p>\n<p>super().sleep() \u5c31\u662f&#034;\u8c03\u7528\u7236\u7c7b\u7684 sleep&#034;\u3002\u5b83\u6267\u884c\u5b8c\u4f1a\u7ee7\u7eed\u6267\u884c\u540e\u9762\u7684\u8bed\u53e5&#xff0c;\u6240\u4ee5\u987a\u5e8f\u662f\u524d\u3001\u4e2d\u3001\u540e\u3002<\/p>\n<p>\u5728 __init__ \u91cc\u7528\u5f97\u6700\u591a\u3002\u7236\u7c7b\u5df2\u7ecf\u628a\u901a\u7528\u5b57\u6bb5\u7ed1\u597d\u4e86&#xff0c;\u5b50\u7c7b\u53ea\u9700\u8981\u7ed1\u81ea\u5df1\u591a\u51fa\u6765\u7684\u90a3\u51e0\u4e2a\u3002<\/p>\n<p>&gt;&gt;&gt; class Employee:<br \/>\n&#8230;     def __init__(self, name, salary):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;         self.salary &#061; salary<br \/>\n&#8230;     def info(self):<br \/>\n&#8230;         return &#034;%s \u6708\u85aa %d&#034; % (self.name, self.salary)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class Manager(Employee):<br \/>\n&#8230;     def __init__(self, name, salary, bonus):<br \/>\n&#8230;         super().__init__(name, salary)<br \/>\n&#8230;         self.bonus &#061; bonus<br \/>\n&#8230;     def info(self):<br \/>\n&#8230;         return super().info() &#043; &#034; \u52a0\u5956\u91d1 %d&#xff0c;\u5171 %d&#034; % (<br \/>\n&#8230;             self.bonus, self.salary &#043; self.bonus)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; m &#061; Manager(&#034;\u5f20\u4e09&#034;, 20000, 5000)<br \/>\n&gt;&gt;&gt; print(m.info())<br \/>\n\u5f20\u4e09 \u6708\u85aa 20000 \u52a0\u5956\u91d1 5000&#xff0c;\u5171 25000<\/p>\n<p>super().__init__(name, salary) \u4e00\u884c\u9876\u66ff\u4e86\u4e24\u6761\u8d4b\u503c&#xff0c;\u800c\u4e14\u5c06\u6765\u7236\u7c7b\u52a0\u4e86\u65b0\u5b57\u6bb5&#xff0c;\u5b50\u7c7b\u4e0d\u7528\u52a8\u3002<\/p>\n<p>\u8981\u662f\u5fd8\u4e86\u5199 super&#xff0c;\u4f1a\u8fd9\u6837\u3002<\/p>\n<p>&gt;&gt;&gt; class BadManager(Employee):<br \/>\n&#8230;     def __init__(self, name, salary, bonus):<br \/>\n&#8230;         self.bonus &#061; bonus<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; bm &#061; BadManager(&#034;\u674e\u56db&#034;, 10000, 2000)<br \/>\n&gt;&gt;&gt; bm.bonus<br \/>\n2000<br \/>\n&gt;&gt;&gt; bm.name<br \/>\nAttributeError: &#039;BadManager&#039; object has no attribute &#039;name&#039;<\/p>\n<p>\u5b50\u7c7b\u5b9a\u4e49\u4e86 __init__&#xff0c;\u5c31\u628a\u7236\u7c7b\u7684\u5b8c\u5168\u76d6\u4f4f\u4e86&#xff0c;\u7236\u7c7b\u7684\u521d\u59cb\u5316\u4e00\u53e5\u6ca1\u6267\u884c&#xff0c;name \u548c salary \u6839\u672c\u6ca1\u7ed1\u4e0a\u3002\u8fd9\u4e2a\u62a5\u9519\u5728\u65b0\u624b\u5199\u7ee7\u627f\u65f6\u7279\u522b\u5e38\u89c1&#xff0c;\u53ea\u8981\u5b50\u7c7b\u5199\u4e86 __init__&#xff0c;\u7b2c\u4e00\u4ef6\u4e8b\u5c31\u662f super().__init__()\u3002<\/p>\n<p>super() \u8fd8\u6709\u4e2a\u8001\u5199\u6cd5&#xff0c;\u628a\u7236\u7c7b\u540d\u5199\u51fa\u6765\u3002<\/p>\n<p>&gt;&gt;&gt; class OldStyle(Employee):<br \/>\n&#8230;     def __init__(self, name, salary, bonus):<br \/>\n&#8230;         Employee.__init__(self, name, salary)<br \/>\n&#8230;         self.bonus &#061; bonus<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; o &#061; OldStyle(&#034;\u738b\u4e94&#034;, 8000, 1000)<br \/>\n&gt;&gt;&gt; print(o.info())<br \/>\n\u738b\u4e94 \u6708\u85aa 8000<\/p>\n<p>\u80fd\u7528&#xff0c;\u4f46\u7236\u7c7b\u540d\u5199\u6b7b\u5728\u91cc\u9762&#xff0c;\u6539\u4e2a\u7ee7\u627f\u5173\u7cfb\u5c31\u5f97\u8ddf\u7740\u6539\u3002\u65b0\u4ee3\u7801\u4e00\u5f8b\u7528 super()\u3002<\/p>\n<h4>4.6 isinstance \u548c issubclass&#xff0c;\u5224\u65ad\u5173\u7cfb\u7528<\/h4>\n<p>\u60f3\u77e5\u9053\u4e00\u4e2a\u5bf9\u8c61\u662f\u4e0d\u662f\u67d0\u4e2a\u7c7b\u7684&#xff0c;\u7528 isinstance\u3002<\/p>\n<p>&gt;&gt;&gt; isinstance(d, Dog)<br \/>\nTrue<br \/>\n&gt;&gt;&gt; isinstance(d, Animal)<br \/>\nTrue<br \/>\n&gt;&gt;&gt; isinstance(d, object)<br \/>\nTrue<br \/>\n&gt;&gt;&gt; isinstance(d, Cat)<br \/>\nFalse<\/p>\n<p>\u72d7\u662f\u72d7&#xff0c;\u72d7\u4e5f\u662f\u52a8\u7269&#xff0c;\u72d7\u5f53\u7136\u662f object\u3002<\/p>\n<p>\u5224\u65ad\u7c7b\u4e4b\u95f4\u7684\u5173\u7cfb\u7528 issubclass\u3002<\/p>\n<p>&gt;&gt;&gt; issubclass(Dog, Animal)<br \/>\nTrue<br \/>\n&gt;&gt;&gt; issubclass(Animal, Dog)<br \/>\nFalse<br \/>\n&gt;&gt;&gt; issubclass(Dog, Dog)<br \/>\nTrue<\/p>\n<p>\u6709\u4e2a\u5bb9\u6613\u6df7\u7684\u5730\u65b9&#xff0c;isinstance \u4f1a\u628a\u7ee7\u627f\u5173\u7cfb\u7b97\u8fdb\u53bb&#xff0c;\u800c type(x) is SomeClass \u4e0d\u4f1a\u3002<\/p>\n<p>&gt;&gt;&gt; type(d) is Dog<br \/>\nTrue<br \/>\n&gt;&gt;&gt; type(d) is Animal<br \/>\nFalse<br \/>\n&gt;&gt;&gt; type(d) is type(d)<br \/>\nTrue<\/p>\n<p>\u60f3\u5224\u65ad&#034;\u5b83\u5230\u5e95\u662f\u54ea\u4e2a\u7c7b\u9020\u51fa\u6765\u7684&#034;\u7528 type&#xff0c;\u60f3\u5224\u65ad&#034;\u5b83\u80fd\u4e0d\u80fd\u5f53\u52a8\u7269\u4f7f&#034;\u7528 isinstance\u3002\u65e5\u5e38\u4e1a\u52a1\u4ee3\u7801\u91cc\u540e\u8005\u7528\u5f97\u591a&#xff0c;\u56e0\u4e3a\u5b83\u5bf9\u7ee7\u627f\u53cb\u597d\u3002<\/p>\n<h4>4.7 \u591a\u7ee7\u627f&#xff0c;\u4e00\u4e2a\u7c7b\u80fd\u8ba4\u597d\u51e0\u4e2a\u7239<\/h4>\n<p>Python \u5141\u8bb8\u4e00\u4e2a\u7c7b\u540c\u65f6\u7ee7\u627f\u591a\u4e2a\u7236\u7c7b\u3002<\/p>\n<p>&gt;&gt;&gt; class Flyable:<br \/>\n&#8230;     def fly(self):<br \/>\n&#8230;         return &#034;\u6211\u80fd\u98de&#034;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class Swimmable:<br \/>\n&#8230;     def swim(self):<br \/>\n&#8230;         return &#034;\u6211\u80fd\u6e38&#034;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class Duck(Animal, Flyable, Swimmable):<br \/>\n&#8230;     def speak(self):<br \/>\n&#8230;         return self.name &#043; &#034; \u560e\u560e&#034;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; duck &#061; Duck(&#034;\u5510\u8001\u9e2d&#034;)<br \/>\n&gt;&gt;&gt; duck.speak()<br \/>\n&#039;\u5510\u8001\u9e2d \u560e\u560e&#039;<br \/>\n&gt;&gt;&gt; duck.fly()<br \/>\n&#039;\u6211\u80fd\u98de&#039;<br \/>\n&gt;&gt;&gt; duck.swim()<br \/>\n&#039;\u6211\u80fd\u6e38&#039;<br \/>\n&gt;&gt;&gt; duck.sleep()<br \/>\n\u5510\u8001\u9e2d \u8db4\u7740\u7761<\/p>\n<p>\u4e09\u4efd\u80fd\u529b\u62fc\u4e00\u8d77\u3002\u8fd9\u79cd\u628a\u4e00\u4e2a\u4e2a\u5c0f\u80fd\u529b\u62fc\u8fdb\u6765\u7684\u5199\u6cd5\u53eb Mixin&#xff0c;\u5b9e\u9645\u9879\u76ee\u91cc\u5f88\u5e38\u89c1\u3002<\/p>\n<p>\u9ebb\u70e6\u51fa\u5728\u591a\u4e2a\u7236\u7c7b\u6709\u540c\u540d\u65b9\u6cd5\u7684\u65f6\u5019&#xff0c;\u5230\u5e95\u542c\u8c01\u7684\u3002Python \u7528\u4e00\u5957\u53eb MRO \u7684\u89c4\u5219\u6765\u6392\u987a\u5e8f\u3002<\/p>\n<p>            A<br \/>\n           \/ \\\\<br \/>\n          B   C<br \/>\n           \\\\ \/<br \/>\n            D          \u2190 D \u540c\u65f6\u7ee7\u627f B \u548c C&#xff0c;B \u548c C \u53c8\u90fd\u7ee7\u627f A<\/p>\n<p>\u8fd9\u79cd\u7ed3\u6784\u53eb\u83f1\u5f62\u7ee7\u627f&#xff0c;\u662f MRO \u5b58\u5728\u7684\u7406\u7531\u3002\u770b\u4e2a\u4f8b\u5b50\u3002<\/p>\n<p>&gt;&gt;&gt; class A:<br \/>\n&#8230;     def hi(self):<br \/>\n&#8230;         print(&#034;A&#034;)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class B(A):<br \/>\n&#8230;     def hi(self):<br \/>\n&#8230;         print(&#034;B&#034;)<br \/>\n&#8230;         super().hi()<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class C(A):<br \/>\n&#8230;     def hi(self):<br \/>\n&#8230;         print(&#034;C&#034;)<br \/>\n&#8230;         super().hi()<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class D(B, C):<br \/>\n&#8230;     def hi(self):<br \/>\n&#8230;         print(&#034;D&#034;)<br \/>\n&#8230;         super().hi()<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; d &#061; D()<br \/>\n&gt;&gt;&gt; d.hi()<br \/>\nD<br \/>\nB<br \/>\nC<br \/>\nA<\/p>\n<p>\u6253\u5370\u987a\u5e8f D \u2192 B \u2192 C \u2192 A\u3002\u8fd9\u4e2a\u987a\u5e8f\u4e0d\u662f\u62cd\u8111\u888b\u5b9a\u7684&#xff0c;\u5c31\u662f MRO\u3002<\/p>\n<p>&gt;&gt;&gt; D.__mro__<br \/>\n(&lt;class &#039;__main__.D&#039;&gt;, &lt;class &#039;__main__.B&#039;&gt;, &lt;class &#039;__main__.C&#039;&gt;, &lt;class &#039;__main__.A&#039;&gt;, &lt;class &#039;object&#039;&gt;)<\/p>\n<p>__mro__ \u628a\u8fd9\u4e2a\u7c7b\u7684\u67e5\u627e\u987a\u5e8f\u644a\u7ed9\u4f60\u770b\u3002\u6ce8\u610f A \u53ea\u51fa\u73b0\u4e86\u4e00\u6b21&#xff0c;\u800c\u4e14\u6392\u5728 B \u548c C \u540e\u9762&#xff0c;\u4e0d\u4f1a\u51fa\u73b0 A \u7684 hi \u88ab\u8c03\u7528\u4e24\u904d\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u6bcf\u4e2a\u65b9\u6cd5\u91cc\u90fd\u5199\u4e86 super().hi()&#xff0c;\u8fd9\u5c31\u662f\u534f\u4f5c\u5f0f\u8c03\u7528&#xff0c;\u5b83\u8ba9\u540c\u4e00\u6761\u94fe\u4e0a\u7684\u6bcf\u4e2a\u7c7b\u90fd\u6709\u673a\u4f1a\u6267\u884c\u4e00\u6b21\u3002\u5c11\u5199\u4efb\u4f55\u4e00\u4e2a super()&#xff0c;\u94fe\u6761\u5c31\u65ad\u5728\u90a3\u91cc\u3002<\/p>\n<p>\u591a\u7ee7\u627f\u7528\u5f97\u597d\u662f\u5229\u5668&#xff0c;\u7528\u4e0d\u597d\u662f\u707e\u96be\u3002\u6211\u7ed9\u81ea\u5df1\u5b9a\u7684\u89c4\u77e9\u662f&#xff0c;\u7ee7\u627f\u5c42\u7ea7\u522b\u8d85\u8fc7\u4e24\u5c42&#xff0c;\u80fd\u7528\u7ec4\u5408\u5c31\u522b\u7528\u591a\u7ee7\u627f\u3002\u771f\u8981\u7528&#xff0c;\u7236\u7c7b\u6700\u597d\u90fd\u662f\u90a3\u79cd\u53ea\u6709\u4e00\u4e24\u4e2a\u65b9\u6cd5\u7684\u7eaf\u80fd\u529b\u7c7b\u3002<\/p>\n<h4>4.8 \u591a\u6001&#xff0c;\u540c\u4e00\u4e2a\u52a8\u4f5c\u4e0d\u540c\u7684\u8868\u73b0<\/h4>\n<p>\u6709\u4e86\u7ee7\u627f\u548c\u65b9\u6cd5\u91cd\u5199&#xff0c;\u591a\u6001\u5c31\u81ea\u7136\u5192\u51fa\u6765\u4e86\u3002<\/p>\n<p>&gt;&gt;&gt; class Animal:<br \/>\n&#8230;     def __init__(self, name):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;     def speak(self):<br \/>\n&#8230;         return &#034;\u2026\u2026&#034;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class Dog(Animal):<br \/>\n&#8230;     def speak(self):<br \/>\n&#8230;         return self.name &#043; &#034; \u6c6a\u6c6a&#034;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class Cat(Animal):<br \/>\n&#8230;     def speak(self):<br \/>\n&#8230;         return self.name &#043; &#034; \u55b5\u55b5&#034;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class Duck(Animal):<br \/>\n&#8230;     def speak(self):<br \/>\n&#8230;         return self.name &#043; &#034; \u560e\u560e&#034;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; for a in [Dog(&#034;\u5c0f\u9ed1&#034;), Cat(&#034;\u5c0f\u767d&#034;), Duck(&#034;\u5510\u8001\u9e2d&#034;)]:<br \/>\n&#8230;     print(a.speak())<br \/>\n\u5c0f\u9ed1 \u6c6a\u6c6a<br \/>\n\u5c0f\u767d \u55b5\u55b5<br \/>\n\u5510\u8001\u9e2d \u560e\u560e<\/p>\n<p>\u5faa\u73af\u90a3\u4e00\u884c\u5b8c\u5168\u4e0d\u5173\u5fc3\u5217\u8868\u91cc\u5230\u5e95\u662f\u732b\u662f\u72d7&#xff0c;\u5b83\u53ea\u7ba1\u558a speak()&#xff0c;\u6bcf\u4e2a\u5bf9\u8c61\u81ea\u5df1\u77e5\u9053\u8be5\u600e\u4e48\u53eb\u3002\u4e0d\u7528\u5199 if isinstance(a, Dog) \u518d\u5206\u652f&#xff0c;\u52a0\u4e00\u79cd\u65b0\u52a8\u7269&#xff0c;\u8fd9\u6bb5\u5faa\u73af\u4e00\u4e2a\u5b57\u90fd\u4e0d\u7528\u6539\u3002<\/p>\n<p>Python \u5728\u8fd9\u4ef6\u4e8b\u4e0a\u6bd4\u522b\u7684\u8bed\u8a00\u66f4\u677e&#xff0c;\u5b83\u8fd8\u6709\u4e2a\u8bf4\u6cd5\u53eb\u9e2d\u5b50\u7c7b\u578b&#xff0c;\u8d70\u8d77\u6765\u50cf\u9e2d\u5b50\u3001\u53eb\u8d77\u6765\u50cf\u9e2d\u5b50&#xff0c;\u90a3\u5b83\u5c31\u662f\u9e2d\u5b50\u3002\u538b\u6839\u4e0d\u7ee7\u627f\u540c\u4e00\u4e2a\u7236\u7c7b\u4e5f\u884c\u3002<\/p>\n<p>&gt;&gt;&gt; class Robot:<br \/>\n&#8230;     def __init__(self, name):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;     def speak(self):<br \/>\n&#8230;         return self.name &#043; &#034; \u6ef4&#xff0c;\u6536\u5230&#034;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; for a in [Dog(&#034;\u5c0f\u9ed1&#034;), Cat(&#034;\u5c0f\u767d&#034;), Robot(&#034;\u94c1\u86cb&#034;)]:<br \/>\n&#8230;     print(a.speak())<br \/>\n\u5c0f\u9ed1 \u6c6a\u6c6a<br \/>\n\u5c0f\u767d \u55b5\u55b5<br \/>\n\u94c1\u86cb \u6ef4&#xff0c;\u6536\u5230<\/p>\n<p>Robot \u8ddf Animal \u4e00\u70b9\u5173\u7cfb\u6ca1\u6709&#xff0c;\u53ea\u8981\u5b83\u6709 speak \u65b9\u6cd5&#xff0c;\u5c31\u80fd\u8fdb\u8fd9\u4e2a\u5faa\u73af\u3002\u8fd9\u79cd&#034;\u6309\u80fd\u529b\u800c\u4e0d\u662f\u6309\u8840\u7edf&#034;\u7684\u9ed8\u5951&#xff0c;\u662f Python \u4ee3\u7801\u5199\u8d77\u6765\u677e\u5feb\u7684\u539f\u56e0\u4e4b\u4e00\u3002<\/p>\n<h4>4.9 \u79c1\u6709\u5c5e\u6027\u548c\u7ee7\u627f&#xff0c;\u4e00\u4e2a\u53cd\u76f4\u89c9\u7684\u7ec6\u8282<\/h4>\n<p>\u524d\u9762\u8bf4\u8fc7 __ \u5f00\u5934\u7684\u5c5e\u6027\u4f1a\u88ab\u6539\u540d\u3002\u5b83\u5728\u7ee7\u627f\u91cc\u7684\u5f71\u54cd\u662f\u8fd9\u6837\u7684\u3002<\/p>\n<p>&gt;&gt;&gt; class Base:<br \/>\n&#8230;     def __init__(self):<br \/>\n&#8230;         self.__value &#061; 1<br \/>\n&#8230;     def get(self):<br \/>\n&#8230;         return self.__value<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class Sub(Base):<br \/>\n&#8230;     def get(self):<br \/>\n&#8230;         return self.__value<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; s &#061; Sub()<br \/>\n&gt;&gt;&gt; s.get()<br \/>\nAttributeError: &#039;Sub&#039; object has no attribute &#039;_Sub__value&#039;<\/p>\n<p>Sub \u91cc\u90a3\u4e2a self.__value \u4f1a\u88ab\u6539\u5199\u6210 self._Sub__value&#xff0c;\u800c\u7236\u7c7b\u521b\u5efa\u7684\u662f _Base__value&#xff0c;\u4e24\u4e2a\u540d\u5b57\u5bf9\u4e0d\u4e0a&#xff0c;\u6240\u4ee5\u53d6\u4e0d\u5230\u3002<\/p>\n<p>\u8fd9\u5176\u5b9e\u662f\u6709\u610f\u4e3a\u4e4b&#xff0c;\u53cc\u4e0b\u5212\u7ebf\u7684\u4f5c\u7528\u4e4b\u4e00\u5c31\u662f\u9632\u6b62\u5b50\u7c7b\u4e0d\u5c0f\u5fc3\u8986\u76d6\u7236\u7c7b\u7684\u5185\u90e8\u5c5e\u6027\u3002\u60f3\u8ba9\u5b50\u7c7b\u8bbf\u95ee&#xff0c;\u6539\u6210\u5355\u4e0b\u5212\u7ebf _value \u5c31\u884c&#xff0c;\u90a3\u53ea\u662f\u4e2a\u7ea6\u5b9a&#xff0c;\u4e0d\u505a\u6539\u540d\u3002<\/p>\n<p>&gt;&gt;&gt; class Base:<br \/>\n&#8230;     def __init__(self):<br \/>\n&#8230;         self._value &#061; 1<br \/>\n&#8230;     def get(self):<br \/>\n&#8230;         return self._value<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class Sub(Base):<br \/>\n&#8230;     def get(self):<br \/>\n&#8230;         return self._value * 10<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; s &#061; Sub()<br \/>\n&gt;&gt;&gt; s.get()<br \/>\n10<\/p>\n<h4>4.10 \u7ee7\u627f\u8fd9\u5757\u6211\u8e29\u8fc7\u7684\u5751<\/h4>\n<p>\u5751\u4e00&#xff0c;\u5b50\u7c7b\u5fd8\u4e86\u8c03 super().__init__()\u3002 4.5 \u6f14\u793a\u8fc7&#xff0c;\u75c7\u72b6\u662f\u7236\u7c7b\u5c5e\u6027\u8bbf\u95ee\u4e0d\u5230\u3002<\/p>\n<p>\u5751\u4e8c&#xff0c;\u7ee7\u627f\u7528\u5728\u4e86\u4e0d\u8be5\u7528\u7684\u5730\u65b9\u3002 \u5224\u65ad\u6807\u51c6\u5c31\u4e00\u53e5\u8bdd&#xff0c;\u770b\u4e24\u8005\u662f&#034;\u662f\u4e00\u79cd&#034;\u8fd8\u662f&#034;\u6709\u4e00\u4e2a&#034;\u3002\u72d7\u662f\u4e00\u79cd\u52a8\u7269&#xff0c;\u7528\u7ee7\u627f&#xff1b;\u6c7d\u8f66\u6709\u4e00\u4e2a\u53d1\u52a8\u673a&#xff0c;\u7528\u7ec4\u5408\u3002<\/p>\n<p>&gt;&gt;&gt; class Engine:<br \/>\n&#8230;     def start(self):<br \/>\n&#8230;         return &#034;\u5f15\u64ce\u542f\u52a8&#034;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class Car:<br \/>\n&#8230;     def __init__(self, brand):<br \/>\n&#8230;         self.brand &#061; brand<br \/>\n&#8230;         self.engine &#061; Engine()<br \/>\n&#8230;     def start(self):<br \/>\n&#8230;         return &#034;%s %s&#034; % (self.brand, self.engine.start())<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; car &#061; Car(&#034;\u6bd4\u4e9a\u8fea&#034;)<br \/>\n&gt;&gt;&gt; car.start()<br \/>\n&#039;\u6bd4\u4e9a\u8fea \u5f15\u64ce\u542f\u52a8&#039;<\/p>\n<p>Car \u6ca1\u6709\u7ee7\u627f Engine&#xff0c;\u800c\u662f\u628a\u53d1\u52a8\u673a\u88c5\u5728\u8eab\u4e0a\u4e86\u3002\u5199\u6210 class Car(Engine) \u4e5f\u80fd\u8dd1&#xff0c;\u4f46\u8bed\u4e49\u5c31\u6210\u4e86&#034;\u6c7d\u8f66\u662f\u4e00\u79cd\u53d1\u52a8\u673a&#034;&#xff0c;\u542c\u7740\u5c31\u4e0d\u5bf9\u3002\u800c\u4e14\u7ec4\u5408\u7684\u597d\u5904\u662f&#xff0c;\u6362\u4e00\u4e2a\u6df7\u52a8\u53d1\u52a8\u673a\u4e0d\u7528\u52a8 Car \u7684\u4ee3\u7801\u3002<\/p>\n<p>\u5751\u4e09&#xff0c;\u7ee7\u627f\u94fe\u592a\u6df1\u3002 \u6211\u63a5\u624b\u8fc7\u4e00\u4e2a\u9879\u76ee&#xff0c;A \u2192 B \u2192 C \u2192 D \u2192 E \u4e94\u5c42&#xff0c;\u60f3\u641e\u6e05\u695a\u67d0\u4e2a\u65b9\u6cd5\u6700\u7ec8\u5728\u54ea\u5b9e\u73b0\u7684&#xff0c;\u5f97\u987a\u7740 __mro__ \u4e00\u8def\u70b9\u8fdb\u53bb&#xff0c;\u6539\u4e00\u884c\u8981\u62c5\u5fc3\u56db\u4e2a\u7236\u7c7b\u3002\u8d85\u8fc7\u4e24\u5c42\u5c31\u8be5\u505c\u4e0b\u6765\u60f3\u60f3\u662f\u4e0d\u662f\u8be5\u62c6\u6210\u7ec4\u5408\u3002<\/p>\n<p>\u5751\u56db&#xff0c;\u591a\u7ee7\u627f\u65f6\u5fd8\u4e86\u5199 super()\u3002 \u94fe\u6761\u65ad\u5728\u4e2d\u95f4&#xff0c;\u540e\u9762\u7684\u7c7b\u4e0d\u4f1a\u88ab\u6267\u884c&#xff0c;\u800c\u4e14\u4e0d\u62a5\u9519&#xff0c;\u53ea\u662f\u5c11\u5e72\u4e86\u6d3b\u3002\u8fd9\u79cd bug \u7279\u522b\u96be\u67e5\u3002<\/p>\n<hr \/>\n<h3>5. \u5b9e\u6218<\/h3>\n<p>\u56db\u4e2a\u4f8b\u5b50&#xff0c;\u4ece\u5c01\u88c5\u5230\u7ee7\u627f&#xff0c;\u628a\u524d\u9762\u51e0\u8282\u4e32\u8d77\u6765&#xff0c;\u5efa\u8bae\u81ea\u5df1\u6572\u4e00\u904d\u3002<\/p>\n<h4>5.1 \u5b9e\u6218\u4e00&#xff0c;\u94f6\u884c\u8d26\u6237\u7cfb\u7edf<\/h4>\n<p>\u9700\u6c42\u3002\u8d26\u6237\u8981\u6709\u6237\u4e3b\u548c\u4f59\u989d&#xff0c;\u53ea\u80fd\u5b58\u3001\u53d6\u3001\u8f6c\u8d26&#xff0c;\u6bcf\u6b21\u64cd\u4f5c\u7559\u6d41\u6c34&#xff0c;\u4f59\u989d\u4e0d\u80fd\u4ece\u5916\u9762\u76f4\u63a5\u6539\u3002<\/p>\n<p>\u601d\u8def\u3002<\/p>\n<p>BankAccount<br \/>\n\u251c\u2500\u2500 \u7c7b\u5c5e\u6027  bank_name\u3001total        \u2190 \u5168\u884c\u5171\u7528<br \/>\n\u251c\u2500\u2500 \u5b9e\u4f8b\u5c5e\u6027 __balance\u3001__logs      \u2190 \u53cc\u4e0b\u5212\u7ebf&#xff0c;\u5916\u9762\u78b0\u4e0d\u5230<br \/>\n\u251c\u2500\u2500 property  balance&#xff08;\u53ea\u8bfb&#xff09;\u3001logs&#xff08;\u8fd4\u56de\u5143\u7ec4&#xff09;<br \/>\n\u2514\u2500\u2500 \u65b9\u6cd5     deposit\u3001withdraw\u3001transfer<\/p>\n<p>\u4ee3\u7801\u3002<\/p>\n<p>class BankAccount:<br \/>\n    &#034;&#034;&#034;\u94f6\u884c\u8d26\u6237&#xff0c;\u4f59\u989d\u53ea\u80fd\u901a\u8fc7\u5b58\u53d6\u6b3e\u65b9\u6cd5\u6539\u52a8&#034;&#034;&#034;<\/p>\n<p>    bank_name &#061; &#034;\u6d3e\u68ee\u94f6\u884c&#034;<br \/>\n    total &#061; 0<\/p>\n<p>    def __init__(self, owner, opening&#061;0):<br \/>\n        self.owner &#061; owner<br \/>\n        self.__balance &#061; 0<br \/>\n        self.__logs &#061; []<br \/>\n        BankAccount.total &#043;&#061; 1<br \/>\n        if opening &gt; 0:<br \/>\n            self.deposit(opening)<\/p>\n<p>    &#064;property<br \/>\n    def balance(self):<br \/>\n        &#034;&#034;&#034;\u4f59\u989d&#xff0c;\u53ea\u8bfb&#034;&#034;&#034;<br \/>\n        return self.__balance<\/p>\n<p>    &#064;property<br \/>\n    def logs(self):<br \/>\n        &#034;&#034;&#034;\u6d41\u6c34&#xff0c;\u8fd4\u56de\u5143\u7ec4\u9632\u6b62\u5916\u9762\u6539&#034;&#034;&#034;<br \/>\n        return tuple(self.__logs)<\/p>\n<p>    def deposit(self, amount):<br \/>\n        &#034;&#034;&#034;\u5b58\u94b1&#034;&#034;&#034;<br \/>\n        if amount &lt;&#061; 0:<br \/>\n            raise ValueError(&#034;\u5b58\u5165\u91d1\u989d\u5fc5\u987b\u5927\u4e8e 0&#xff0c;\u6536\u5230 %s&#034; % amount)<br \/>\n        self.__balance &#043;&#061; amount<br \/>\n        self.__logs.append(&#034;\u5b58\u5165 %.2f&#xff0c;\u4f59\u989d %.2f&#034; % (amount, self.__balance))<br \/>\n        return self.__balance<\/p>\n<p>    def withdraw(self, amount):<br \/>\n        &#034;&#034;&#034;\u53d6\u94b1&#034;&#034;&#034;<br \/>\n        if amount &lt;&#061; 0:<br \/>\n            raise ValueError(&#034;\u53d6\u6b3e\u91d1\u989d\u5fc5\u987b\u5927\u4e8e 0&#xff0c;\u6536\u5230 %s&#034; % amount)<br \/>\n        if amount &gt; self.__balance:<br \/>\n            raise ValueError(&#034;\u4f59\u989d\u4e0d\u8db3&#xff0c;\u5f53\u524d %.2f \u60f3\u53d6 %.2f&#034; % (self.__balance, amount))<br \/>\n        self.__balance -&#061; amount<br \/>\n        self.__logs.append(&#034;\u53d6\u51fa %.2f&#xff0c;\u4f59\u989d %.2f&#034; % (amount, self.__balance))<br \/>\n        return self.__balance<\/p>\n<p>    def transfer(self, other, amount):<br \/>\n        &#034;&#034;&#034;\u8f6c\u8d26\u7ed9\u53e6\u4e00\u4e2a\u8d26\u6237&#034;&#034;&#034;<br \/>\n        self.withdraw(amount)<br \/>\n        other.deposit(amount)<br \/>\n        self.__logs.append(&#034;\u8f6c\u7ed9 %s %.2f&#034; % (other.owner, amount))<br \/>\n        other.__logs.append(&#034;\u6536\u5230 %s \u8f6c\u6765 %.2f&#034; % (self.owner, amount))<br \/>\n        return self.__balance<\/p>\n<p>    def __str__(self):<br \/>\n        return &#034;%s \u7684\u8d26\u6237&#xff0c;\u4f59\u989d %.2f&#034; % (self.owner, self.__balance)<\/p>\n<p>zhang &#061; BankAccount(&#034;\u5f20\u4e09&#034;, 1000)<br \/>\nli &#061; BankAccount(&#034;\u674e\u56db&#034;, 500)<\/p>\n<p>print(&#034;\u5f00\u6237\u884c&#034;, BankAccount.bank_name, &#034;\u5df2\u5f00\u8d26\u6237\u6570&#034;, BankAccount.total)<br \/>\nprint(zhang)<br \/>\nprint(li)<\/p>\n<p>zhang.withdraw(200)<br \/>\nzhang.transfer(li, 300)<\/p>\n<p>print(&#034;\\\\n&#8212; \u4ea4\u6613\u4e4b\u540e\u7684\u4f59\u989d&#034;)<br \/>\nprint(zhang)<br \/>\nprint(li)<\/p>\n<p>print(&#034;\\\\n&#8212; \u5f20\u4e09\u7684\u6d41\u6c34&#034;)<br \/>\nfor line in zhang.logs:<br \/>\n    print(&#034; &#034;, line)<\/p>\n<p>print(&#034;\\\\n&#8212; \u674e\u56db\u7684\u6d41\u6c34&#034;)<br \/>\nfor line in li.logs:<br \/>\n    print(&#034; &#034;, line)<\/p>\n<p>print(&#034;\\\\n\u4f59\u989d\u662f\u53ea\u8bfb\u7684&#xff0c;\u76f4\u63a5\u6539\u4f1a\u62a5\u9519&#034;)<br \/>\ntry:<br \/>\n    zhang.balance &#061; 999999<br \/>\nexcept AttributeError as e:<br \/>\n    print(&#034;  AttributeError:&#034;, e)<\/p>\n<p>try:<br \/>\n    zhang.withdraw(99999)<br \/>\nexcept ValueError as e:<br \/>\n    print(&#034; &#034;, type(e).__name__ &#043; &#034;:&#034;, e)<\/p>\n<p>print(&#034;\\\\n\u6d41\u6c34\u8fd4\u56de\u7684\u662f\u5143\u7ec4&#xff0c;\u6539\u4e0d\u52a8&#034;)<br \/>\nprint(&#034; &#034;, type(zhang.logs).__name__)<\/p>\n<p>\u8f93\u51fa\u3002<\/p>\n<p>\u5f00\u6237\u884c \u6d3e\u68ee\u94f6\u884c \u5df2\u5f00\u8d26\u6237\u6570 2<br \/>\n\u5f20\u4e09 \u7684\u8d26\u6237&#xff0c;\u4f59\u989d 1000.00<br \/>\n\u674e\u56db \u7684\u8d26\u6237&#xff0c;\u4f59\u989d 500.00<\/p>\n<p>&#8212; \u4ea4\u6613\u4e4b\u540e\u7684\u4f59\u989d<br \/>\n\u5f20\u4e09 \u7684\u8d26\u6237&#xff0c;\u4f59\u989d 500.00<br \/>\n\u674e\u56db \u7684\u8d26\u6237&#xff0c;\u4f59\u989d 800.00<\/p>\n<p>&#8212; \u5f20\u4e09\u7684\u6d41\u6c34<br \/>\n  \u5b58\u5165 1000.00&#xff0c;\u4f59\u989d 1000.00<br \/>\n  \u53d6\u51fa 200.00&#xff0c;\u4f59\u989d 800.00<br \/>\n  \u53d6\u51fa 300.00&#xff0c;\u4f59\u989d 500.00<br \/>\n  \u8f6c\u7ed9 \u674e\u56db 300.00<\/p>\n<p>&#8212; \u674e\u56db\u7684\u6d41\u6c34<br \/>\n  \u5b58\u5165 500.00&#xff0c;\u4f59\u989d 500.00<br \/>\n  \u5b58\u5165 300.00&#xff0c;\u4f59\u989d 800.00<br \/>\n  \u6536\u5230 \u5f20\u4e09 \u8f6c\u6765 300.00<\/p>\n<p>\u4f59\u989d\u662f\u53ea\u8bfb\u7684&#xff0c;\u76f4\u63a5\u6539\u4f1a\u62a5\u9519<br \/>\n  AttributeError: property &#039;balance&#039; of &#039;BankAccount&#039; object has no setter<br \/>\n  ValueError: \u4f59\u989d\u4e0d\u8db3&#xff0c;\u5f53\u524d 500.00 \u60f3\u53d6 99999.00<\/p>\n<p>\u6d41\u6c34\u8fd4\u56de\u7684\u662f\u5143\u7ec4&#xff0c;\u6539\u4e0d\u52a8<br \/>\n  tuple<\/p>\n<p>\u590d\u76d8\u3002<\/p>\n<p>balance \u662f\u53ea\u8bfb property&#xff0c;\u5916\u9762\u8bfb\u5f97\u5230\u6539\u4e0d\u52a8&#xff0c;\u6539\u4f59\u989d\u53ea\u6709\u5b58\u53d6\u6b3e\u548c\u8f6c\u8d26\u4e09\u6761\u8def&#xff0c;\u6bcf\u6761\u8def\u90fd\u6709\u6821\u9a8c\u3002\u8fd9\u5c31\u662f\u5c01\u88c5\u6700\u5b9e\u5728\u7684\u597d\u5904&#xff0c;\u94b1\u8fd9\u4e2a\u4e1c\u897f\u4e0d\u8be5\u655e\u7740\u3002<\/p>\n<p>logs \u8fd4\u56de\u7684\u662f tuple(self.__logs) \u800c\u4e0d\u662f\u5217\u8868\u672c\u8eab\u3002\u76f4\u63a5\u628a\u5217\u8868\u4ea4\u51fa\u53bb\u7684\u8bdd&#xff0c;\u5916\u9762\u62ff\u5230\u5f15\u7528\u5c31\u80fd\u968f\u4fbf append \u4f2a\u9020\u6d41\u6c34&#xff0c;\u8f6c\u6210\u5143\u7ec4\u5835\u4f4f\u4e86\u8fd9\u6761\u8def\u3002\u8fd9\u4e5f\u662f\u6211\u5904\u7406&#034;\u5185\u90e8\u6709\u53ef\u53d8\u6570\u636e\u4f46\u8981\u66b4\u9732\u51fa\u53bb&#034;\u65f6\u7684\u5e38\u7528\u624b\u6cd5\u3002<\/p>\n<p>transfer \u91cc\u80fd\u5199 other.__logs&#xff0c;\u56e0\u4e3a\u53cc\u4e0b\u5212\u7ebf\u6539\u540d\u662f\u6309\u7c7b\u540d\u8d70\u7684&#xff0c;BankAccount \u5185\u90e8\u8bbf\u95ee\u4efb\u4f55 BankAccount \u5bf9\u8c61\u7684 __logs \u90fd\u5408\u6cd5\u3002\u540c\u7c7b\u4e4b\u95f4\u4e0d\u8bbe\u9632&#xff0c;\u9632\u7684\u662f\u5916\u9762\u7684\u4ee3\u7801\u3002<\/p>\n<p>BankAccount.total &#043;&#061; 1 \u5199\u7684\u662f\u7c7b\u540d\u3002\u5199\u5b8c\u8fd9\u4e00\u884c\u518d\u770b 2.6 \u90a3\u6bb5&#xff0c;\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u5199 self.total &#043;&#061; 1&#xff0c;\u90a3\u91cc\u7684\u89e3\u91ca\u5728\u8fd9\u513f\u6709\u4e86\u5b9e\u9645\u573a\u666f\u3002<\/p>\n<h4>5.2 \u5b9e\u6218\u4e8c&#xff0c;\u7535\u5546\u8d2d\u7269\u8f66<\/h4>\n<p>\u9700\u6c42\u3002\u5f80\u8d2d\u7269\u8f66\u52a0\u5546\u54c1\u3001\u6539\u6570\u91cf\u3001\u5220\u884c&#xff0c;\u81ea\u52a8\u7b97\u6298\u6263&#xff0c;\u6253\u5c0f\u7968\u3002<\/p>\n<p>\u601d\u8def\u3002\u4e09\u4e2a\u7c7b\u5404\u7ba1\u4e00\u5c42&#xff0c;\u5178\u578b\u7684\u7ec4\u5408\u5173\u7cfb\u3002<\/p>\n<p>Cart  \u2500\u2500\u5305\u542b\u2500\u2500\u25ba  CartItem  \u2500\u2500\u5f15\u7528\u2500\u2500\u25ba  Goods<br \/>\n \u2502                 \u2502                   \u2502<br \/>\n \u2502  owner          \u2502  goods \/ count    \u2502  name \/ price \/ unit<br \/>\n \u2502  __items        \u2502  subtotal(property)<br \/>\n \u2502  total \/ discount \/ payable(property)<br \/>\n \u2502  receipt()<\/p>\n<p>\u4ee3\u7801\u3002<\/p>\n<p>import unicodedata<\/p>\n<p>def pad(text, width):<br \/>\n    &#034;&#034;&#034;\u6309\u5b9e\u9645\u663e\u793a\u5bbd\u5ea6\u8865\u7a7a\u683c&#xff0c;\u4e2d\u6587\u7b97\u4e24\u5217&#034;&#034;&#034;<br \/>\n    shown &#061; 0<br \/>\n    for ch in str(text):<br \/>\n        shown &#043;&#061; 2 if unicodedata.east_asian_width(ch) in (&#034;W&#034;, &#034;F&#034;) else 1<br \/>\n    return str(text) &#043; &#034; &#034; * max(0, width &#8211; shown)<\/p>\n<p>DISCOUNT_RULES &#061; [<br \/>\n    (200, 0.95),<br \/>\n    (500, 0.90),<br \/>\n    (1000, 0.85),<br \/>\n]<\/p>\n<p>class Goods:<br \/>\n    &#034;&#034;&#034;\u5546\u54c1&#034;&#034;&#034;<\/p>\n<p>    __slots__ &#061; (&#034;name&#034;, &#034;price&#034;, &#034;unit&#034;)<\/p>\n<p>    def __init__(self, name, price, unit&#061;&#034;\u4ef6&#034;):<br \/>\n        self.name &#061; name<br \/>\n        self.price &#061; price<br \/>\n        self.unit &#061; unit<\/p>\n<p>    def __str__(self):<br \/>\n        return &#034;%s %.2f\u5143\/%s&#034; % (self.name, self.price, self.unit)<\/p>\n<p>    __repr__ &#061; __str__<\/p>\n<p>class CartItem:<br \/>\n    &#034;&#034;&#034;\u8d2d\u7269\u8f66\u91cc\u7684\u4e00\u884c&#xff0c;\u5546\u54c1\u52a0\u6570\u91cf&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, goods, count&#061;1):<br \/>\n        self.goods &#061; goods<br \/>\n        self.count &#061; count<\/p>\n<p>    &#064;property<br \/>\n    def subtotal(self):<br \/>\n        &#034;&#034;&#034;\u5c0f\u8ba1&#xff0c;\u73b0\u7b97\u7684&#034;&#034;&#034;<br \/>\n        return round(self.goods.price * self.count, 2)<\/p>\n<p>    def __str__(self):<br \/>\n        return pad(self.goods.name, 14) &#043; pad(&#034;x&#034; &#043; str(self.count), 6) &#043; &#034;%8.2f&#034; % self.subtotal<\/p>\n<p>class Cart:<br \/>\n    &#034;&#034;&#034;\u8d2d\u7269\u8f66&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, owner):<br \/>\n        self.owner &#061; owner<br \/>\n        self.__items &#061; []<\/p>\n<p>    def add(self, goods, count&#061;1):<br \/>\n        &#034;&#034;&#034;\u52a0\u8d2d&#xff0c;\u5df2\u6709\u5c31\u7d2f\u52a0\u6570\u91cf&#034;&#034;&#034;<br \/>\n        for item in self.__items:<br \/>\n            if item.goods.name &#061;&#061; goods.name:<br \/>\n                item.count &#043;&#061; count<br \/>\n                return<br \/>\n        self.__items.append(CartItem(goods, count))<\/p>\n<p>    def remove(self, name, count&#061;None):<br \/>\n        &#034;&#034;&#034;\u51cf\u6570\u91cf&#xff0c;\u4e0d\u4f20 count \u5c31\u6574\u884c\u5220\u6389&#034;&#034;&#034;<br \/>\n        for i, item in enumerate(self.__items):<br \/>\n            if item.goods.name &#061;&#061; name:<br \/>\n                if count is None or count &gt;&#061; item.count:<br \/>\n                    self.__items.pop(i)<br \/>\n                else:<br \/>\n                    item.count -&#061; count<br \/>\n                return True<br \/>\n        return False<\/p>\n<p>    &#064;property<br \/>\n    def items(self):<br \/>\n        return tuple(self.__items)<\/p>\n<p>    &#064;property<br \/>\n    def kinds(self):<br \/>\n        &#034;&#034;&#034;\u6709\u51e0\u79cd\u5546\u54c1&#034;&#034;&#034;<br \/>\n        return len(self.__items)<\/p>\n<p>    &#064;property<br \/>\n    def quantity(self):<br \/>\n        &#034;&#034;&#034;\u4e00\u5171\u591a\u5c11\u4ef6&#034;&#034;&#034;<br \/>\n        return sum(item.count for item in self.__items)<\/p>\n<p>    &#064;property<br \/>\n    def total(self):<br \/>\n        &#034;&#034;&#034;\u539f\u4ef7\u5408\u8ba1&#034;&#034;&#034;<br \/>\n        return round(sum(item.subtotal for item in self.__items), 2)<\/p>\n<p>    &#064;property<br \/>\n    def discount(self):<br \/>\n        &#034;&#034;&#034;\u6309\u91d1\u989d\u81ea\u52a8\u6253\u6298&#034;&#034;&#034;<br \/>\n        rate &#061; 1.0<br \/>\n        for limit, value in DISCOUNT_RULES:<br \/>\n            if self.total &gt;&#061; limit:<br \/>\n                rate &#061; value<br \/>\n        return rate<\/p>\n<p>    &#064;property<br \/>\n    def payable(self):<br \/>\n        &#034;&#034;&#034;\u5e94\u4ed8&#034;&#034;&#034;<br \/>\n        return round(self.total * self.discount, 2)<\/p>\n<p>    def receipt(self):<br \/>\n        &#034;&#034;&#034;\u6253\u5c0f\u7968&#034;&#034;&#034;<br \/>\n        print(&#034;&#061;&#034; * 46)<br \/>\n        print(&#034;\u4e70\u5bb6 %s&#034; % self.owner)<br \/>\n        print(&#034;&#061;&#034; * 46)<br \/>\n        print(&#034;  &#034; &#043; pad(&#034;\u5546\u54c1&#034;, 14) &#043; pad(&#034;\u6570\u91cf&#034;, 6) &#043; pad(&#034;\u5c0f\u8ba1&#034;, 8))<br \/>\n        for item in self.__items:<br \/>\n            print(&#034;  &#034; &#043; str(item))<br \/>\n        print(&#034;-&#034; * 46)<br \/>\n        print(&#034;\u5171 %d \u79cd %d \u4ef6&#xff0c;\u539f\u4ef7 %.2f \u5143&#034; % (self.kinds, self.quantity, self.total))<br \/>\n        print(&#034;\u6298\u6263 %.2f&#xff0c;\u5e94\u4ed8 %.2f \u5143&#xff0c;\u7701 %.2f \u5143&#034;<br \/>\n              % (self.discount, self.payable, self.total &#8211; self.payable))<br \/>\n        print(&#034;&#061;&#034; * 46)<\/p>\n<p>catalog &#061; [<br \/>\n    Goods(&#034;\u673a\u68b0\u952e\u76d8&#034;, 299, &#034;\u4e2a&#034;),<br \/>\n    Goods(&#034;\u65e0\u7ebf\u9f20\u6807&#034;, 89, &#034;\u4e2a&#034;),<br \/>\n    Goods(&#034;\u663e\u793a\u5668&#034;, 1299, &#034;\u53f0&#034;),<br \/>\n    Goods(&#034;USB \u7ebf&#034;, 19.9, &#034;\u6839&#034;),<br \/>\n]<\/p>\n<p>cart &#061; Cart(&#034;\u5f20\u4e09&#034;)<br \/>\ncart.add(catalog[0])<br \/>\ncart.add(catalog[1], 2)<br \/>\ncart.add(catalog[3], 3)<br \/>\ncart.receipt()<\/p>\n<p>print(&#034;\\\\n\u518d\u52a0\u4e00\u53f0\u663e\u793a\u5668&#xff0c;\u770b\u770b\u6298\u6263\u53d8\u5316&#034;)<br \/>\ncart.add(catalog[2])<br \/>\ncart.receipt()<\/p>\n<p>print(&#034;\\\\n\u5220\u6389 2 \u6839 USB \u7ebf&#034;)<br \/>\ncart.remove(&#034;USB \u7ebf&#034;, 2)<br \/>\nprint(&#034;\u73b0\u5728 %d \u79cd %d \u4ef6&#xff0c;\u5e94\u4ed8 %.2f&#034; % (cart.kinds, cart.quantity, cart.payable))<\/p>\n<p>print(&#034;\\\\n\u8d2d\u7269\u8f66\u91cc\u88c5\u7684\u662f\u5bf9\u8c61&#034;)<br \/>\nprint(&#034; &#034;, cart.items[0].goods, &#034;\u5355\u4ef7&#034;, cart.items[0].goods.price)<\/p>\n<p>\u8f93\u51fa\u3002<\/p>\n<p>&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n\u4e70\u5bb6 \u5f20\u4e09<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n  \u5546\u54c1          \u6570\u91cf  \u5c0f\u8ba1<br \/>\n  \u673a\u68b0\u952e\u76d8      x1      299.00<br \/>\n  \u65e0\u7ebf\u9f20\u6807      x2      178.00<br \/>\n  USB \u7ebf        x3       59.70<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n\u5171 3 \u79cd 6 \u4ef6&#xff0c;\u539f\u4ef7 536.70 \u5143<br \/>\n\u6298\u6263 0.90&#xff0c;\u5e94\u4ed8 483.03 \u5143&#xff0c;\u7701 53.67 \u5143<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<\/p>\n<p>\u518d\u52a0\u4e00\u53f0\u663e\u793a\u5668&#xff0c;\u770b\u770b\u6298\u6263\u53d8\u5316<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n\u4e70\u5bb6 \u5f20\u4e09<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n  \u5546\u54c1          \u6570\u91cf  \u5c0f\u8ba1<br \/>\n  \u673a\u68b0\u952e\u76d8      x1      299.00<br \/>\n  \u65e0\u7ebf\u9f20\u6807      x2      178.00<br \/>\n  USB \u7ebf        x3       59.70<br \/>\n  \u663e\u793a\u5668        x1     1299.00<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n\u5171 4 \u79cd 7 \u4ef6&#xff0c;\u539f\u4ef7 1835.70 \u5143<br \/>\n\u6298\u6263 0.85&#xff0c;\u5e94\u4ed8 1560.35 \u5143&#xff0c;\u7701 275.35 \u5143<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<\/p>\n<p>\u5220\u6389 2 \u6839 USB \u7ebf<br \/>\n\u73b0\u5728 4 \u79cd 5 \u4ef6&#xff0c;\u5e94\u4ed8 1526.52<\/p>\n<p>\u8d2d\u7269\u8f66\u91cc\u88c5\u7684\u662f\u5bf9\u8c61<br \/>\n  \u673a\u68b0\u952e\u76d8 299.00\u5143\/\u4e2a \u5355\u4ef7 299<\/p>\n<p>\u590d\u76d8\u3002<\/p>\n<p>Goods \u7528\u4e86 __slots__&#xff0c;\u5546\u54c1\u5b57\u6bb5\u5c31\u4e09\u4e2a&#xff0c;\u4e0d\u5141\u8bb8\u4e71\u52a0\u3002\u5bf9\u8c61\u591a\u7684\u573a\u666f\u7701\u5185\u5b58&#xff0c;\u4e5f\u5835\u4f4f\u4e86\u62fc\u9519\u5c5e\u6027\u540d\u3002<\/p>\n<p>subtotal\u3001total\u3001discount\u3001payable \u5168\u662f property&#xff0c;\u6ca1\u6709\u4e00\u4e2a\u5728\u5185\u5b58\u91cc\u5b58\u503c&#xff0c;\u5168\u662f\u8bfb\u7684\u65f6\u5019\u73b0\u7b97\u3002\u597d\u5904\u662f\u6539\u6570\u91cf\u3001\u6539\u6298\u6263\u4e4b\u540e\u4e0d\u7528\u624b\u52a8\u540c\u6b65&#xff0c;\u8bfb\u5230\u7684\u6c38\u8fdc\u662f\u6700\u65b0\u7ed3\u679c\u3002\u8fd9\u662f property \u6700\u9002\u5408\u7684\u5730\u65b9\u3002<\/p>\n<p>discount \u90a3\u4e2a\u5b9e\u73b0\u6709\u70b9\u610f\u601d&#xff0c;\u5b83\u4ece DISCOUNT_RULES \u91cc\u987a\u5e8f\u67e5&#xff0c;\u6700\u540e\u7684\u89c4\u5219\u8986\u76d6\u524d\u9762\u7684&#xff0c;\u6240\u4ee5\u6ee1\u4e86 1000 \u5c31\u6253 85 \u6298\u3002\u89c4\u5219\u5199\u5728\u7c7b\u5916\u9762&#xff0c;\u6539\u6298\u6263\u4e0d\u7528\u52a8\u7c7b\u3002<\/p>\n<p>pad \u8fd9\u4e2a\u51fd\u6570\u662f\u7b2c\u516d\u7ae0\u5b9e\u6218\u4e00\u90a3\u4f4d\u7684\u8001\u670b\u53cb&#xff0c;\u4e2d\u6587\u5360\u4e24\u5217\u5bbd\u5ea6&#xff0c;%-14s \u6392\u51fa\u6765\u7684\u8868\u662f\u6b6a\u7684\u3002\u547d\u4ee4\u884c\u91cc\u6253\u4e2d\u6587\u62a5\u8868\u8eb2\u4e0d\u5f00\u5b83\u3002<\/p>\n<p>cart.items[0].goods \u8fd9\u884c\u662f\u7ec4\u5408\u7684\u6837\u5b50&#xff0c;\u8d2d\u7269\u8f66\u4e0d\u5b58\u5546\u54c1\u7684\u540d\u5b57\u548c\u4ef7\u683c&#xff0c;\u5b83\u5b58\u7684\u662f\u5546\u54c1\u5bf9\u8c61\u672c\u8eab\u3002\u5c06\u6765\u5546\u54c1\u8981\u52a0\u5e93\u5b58\u3001\u5206\u7c7b\u3001\u56fe\u7247&#xff0c;CartItem \u4e00\u884c\u90fd\u4e0d\u7528\u6539\u3002<\/p>\n<h4>5.3 \u5b9e\u6218\u4e09&#xff0c;\u56fe\u5f62\u9762\u79ef\u8ba1\u7b97\u5668<\/h4>\n<p>\u9700\u6c42\u3002\u652f\u6301\u5706\u3001\u77e9\u5f62\u3001\u4e09\u89d2\u5f62&#xff0c;\u80fd\u7b97\u9762\u79ef\u548c\u5468\u957f&#xff0c;\u80fd\u653e\u8fdb\u540c\u4e00\u4e2a\u5217\u8868\u91cc\u7edf\u4e00\u5904\u7406&#xff0c;\u8fb9\u957f\u53c2\u6570\u8981\u6821\u9a8c\u3002<\/p>\n<p>\u601d\u8def\u3002<\/p>\n<p>        Shape&#xff08;\u57fa\u7c7b&#xff0c;\u53ea\u5b9a\u63a5\u53e3&#xff09;<br \/>\n        area() \/ perimeter() \/ describe()<br \/>\n             \u2502<br \/>\n      \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n      \u25bc      \u25bc         \u25bc<br \/>\n   Circle  Rectangle  Triangle<br \/>\n   radius  w\/h        \u4e09\u8fb9<\/p>\n<p>\u4ee3\u7801\u3002<\/p>\n<p>import math<br \/>\nimport unicodedata<\/p>\n<p>def pad(text, width):<br \/>\n    &#034;&#034;&#034;\u6309\u5b9e\u9645\u663e\u793a\u5bbd\u5ea6\u8865\u7a7a\u683c&#xff0c;\u4e2d\u6587\u7b97\u4e24\u5217&#034;&#034;&#034;<br \/>\n    shown &#061; 0<br \/>\n    for ch in str(text):<br \/>\n        shown &#043;&#061; 2 if unicodedata.east_asian_width(ch) in (&#034;W&#034;, &#034;F&#034;) else 1<br \/>\n    return str(text) &#043; &#034; &#034; * max(0, width &#8211; shown)<\/p>\n<p>class Shape:<br \/>\n    &#034;&#034;&#034;\u56fe\u5f62\u57fa\u7c7b&#xff0c;\u53ea\u5b9a\u63a5\u53e3&#xff0c;\u4e0d\u5b9e\u73b0&#034;&#034;&#034;<\/p>\n<p>    def area(self):<br \/>\n        raise NotImplementedError(&#034;\u5b50\u7c7b\u5fc5\u987b\u5b9e\u73b0 area&#034;)<\/p>\n<p>    def perimeter(self):<br \/>\n        raise NotImplementedError(&#034;\u5b50\u7c7b\u5fc5\u987b\u5b9e\u73b0 perimeter&#034;)<\/p>\n<p>    def describe(self):<br \/>\n        return &#034;%s \u9762\u79ef %.2f \u5468\u957f %.2f&#034; % (self.__class__.__name__, self.area(), self.perimeter())<\/p>\n<p>class Circle(Shape):<br \/>\n    def __init__(self, radius):<br \/>\n        if radius &lt;&#061; 0:<br \/>\n            raise ValueError(&#034;\u534a\u5f84\u5fc5\u987b\u5927\u4e8e 0&#034;)<br \/>\n        self.radius &#061; radius<\/p>\n<p>    def area(self):<br \/>\n        return math.pi * self.radius ** 2<\/p>\n<p>    def perimeter(self):<br \/>\n        return 2 * math.pi * self.radius<\/p>\n<p>    def __str__(self):<br \/>\n        return &#034;\u5706 r&#061;%g&#034; % self.radius<\/p>\n<p>class Rectangle(Shape):<br \/>\n    def __init__(self, width, height):<br \/>\n        if width &lt;&#061; 0 or height &lt;&#061; 0:<br \/>\n            raise ValueError(&#034;\u957f\u5bbd\u5fc5\u987b\u5927\u4e8e 0&#034;)<br \/>\n        self.width &#061; width<br \/>\n        self.height &#061; height<\/p>\n<p>    def area(self):<br \/>\n        return self.width * self.height<\/p>\n<p>    def perimeter(self):<br \/>\n        return 2 * (self.width &#043; self.height)<\/p>\n<p>    &#064;property<br \/>\n    def is_square(self):<br \/>\n        return self.width &#061;&#061; self.height<\/p>\n<p>    def __str__(self):<br \/>\n        return &#034;\u77e9\u5f62 %gx%g&#034; % (self.width, self.height)<\/p>\n<p>class Triangle(Shape):<br \/>\n    def __init__(self, a, b, c):<br \/>\n        sides &#061; sorted([a, b, c])<br \/>\n        if sides[0] &lt;&#061; 0:<br \/>\n            raise ValueError(&#034;\u8fb9\u957f\u5fc5\u987b\u5927\u4e8e 0&#034;)<br \/>\n        if sides[0] &#043; sides[1] &lt;&#061; sides[2]:<br \/>\n            raise ValueError(&#034;\u4e24\u8fb9\u4e4b\u548c\u5fc5\u987b\u5927\u4e8e\u7b2c\u4e09\u8fb9&#034;)<br \/>\n        self.a, self.b, self.c &#061; a, b, c<\/p>\n<p>    def area(self):<br \/>\n        &#034;&#034;&#034;\u6d77\u4f26\u516c\u5f0f&#034;&#034;&#034;<br \/>\n        s &#061; self.perimeter() \/ 2<br \/>\n        return math.sqrt(s * (s &#8211; self.a) * (s &#8211; self.b) * (s &#8211; self.c))<\/p>\n<p>    def perimeter(self):<br \/>\n        return self.a &#043; self.b &#043; self.c<\/p>\n<p>    def __str__(self):<br \/>\n        return &#034;\u4e09\u89d2\u5f62 %g\/%g\/%g&#034; % (self.a, self.b, self.c)<\/p>\n<p>def total_area(shapes):<br \/>\n    &#034;&#034;&#034;\u4e0d\u7ba1\u4ec0\u4e48\u56fe\u5f62&#xff0c;\u6709 area \u5c31\u80fd\u7b97&#034;&#034;&#034;<br \/>\n    return round(sum(s.area() for s in shapes), 2)<\/p>\n<p>shapes &#061; [<br \/>\n    Circle(3),<br \/>\n    Rectangle(4, 6),<br \/>\n    Rectangle(5, 5),<br \/>\n    Triangle(3, 4, 5),<br \/>\n]<\/p>\n<p>print(&#034;&#061;&#034; * 52)<br \/>\nprint(pad(&#034;\u56fe\u5f62&#034;, 18) &#043; pad(&#034;\u9762\u79ef&#034;, 10) &#043; pad(&#034;\u5468\u957f&#034;, 10))<br \/>\nprint(&#034;&#061;&#034; * 52)<br \/>\nfor s in shapes:<br \/>\n    print(pad(s, 18) &#043; pad(&#034;%.2f&#034; % s.area(), 10) &#043; pad(&#034;%.2f&#034; % s.perimeter(), 10))<br \/>\nprint(&#034;&#061;&#034; * 52)<br \/>\nprint(&#034;\u5408\u8ba1\u9762\u79ef %.2f&#034; % total_area(shapes))<\/p>\n<p>print(&#034;\\\\n\u6b63\u65b9\u5f62\u5224\u65ad&#034;, [str(s) for s in shapes if isinstance(s, Rectangle) and s.is_square])<\/p>\n<p>print(&#034;\\\\n\u6309\u9762\u79ef\u6392\u5e8f&#034;)<br \/>\nfor s in sorted(shapes, key&#061;lambda x: x.area(), reverse&#061;True):<br \/>\n    print(&#034;  &#034; &#043; pad(s, 16) &#043; &#034;%10.2f&#034; % s.area())<\/p>\n<p>print(&#034;\\\\n\u6ca1\u5b9e\u73b0\u63a5\u53e3\u7684\u8bdd&#034;)<br \/>\ntry:<br \/>\n    Shape().area()<br \/>\nexcept NotImplementedError as e:<br \/>\n    print(&#034;  NotImplementedError:&#034;, e)<\/p>\n<p>print(&#034;\\\\n\u53c2\u6570\u6821\u9a8c&#034;)<br \/>\nfor bad in [lambda: Circle(-1), lambda: Rectangle(0, 5), lambda: Triangle(1, 2, 5)]:<br \/>\n    try:<br \/>\n        bad()<br \/>\n    except ValueError as e:<br \/>\n        print(&#034;  ValueError:&#034;, e)<\/p>\n<p>\u8f93\u51fa\u3002<\/p>\n<p>&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n\u56fe\u5f62              \u9762\u79ef      \u5468\u957f<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n\u5706 r&#061;3            28.27     18.85<br \/>\n\u77e9\u5f62 4&#215;6          24.00     20.00<br \/>\n\u77e9\u5f62 5&#215;5          25.00     20.00<br \/>\n\u4e09\u89d2\u5f62 3\/4\/5      6.00      12.00<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n\u5408\u8ba1\u9762\u79ef 83.27<\/p>\n<p>\u6b63\u65b9\u5f62\u5224\u65ad [&#039;\u77e9\u5f62 5&#215;5&#039;]<\/p>\n<p>\u6309\u9762\u79ef\u6392\u5e8f<br \/>\n  \u5706 r&#061;3               28.27<br \/>\n  \u77e9\u5f62 5&#215;5             25.00<br \/>\n  \u77e9\u5f62 4&#215;6             24.00<br \/>\n  \u4e09\u89d2\u5f62 3\/4\/5          6.00<\/p>\n<p>\u6ca1\u5b9e\u73b0\u63a5\u53e3\u7684\u8bdd<br \/>\n  NotImplementedError: \u5b50\u7c7b\u5fc5\u987b\u5b9e\u73b0 area<\/p>\n<p>\u53c2\u6570\u6821\u9a8c<br \/>\n  ValueError: \u534a\u5f84\u5fc5\u987b\u5927\u4e8e 0<br \/>\n  ValueError: \u957f\u5bbd\u5fc5\u987b\u5927\u4e8e 0<br \/>\n  ValueError: \u4e24\u8fb9\u4e4b\u548c\u5fc5\u987b\u5927\u4e8e\u7b2c\u4e09\u8fb9<\/p>\n<p>\u590d\u76d8\u3002<\/p>\n<p>Shape \u662f\u4e2a\u7eaf\u63a5\u53e3\u7c7b&#xff0c;area \u548c perimeter \u91cc\u76f4\u63a5 raise NotImplementedError\u3002\u8fd9\u4e48\u5199\u7684\u610f\u601d\u662f&#034;\u5b50\u7c7b\u5fc5\u987b\u81ea\u5df1\u5b9e\u73b0&#xff0c;\u5fd8\u4e86\u5c31\u5f53\u573a\u62a5\u9519&#034;&#xff0c;\u6bd4\u7559\u4e2a\u7a7a\u51fd\u6570\u8fd4\u56de 0 \u5f3a\u5f97\u591a&#xff0c;\u8fd4\u56de 0 \u7684\u8bdd\u7b97\u51fa\u6765\u7684\u603b\u9762\u79ef\u4f1a\u6084\u6084\u5c11\u4e00\u5757\u3002<\/p>\n<p>total_area \u548c\u6392\u5e8f\u90a3\u4e24\u5904\u662f\u5168\u7bc7\u6700\u503c\u94b1\u7684\u5730\u65b9\u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u4ece\u5934\u5230\u5c3e\u6ca1\u5199\u8fc7 isinstance&#xff0c;\u5b83\u4eec\u53ea\u5173\u5fc3\u5bf9\u8c61\u6709\u6ca1\u6709 area\u3002\u5c06\u6765\u52a0\u4e2a Ellipse \u7c7b&#xff0c;\u53ea\u8981\u5b9e\u73b0\u4e86 area&#xff0c;\u76f4\u63a5\u585e\u8fdb\u5217\u8868\u5c31\u80fd\u7b97&#xff0c;\u8fd9\u4e24\u884c\u4ee3\u7801\u4e00\u4e2a\u5b57\u90fd\u4e0d\u7528\u6539\u3002\u8fd9\u5c31\u662f\u591a\u6001\u3002<\/p>\n<p>Triangle \u7684 area \u7528\u4e86\u6d77\u4f26\u516c\u5f0f&#xff0c;\u5185\u90e8\u8c03\u4e86 self.perimeter()\u3002\u5b50\u7c7b\u5185\u90e8\u8c03\u7528\u81ea\u5df1\u7684\u65b9\u6cd5\u7167\u6837\u8d70\u591a\u6001&#xff0c;\u5c06\u6765\u6709\u4eba\u7ee7\u627f Triangle \u6539\u4e86\u5468\u957f\u7b97\u6cd5&#xff0c;\u9762\u79ef\u4f1a\u81ea\u52a8\u8ddf\u7740\u53d8\u3002<\/p>\n<p>\u6821\u9a8c\u653e\u5728 __init__ \u91cc\u800c\u4e0d\u662f area \u91cc&#xff0c;\u7406\u7531\u5f88\u5b9e\u5728&#xff0c;\u8fb9\u957f\u4e0d\u5408\u6cd5\u7684\u5bf9\u8c61\u538b\u6839\u4e0d\u8be5\u88ab\u9020\u51fa\u6765&#xff0c;\u65e9\u62a5\u9519\u6bd4\u665a\u62a5\u9519\u597d\u67e5\u3002<\/p>\n<h4>5.4 \u5b9e\u6218\u56db&#xff0c;\u5b66\u751f\u7ba1\u7406\u7cfb\u7edf&#xff0c;\u628a\u51fd\u6570\u7248\u91cd\u5199\u6210\u7c7b<\/h4>\n<p>\u7b2c\u4e00\u7ae0\u5230\u7b2c\u516d\u7ae0\u6211\u4eec\u4e00\u76f4\u5728\u7528\u5217\u8868\u88c5\u5143\u7ec4\u5904\u7406\u5b66\u751f\u6570\u636e\u3002\u8fd9\u4e00\u8282\u628a\u5b83\u6574\u4e2a\u7ffb\u6210\u7c7b\u3002<\/p>\n<p>\u9700\u6c42\u3002\u5b66\u751f\u6709\u59d3\u540d\u3001\u6027\u522b\u3001\u5206\u6570&#xff0c;\u5206\u6570\u8981\u6821\u9a8c\u3001\u7b49\u7ea7\u8981\u81ea\u52a8\u7b97&#xff1b;\u73ed\u7ea7\u80fd\u52a0\u4eba\u5220\u4eba\u67e5\u4eba&#xff0c;\u80fd\u7ed9\u5e73\u5747\u5206\u3001\u53ca\u683c\u7387\u3001\u6392\u540d\u3001\u7b49\u7ea7\u5206\u7ec4\u3002<\/p>\n<p>\u4ee3\u7801\u3002<\/p>\n<p>import unicodedata<\/p>\n<p>def pad(text, width):<br \/>\n    &#034;&#034;&#034;\u6309\u5b9e\u9645\u663e\u793a\u5bbd\u5ea6\u8865\u7a7a\u683c&#xff0c;\u4e2d\u6587\u7b97\u4e24\u5217&#034;&#034;&#034;<br \/>\n    shown &#061; 0<br \/>\n    for ch in str(text):<br \/>\n        shown &#043;&#061; 2 if unicodedata.east_asian_width(ch) in (&#034;W&#034;, &#034;F&#034;) else 1<br \/>\n    return str(text) &#043; &#034; &#034; * max(0, width &#8211; shown)<\/p>\n<p>class Student:<br \/>\n    &#034;&#034;&#034;\u5b66\u751f&#xff0c;\u5206\u6570\u53d7 property \u4fdd\u62a4&#034;&#034;&#034;<\/p>\n<p>    __slots__ &#061; (&#034;name&#034;, &#034;_score&#034;, &#034;gender&#034;)<\/p>\n<p>    def __init__(self, name, score, gender&#061;&#034;\u672a\u77e5&#034;):<br \/>\n        self.name &#061; name<br \/>\n        self.gender &#061; gender<br \/>\n        self.score &#061; score<\/p>\n<p>    &#064;property<br \/>\n    def score(self):<br \/>\n        return self._score<\/p>\n<p>    &#064;score.setter<br \/>\n    def score(self, value):<br \/>\n        if not isinstance(value, int):<br \/>\n            raise TypeError(&#034;\u5206\u6570\u5fc5\u987b\u662f\u6574\u6570&#xff0c;\u6536\u5230 %r&#034; % value)<br \/>\n        if not 0 &lt;&#061; value &lt;&#061; 100:<br \/>\n            raise ValueError(&#034;\u5206\u6570\u8981\u5728 0 \u5230 100 \u4e4b\u95f4&#xff0c;\u6536\u5230 %d&#034; % value)<br \/>\n        self._score &#061; value<\/p>\n<p>    &#064;property<br \/>\n    def grade(self):<br \/>\n        &#034;&#034;&#034;\u7b49\u7ea7&#xff0c;\u6309\u5206\u6570\u73b0\u7b97&#034;&#034;&#034;<br \/>\n        if self._score &gt;&#061; 90:<br \/>\n            return &#034;A&#034;<br \/>\n        if self._score &gt;&#061; 80:<br \/>\n            return &#034;B&#034;<br \/>\n        if self._score &gt;&#061; 60:<br \/>\n            return &#034;C&#034;<br \/>\n        return &#034;D&#034;<\/p>\n<p>    &#064;property<br \/>\n    def passed(self):<br \/>\n        return self._score &gt;&#061; 60<\/p>\n<p>    def __str__(self):<br \/>\n        return &#034;%s&#xff08;%s&#xff09;%d \u5206 %s \u7b49&#034; % (self.name, self.gender, self._score, self.grade)<\/p>\n<p>    __repr__ &#061; __str__<\/p>\n<p>class ClassRoom:<br \/>\n    &#034;&#034;&#034;\u73ed\u7ea7&#xff0c;\u88c5\u4e00\u5806 Student&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, title):<br \/>\n        self.title &#061; title<br \/>\n        self.__students &#061; []<\/p>\n<p>    def add(self, student):<br \/>\n        if not isinstance(student, Student):<br \/>\n            raise TypeError(&#034;\u53ea\u80fd\u52a0 Student \u5bf9\u8c61&#xff0c;\u6536\u5230 %s&#034; % type(student).__name__)<br \/>\n        self.__students.append(student)<\/p>\n<p>    def find(self, name):<br \/>\n        for stu in self.__students:<br \/>\n            if stu.name &#061;&#061; name:<br \/>\n                return stu<br \/>\n        return None<\/p>\n<p>    def remove(self, name):<br \/>\n        stu &#061; self.find(name)<br \/>\n        if stu is None:<br \/>\n            return False<br \/>\n        self.__students.remove(stu)<br \/>\n        return True<\/p>\n<p>    &#064;property<br \/>\n    def students(self):<br \/>\n        return tuple(self.__students)<\/p>\n<p>    def __len__(self):<br \/>\n        return len(self.__students)<\/p>\n<p>    def __iter__(self):<br \/>\n        return iter(self.__students)<\/p>\n<p>    &#064;property<br \/>\n    def average(self):<br \/>\n        if not self.__students:<br \/>\n            return 0.0<br \/>\n        return round(sum(s.score for s in self.__students) \/ len(self.__students), 1)<\/p>\n<p>    &#064;property<br \/>\n    def pass_rate(self):<br \/>\n        if not self.__students:<br \/>\n            return 0.0<br \/>\n        n &#061; sum(1 for s in self.__students if s.passed)<br \/>\n        return round(n \/ len(self.__students) * 100, 1)<\/p>\n<p>    def ranking(self):<br \/>\n        &#034;&#034;&#034;\u6309\u5206\u6570\u4ece\u9ad8\u5230\u4f4e\u6392&#034;&#034;&#034;<br \/>\n        return sorted(self.__students, key&#061;lambda s: s.score, reverse&#061;True)<\/p>\n<p>    def by_grade(self):<br \/>\n        &#034;&#034;&#034;\u6309\u7b49\u7ea7\u5206\u7ec4&#034;&#034;&#034;<br \/>\n        groups &#061; {}<br \/>\n        for stu in self.__students:<br \/>\n            groups.setdefault(stu.grade, []).append(stu)<br \/>\n        return groups<\/p>\n<p>    def report(self):<br \/>\n        &#034;&#034;&#034;\u6253\u5370\u6210\u7ee9\u5355&#034;&#034;&#034;<br \/>\n        print(&#034;&#061;&#034; * 54)<br \/>\n        print(&#034;%s  \u5171 %d \u4eba  \u5e73\u5747\u5206 %.1f  \u53ca\u683c\u7387 %.1f%%&#034;<br \/>\n              % (self.title, len(self), self.average, self.pass_rate))<br \/>\n        print(&#034;&#061;&#034; * 54)<br \/>\n        print(pad(&#034;\u59d3\u540d&#034;, 10) &#043; pad(&#034;\u6027\u522b&#034;, 8) &#043; pad(&#034;\u5206\u6570&#034;, 8) &#043; &#034;\u7b49\u7ea7&#034;)<br \/>\n        print(&#034;-&#034; * 54)<br \/>\n        for stu in self.ranking():<br \/>\n            print(pad(stu.name, 10) &#043; pad(stu.gender, 8)<br \/>\n                  &#043; pad(str(stu.score), 8) &#043; stu.grade)<br \/>\n        print(&#034;-&#034; * 54)<br \/>\n        for grade in sorted(self.by_grade()):<br \/>\n            members &#061; self.by_grade()[grade]<br \/>\n            print(&#034;%s \u7b49 %d \u4eba  %s&#034; % (grade, len(members),<br \/>\n                                    &#034;\u3001&#034;.join(s.name for s in members)))<br \/>\n        print(&#034;&#061;&#034; * 54)<\/p>\n<p>klass &#061; ClassRoom(&#034;\u9ad8\u4e8c\u4e09\u73ed&#034;)<br \/>\nfor name, score, gender in [<br \/>\n    (&#034;\u5f20\u4e09&#034;, 88, &#034;\u7537&#034;),<br \/>\n    (&#034;\u674e\u56db&#034;, 95, &#034;\u5973&#034;),<br \/>\n    (&#034;\u738b\u4e94&#034;, 72, &#034;\u7537&#034;),<br \/>\n    (&#034;\u8d75\u516d&#034;, 58, &#034;\u5973&#034;),<br \/>\n    (&#034;\u5b59\u4e03&#034;, 90, &#034;\u7537&#034;),<br \/>\n    (&#034;\u5468\u516b&#034;, 66, &#034;\u5973&#034;),<br \/>\n]:<br \/>\n    klass.add(Student(name, score, gender))<\/p>\n<p>klass.report()<\/p>\n<p>print(&#034;\\\\n\u76f4\u63a5\u904d\u5386\u5bf9\u8c61&#xff0c;\u56e0\u4e3a\u5b9e\u73b0\u4e86 __iter__&#034;)<br \/>\nfor stu in klass:<br \/>\n    print(&#034; &#034;, stu)<\/p>\n<p>print(&#034;\\\\n\u627e\u4eba&#034;)<br \/>\nstu &#061; klass.find(&#034;\u738b\u4e94&#034;)<br \/>\nprint(&#034; &#034;, stu)<br \/>\nprint(&#034;  \u7ed9\u4ed6\u52a0 5 \u5206&#034;)<br \/>\nstu.score &#061; 77<br \/>\nprint(&#034; &#034;, stu, &#034;\u7b49\u7ea7&#034;, stu.grade)<\/p>\n<p>print(&#034;\\\\n\u5206\u6570\u6821\u9a8c\u62e6\u5f97\u4f4f\u810f\u6570\u636e&#034;)<br \/>\nfor bad in [105, -3, &#034;\u4e5d\u5341&#034;]:<br \/>\n    try:<br \/>\n        stu.score &#061; bad<br \/>\n    except (ValueError, TypeError) as e:<br \/>\n        print(&#034;  %s: %s&#034; % (type(e).__name__, e))<\/p>\n<p>print(&#034;\\\\n\u8f6c\u5b66\u8d70\u4e00\u4e2a&#034;)<br \/>\nklass.remove(&#034;\u8d75\u516d&#034;)<br \/>\nprint(&#034;  \u73b0\u5728 %d \u4eba&#xff0c;\u5e73\u5747\u5206 %.1f&#xff0c;\u53ca\u683c\u7387 %.1f%%&#034;<br \/>\n      % (len(klass), klass.average, klass.pass_rate))<\/p>\n<p>print(&#034;\\\\n\u52a0\u9519\u7c7b\u578b\u4f1a\u88ab\u62e6\u4e0b&#034;)<br \/>\ntry:<br \/>\n    klass.add((&#034;\u94b1\u4e5d&#034;, 80))<br \/>\nexcept TypeError as e:<br \/>\n    print(&#034;  TypeError:&#034;, e)<\/p>\n<p>\u8f93\u51fa\u3002<\/p>\n<p>&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n\u9ad8\u4e8c\u4e09\u73ed  \u5171 6 \u4eba  \u5e73\u5747\u5206 78.2  \u53ca\u683c\u7387 83.3%<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n\u59d3\u540d      \u6027\u522b    \u5206\u6570    \u7b49\u7ea7<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n\u674e\u56db      \u5973      95      A<br \/>\n\u5b59\u4e03      \u7537      90      A<br \/>\n\u5f20\u4e09      \u7537      88      B<br \/>\n\u738b\u4e94      \u7537      72      C<br \/>\n\u5468\u516b      \u5973      66      C<br \/>\n\u8d75\u516d      \u5973      58      D<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nA \u7b49 2 \u4eba  \u674e\u56db\u3001\u5b59\u4e03<br \/>\nB \u7b49 1 \u4eba  \u5f20\u4e09<br \/>\nC \u7b49 2 \u4eba  \u738b\u4e94\u3001\u5468\u516b<br \/>\nD \u7b49 1 \u4eba  \u8d75\u516d<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<\/p>\n<p>\u76f4\u63a5\u904d\u5386\u5bf9\u8c61&#xff0c;\u56e0\u4e3a\u5b9e\u73b0\u4e86 __iter__<br \/>\n  \u5f20\u4e09&#xff08;\u7537&#xff09;88 \u5206 B \u7b49<br \/>\n  \u674e\u56db&#xff08;\u5973&#xff09;95 \u5206 A \u7b49<br \/>\n  \u738b\u4e94&#xff08;\u7537&#xff09;72 \u5206 C \u7b49<br \/>\n  \u8d75\u516d&#xff08;\u5973&#xff09;58 \u5206 D \u7b49<br \/>\n  \u5b59\u4e03&#xff08;\u7537&#xff09;90 \u5206 A \u7b49<br \/>\n  \u5468\u516b&#xff08;\u5973&#xff09;66 \u5206 C \u7b49<\/p>\n<p>\u627e\u4eba<br \/>\n  \u738b\u4e94&#xff08;\u7537&#xff09;72 \u5206 C \u7b49<br \/>\n  \u7ed9\u4ed6\u52a0 5 \u5206<br \/>\n  \u738b\u4e94&#xff08;\u7537&#xff09;77 \u5206 C \u7b49 \u7b49\u7ea7 C<\/p>\n<p>\u5206\u6570\u6821\u9a8c\u62e6\u5f97\u4f4f\u810f\u6570\u636e<br \/>\n  ValueError: \u5206\u6570\u8981\u5728 0 \u5230 100 \u4e4b\u95f4&#xff0c;\u6536\u5230 105<br \/>\n  ValueError: \u5206\u6570\u8981\u5728 0 \u5230 100 \u4e4b\u95f4&#xff0c;\u6536\u5230 -3<br \/>\n  TypeError: \u5206\u6570\u5fc5\u987b\u662f\u6574\u6570&#xff0c;\u6536\u5230 &#039;\u4e5d\u5341&#039;<\/p>\n<p>\u8f6c\u5b66\u8d70\u4e00\u4e2a<br \/>\n  \u73b0\u5728 5 \u4eba&#xff0c;\u5e73\u5747\u5206 83.2&#xff0c;\u53ca\u683c\u7387 100.0%<\/p>\n<p>\u52a0\u9519\u7c7b\u578b\u4f1a\u88ab\u62e6\u4e0b<br \/>\n  TypeError: \u53ea\u80fd\u52a0 Student \u5bf9\u8c61&#xff0c;\u6536\u5230 tuple<\/p>\n<p>\u590d\u76d8\u3002<\/p>\n<p>\u5bf9\u6bd4\u4e00\u4e0b\u5f00\u5934\u90a3\u4e2a\u7528\u5143\u7ec4\u7684\u7248\u672c\u3002\u5b57\u6bb5\u4ece row[0] row[1] \u53d8\u6210\u4e86 stu.name stu.score&#xff0c;\u542b\u4e49\u5199\u5728\u4ee3\u7801\u91cc&#xff0c;\u4e0d\u9760\u7ea6\u5b9a\u3002<\/p>\n<p>\u5206\u6570\u6821\u9a8c\u8fd9\u6b21\u662f\u786c\u7684\u3002Student(&#034;\u5f20\u4e09&#034;, 999) \u5728\u6784\u9020\u65f6\u5c31\u4f1a\u70b8&#xff0c;\u810f\u6570\u636e\u8fdb\u4e0d\u4e86\u7cfb\u7edf\u3002\u800c\u5143\u7ec4\u7248\u672c\u91cc&#xff0c;999 \u5206\u80fd\u4e00\u8def\u8d70\u5230\u6253\u5370\u73af\u8282&#xff0c;\u6700\u540e\u4ee5&#034;\u5e73\u5747\u5206 1200&#034;\u8fd9\u79cd\u8352\u8c2c\u6570\u5b57\u51fa\u73b0\u5728\u62a5\u8868\u4e0a\u3002<\/p>\n<p>grade \u548c passed \u90fd\u662f\u73b0\u7b97\u7684 property&#xff0c;\u6539\u4e86\u5206\u6570\u7b49\u7ea7\u7acb\u523b\u53d8&#xff0c;\u4e0d\u5b58\u5728&#034;\u5206\u6570\u6539\u4e86\u7b49\u7ea7\u6ca1\u540c\u6b65&#034;\u8fd9\u79cd\u4e8b\u3002<\/p>\n<p>__len__ \u548c __iter__ \u8fd9\u4e24\u4e2a\u7279\u6b8a\u65b9\u6cd5\u8ba9 ClassRoom \u7528\u8d77\u6765\u50cf\u4e2a\u771f\u7684\u5bb9\u5668&#xff0c;len(klass)\u3001for stu in klass \u76f4\u63a5\u5c31\u80fd\u5199\u3002\u521a\u5b66\u7684\u65f6\u5019\u89c9\u5f97\u8fd9\u4fe9\u6ca1\u7528&#xff0c;\u5199\u5230\u7b2c\u4e8c\u4e2a\u7ba1\u7406\u7c7b\u5c31\u77e5\u9053\u6709\u591a\u7701\u4e8b\u3002<\/p>\n<p>add \u91cc\u90a3\u53e5\u7c7b\u578b\u68c0\u67e5\u770b\u7740\u591a\u4f59&#xff0c;\u5b9e\u9645\u6551\u8fc7\u6211\u3002\u66fe\u7ecf\u5f80\u73ed\u7ea7\u91cc\u585e\u4e86\u4e2a\u5b57\u5178&#xff0c;\u62a5\u9519\u62a5\u5728\u6392\u5e8f\u90a3\u4e00\u884c&#xff0c;\u9519\u8bef\u4fe1\u606f\u662f &#039;&gt;&#039; not supported between instances of &#039;dict&#039; and &#039;Student&#039;&#xff0c;\u4ece\u8fd9\u884c\u5f80\u4e0a\u627e\u4e86\u5341\u5206\u949f\u624d\u5b9a\u4f4d\u5230\u662f\u52a0\u4eba\u7684\u65f6\u5019\u585e\u9519\u4e86\u3002\u73b0\u5728\u76f4\u63a5\u62e6\u5728\u5165\u53e3&#xff0c;\u62a5\u9519\u4fe1\u606f\u6e05\u695a\u660e\u767d\u3002<\/p>\n<hr \/>\n<h3>6. \u5c0f\u7ed3<\/h3>\n<h4>6.1 \u77e5\u8bc6\u6e05\u5355<\/h4>\n<p>\u9762\u5411\u5bf9\u8c61\u6982\u8ff0<\/p>\n<ul>\n<li>\u9762\u5411\u8fc7\u7a0b\u6309\u6b65\u9aa4\u62c6&#xff0c;\u9762\u5411\u5bf9\u8c61\u6309&#034;\u4e1c\u897f&#034;\u62c6<\/li>\n<li>\u7c7b\u662f\u56fe\u7eb8&#xff0c;\u5bf9\u8c61\u662f\u6309\u56fe\u7eb8\u9020\u51fa\u6765\u7684\u5b9e\u4f53&#xff0c;\u5b9e\u4f8b\u5c31\u662f\u5bf9\u8c61<\/li>\n<li>\u4e09\u5927\u7279\u5f81&#xff0c;\u5c01\u88c5\u7ba1\u6570\u636e\u5b89\u5168\u3001\u7ee7\u627f\u7ba1\u590d\u7528\u3001\u591a\u6001\u7ba1\u7edf\u4e00\u8c03\u7528<\/li>\n<li>\u522b\u4e3a\u4e86\u7528\u7c7b\u800c\u7528\u7c7b&#xff0c;\u51e0\u5341\u884c\u7684\u4e00\u6b21\u6027\u811a\u672c\u7528\u51fd\u6570\u5c31\u591f\u4e86<\/li>\n<\/ul>\n<p>\u7c7b\u7684\u5b9a\u4e49\u548c\u4f7f\u7528<\/p>\n<ul>\n<li>class \u7c7b\u540d: \u5b9a\u4e49&#xff0c;\u7c7b\u540d\u7528\u5927\u9a7c\u5cf0<\/li>\n<li>__init__ \u662f\u521d\u59cb\u5316&#xff0c;__new__ \u624d\u662f\u521b\u5efa<\/li>\n<li>self \u662f\u5bf9\u8c61\u81ea\u5df1&#xff0c;Python \u81ea\u52a8\u4f20\u5165&#xff0c;\u6f0f\u5199\u4f1a\u62a5 takes 0 positional arguments but 1 was given<\/li>\n<li>\u7c7b\u5c5e\u6027\u5168\u7c7b\u5171\u7528&#xff0c;\u5b9e\u4f8b\u5c5e\u6027\u5404\u5bf9\u8c61\u4e00\u4efd&#xff1b;\u53ef\u53d8\u5bf9\u8c61\u5f53\u7c7b\u5c5e\u6027\u4f1a\u88ab\u6240\u6709\u5bf9\u8c61\u5171\u4eab<\/li>\n<li>__str__ \u9762\u5411\u7528\u6237&#xff0c;__repr__ \u9762\u5411\u5f00\u53d1\u8005<\/li>\n<li>__slots__ \u80fd\u9650\u5236\u5c5e\u6027\u3001\u7701\u5185\u5b58<\/li>\n<li>\u53cc\u4e0b\u5212\u7ebf\u662f\u540d\u5b57\u6539\u5199\u4e0d\u662f\u771f\u79c1\u6709&#xff0c;\u5355\u4e0b\u5212\u7ebf\u662f\u7ea6\u5b9a<\/li>\n<li>&#064;staticmethod \u4e0d\u6536 self&#xff0c;&#064;classmethod \u6536 cls \u5e38\u7528\u6765\u505a\u5de5\u5382\u65b9\u6cd5<\/li>\n<\/ul>\n<p>\u5c5e\u6027 property<\/p>\n<ul>\n<li>&#064;property \u88c5\u9970\u8bfb&#xff0c;&#064;x.setter \u88c5\u9970\u5199&#xff0c;&#064;x.deleter \u88c5\u9970\u5220<\/li>\n<li>\u6700\u5927\u7684\u7528\u5904\u662f\u628a\u88f8\u5c5e\u6027\u5347\u7ea7\u6210\u53d7\u63a7\u5c5e\u6027&#xff0c;\u8c03\u7528\u65b9\u4e0d\u7528\u6539<\/li>\n<li>\u53ea\u5199 getter \u5c31\u662f\u53ea\u8bfb\u5c5e\u6027<\/li>\n<li>\u5c5e\u6027\u540d\u548c\u5185\u90e8\u53d8\u91cf\u540d\u4e0d\u80fd\u540c\u540d&#xff0c;\u540c\u540d\u4f1a\u65e0\u9650\u9012\u5f52<\/li>\n<\/ul>\n<p>\u7ee7\u627f<\/p>\n<ul>\n<li>class \u5b50\u7c7b(\u7236\u7c7b)&#xff0c;\u5b50\u7c7b\u81ea\u52a8\u62e5\u6709\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/li>\n<li>\u91cd\u5199\u5c31\u662f\u5b50\u7c7b\u5199\u4e2a\u540c\u540d\u65b9\u6cd5\u76d6\u4f4f\u7236\u7c7b\u7684<\/li>\n<li>super() \u8c03\u7236\u7c7b\u65b9\u6cd5&#xff0c;\u5b50\u7c7b\u5199\u4e86 __init__ \u5c31\u5fc5\u987b super().__init__()<\/li>\n<li>isinstance \u8ba4\u7ee7\u627f\u5173\u7cfb&#xff0c;type(x) is C \u4e0d\u8ba4<\/li>\n<li>MRO \u51b3\u5b9a\u591a\u7ee7\u627f\u7684\u67e5\u627e\u987a\u5e8f&#xff0c;__mro__ \u80fd\u67e5<\/li>\n<li>\u5224\u65ad\u8be5\u4e0d\u8be5\u7ee7\u627f&#xff0c;\u770b\u662f&#034;\u662f\u4e00\u79cd&#034;\u8fd8\u662f&#034;\u6709\u4e00\u4e2a&#034;<\/li>\n<\/ul>\n<h4>6.2 \u907f\u5751\u6e05\u5355<\/h4>\n<table>\n<tr>\u6392\u540d\u5751\u75c7\u72b6<\/tr>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>\u53ef\u53d8\u5bf9\u8c61\u5f53\u7c7b\u5c5e\u6027<\/td>\n<td>\u6539\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u5176\u4ed6\u5bf9\u8c61\u8ddf\u7740\u53d8<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>property \u7684 getter\/setter \u91cc\u7528\u4e86\u540c\u540d\u5c5e\u6027<\/td>\n<td>RecursionError<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>\u5b50\u7c7b __init__ \u5fd8\u4e86 super()<\/td>\n<td>\u7236\u7c7b\u5c5e\u6027\u8bbf\u95ee\u4e0d\u5230<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>\u65b9\u6cd5\u6f0f\u5199 self<\/td>\n<td>takes 0 positional arguments but 1 was given<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>\u62ff\u7c7b\u540d\u8bbf\u95ee\u5b9e\u4f8b\u5c5e\u6027<\/td>\n<td>AttributeError: type object<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>\u8ba1\u6570\u5668\u5199\u6210\u4e86 self.total &#043;&#061; 1<\/td>\n<td>\u7c7b\u5c5e\u6027\u6c38\u8fdc\u662f 0<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>\u62ff\u5bf9\u8c61\u76f4\u63a5\u8c03\u7528\u5374\u671f\u671b\u6253\u5370\u597d\u770b<\/td>\n<td>\u6ca1\u5199 __str__&#xff0c;\u6253\u51fa\u4e00\u4e32\u5730\u5740<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>\u7ee7\u627f\u5c42\u7ea7\u592a\u6df1<\/td>\n<td>\u627e\u4e2a\u65b9\u6cd5\u8981\u7ffb\u56db\u5c42\u7236\u7c7b<\/td>\n<\/tr>\n<tr>\n<td>9<\/td>\n<td>\u591a\u7ee7\u627f\u6f0f\u5199 super()<\/td>\n<td>\u94fe\u6761\u65ad\u4e86&#xff0c;\u4e0d\u62a5\u9519\u53ea\u662f\u5c11\u5e72\u6d3b<\/td>\n<\/tr>\n<tr>\n<td>10<\/td>\n<td>\u62fc\u9519\u5c5e\u6027\u540d<\/td>\n<td>\u4e0d\u62a5\u9519&#xff0c;\u51ed\u7a7a\u591a\u4e00\u4e2a\u5c5e\u6027<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u524d\u4e24\u6761\u662f\u8fd9\u4e00\u7ae0\u6700\u8be5\u8bb0\u4f4f\u7684\u3002\u7b2c\u4e00\u6761\u8ddf\u7b2c\u516d\u7ae0\u9ed8\u8ba4\u53c2\u6570\u7684\u5751\u662f\u540c\u4e00\u4e2a\u75c5\u56e0&#xff0c;\u7b2c\u4e8c\u6761\u6211\u5230\u73b0\u5728\u5076\u5c14\u8fd8\u4f1a\u624b\u6ed1\u5199\u51fa\u3002<\/p>\n<h4>6.3 \u4e94\u9053\u9898<\/h4>\n<p>\u7b2c\u4e00\u9898&#xff0c;\u5199\u4e00\u4e2a Rectangle \u7c7b&#xff0c;\u6709\u5bbd\u548c\u9ad8&#xff0c;\u7528 property \u63d0\u4f9b\u53ea\u8bfb\u7684 area \u548c perimeter&#xff0c;\u957f\u5bbd\u8d4b\u503c\u65f6\u6821\u9a8c\u5fc5\u987b\u5927\u4e8e 0\u3002<\/p>\n<p>\u7b2c\u4e8c\u9898&#xff0c;\u5199\u4e00\u4e2a Counter \u7c7b&#xff0c;\u7528\u7c7b\u5c5e\u6027\u7edf\u8ba1\u4e00\u5171\u521b\u5efa\u4e86\u591a\u5c11\u4e2a\u5b9e\u4f8b&#xff0c;\u5b9e\u4f8b\u65b9\u6cd5 hit() \u6bcf\u6b21\u8c03\u7528\u628a\u81ea\u5df1\u7684\u8ba1\u6570\u52a0\u4e00\u3002<\/p>\n<p>\u7b2c\u4e09\u9898&#xff0c;\u5199 Animal\u3001Dog\u3001Cat \u4e09\u4e2a\u7c7b&#xff0c;Dog \u548c Cat \u91cd\u5199 speak()&#xff0c;\u518d\u5199\u4e2a\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u52a8\u7269\u5217\u8868&#xff0c;\u4f9d\u6b21\u6253\u5370\u5b83\u4eec\u7684\u53eb\u58f0&#xff0c;\u51fd\u6570\u91cc\u4e0d\u8bb8\u51fa\u73b0 isinstance\u3002<\/p>\n<p>\u7b2c\u56db\u9898&#xff0c;\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u6709\u4ec0\u4e48\u95ee\u9898&#xff0c;\u600e\u4e48\u6539&#xff1f;<\/p>\n<p>class Temperature:<br \/>\n    def __init__(self, celsius):<br \/>\n        self.celsius &#061; celsius<\/p>\n<p>    &#064;property<br \/>\n    def celsius(self):<br \/>\n        return self.celsius<\/p>\n<p>    &#064;celsius.setter<br \/>\n    def celsius(self, value):<br \/>\n        self.celsius &#061; value<\/p>\n<p>\u7b2c\u4e94\u9898&#xff0c;\u5199\u4e00\u4e2a Stack \u7c7b&#xff0c;\u5b9e\u73b0 push\u3001pop\u3001is_empty&#xff0c;\u518d\u52a0 __len__ \u548c __str__&#xff0c;\u8ba9\u5b83\u80fd len(s) \u548c print(s)\u3002<\/p>\n<p>\u53c2\u8003\u7b54\u6848<\/p>\n<p>\u7b2c\u4e00\u9898&#xff0c;\u957f\u5bbd\u6821\u9a8c\u5199\u5728 setter \u91cc&#xff0c;\u9762\u79ef\u5468\u957f\u53ea\u8bfb\u3002<\/p>\n<p>&gt;&gt;&gt; class Rectangle:<br \/>\n&#8230;     def __init__(self, width, height):<br \/>\n&#8230;         self.width &#061; width<br \/>\n&#8230;         self.height &#061; height<br \/>\n&#8230;     &#064;property<br \/>\n&#8230;     def width(self):<br \/>\n&#8230;         return self._width<br \/>\n&#8230;     &#064;width.setter<br \/>\n&#8230;     def width(self, value):<br \/>\n&#8230;         if value &lt;&#061; 0:<br \/>\n&#8230;             raise ValueError(&#034;\u5bbd\u5fc5\u987b\u5927\u4e8e 0&#034;)<br \/>\n&#8230;         self._width &#061; value<br \/>\n&#8230;     &#064;property<br \/>\n&#8230;     def height(self):<br \/>\n&#8230;         return self._height<br \/>\n&#8230;     &#064;height.setter<br \/>\n&#8230;     def height(self, value):<br \/>\n&#8230;         if value &lt;&#061; 0:<br \/>\n&#8230;             raise ValueError(&#034;\u9ad8\u5fc5\u987b\u5927\u4e8e 0&#034;)<br \/>\n&#8230;         self._height &#061; value<br \/>\n&#8230;     &#064;property<br \/>\n&#8230;     def area(self):<br \/>\n&#8230;         return self._width * self._height<br \/>\n&#8230;     &#064;property<br \/>\n&#8230;     def perimeter(self):<br \/>\n&#8230;         return 2 * (self._width &#043; self._height)<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; r &#061; Rectangle(3, 4)<br \/>\n&gt;&gt;&gt; print(r.area, r.perimeter)<br \/>\n12 14<br \/>\n&gt;&gt;&gt; r.width &#061; 5<br \/>\n&gt;&gt;&gt; r.area<br \/>\n20<br \/>\n&gt;&gt;&gt; Rectangle(0, 4)<br \/>\nValueError: \u5bbd\u5fc5\u987b\u5927\u4e8e 0<\/p>\n<p>\u7b2c\u4e8c\u9898&#xff0c;\u7c7b\u5c5e\u6027\u914d\u7c7b\u540d\u81ea\u589e\u3002<\/p>\n<p>&gt;&gt;&gt; class Counter:<br \/>\n&#8230;     total &#061; 0<br \/>\n&#8230;     def __init__(self, name):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;         self.count &#061; 0<br \/>\n&#8230;         Counter.total &#043;&#061; 1<br \/>\n&#8230;     def hit(self):<br \/>\n&#8230;         self.count &#043;&#061; 1<br \/>\n&#8230;         return self.count<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; a &#061; Counter(&#034;a&#034;)<br \/>\n&gt;&gt;&gt; b &#061; Counter(&#034;b&#034;)<br \/>\n&gt;&gt;&gt; a.hit()<br \/>\n1<br \/>\n&gt;&gt;&gt; a.hit()<br \/>\n2<br \/>\n&gt;&gt;&gt; b.hit()<br \/>\n1<br \/>\n&gt;&gt;&gt; Counter.total<br \/>\n2<\/p>\n<p>\u7b2c\u4e09\u9898&#xff0c;\u591a\u6001&#xff0c;\u51fd\u6570\u91cc\u53ea\u6709 speak()\u3002<\/p>\n<p>&gt;&gt;&gt; class Animal:<br \/>\n&#8230;     def __init__(self, name):<br \/>\n&#8230;         self.name &#061; name<br \/>\n&#8230;     def speak(self):<br \/>\n&#8230;         return &#034;\u2026\u2026&#034;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class Dog(Animal):<br \/>\n&#8230;     def speak(self):<br \/>\n&#8230;         return self.name &#043; &#034;&#xff1a;\u6c6a\u6c6a&#034;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; class Cat(Animal):<br \/>\n&#8230;     def speak(self):<br \/>\n&#8230;         return self.name &#043; &#034;&#xff1a;\u55b5\u55b5&#034;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; def chorus(animals):<br \/>\n&#8230;     for one in animals:<br \/>\n&#8230;         print(one.speak())<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; chorus([Dog(&#034;\u5c0f\u9ed1&#034;), Cat(&#034;\u5c0f\u767d&#034;)])<br \/>\n\u5c0f\u9ed1&#xff1a;\u6c6a\u6c6a<br \/>\n\u5c0f\u767d&#xff1a;\u55b5\u55b5<\/p>\n<p>\u7b2c\u56db\u9898&#xff0c;celsius \u5c5e\u6027\u548c\u5185\u90e8\u53d8\u91cf\u540c\u540d\u4e86\u3002\u8bfb self.celsius \u4f1a\u53bb\u8c03 getter&#xff0c;getter \u53c8\u8bfb self.celsius&#xff0c;\u65e0\u9650\u9012\u5f52\u3002\u6539\u6cd5\u662f\u628a\u5185\u90e8\u53d8\u91cf\u6362\u6210 _celsius\u3002<\/p>\n<p>&gt;&gt;&gt; class Temperature:<br \/>\n&#8230;     def __init__(self, celsius):<br \/>\n&#8230;         self.celsius &#061; celsius<br \/>\n&#8230;     &#064;property<br \/>\n&#8230;     def celsius(self):<br \/>\n&#8230;         return self._celsius<br \/>\n&#8230;     &#064;celsius.setter<br \/>\n&#8230;     def celsius(self, value):<br \/>\n&#8230;         self._celsius &#061; value<br \/>\n&#8230;     &#064;property<br \/>\n&#8230;     def fahrenheit(self):<br \/>\n&#8230;         return self._celsius * 1.8 &#043; 32<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; t &#061; Temperature(100)<br \/>\n&gt;&gt;&gt; print(t.celsius, t.fahrenheit)<br \/>\n100 212.0<\/p>\n<p>\u7b2c\u4e94\u9898&#xff0c;__len__ \u548c __str__ \u8ba9\u5bf9\u8c61\u7528\u8d77\u6765\u50cf\u5185\u7f6e\u5bb9\u5668\u3002<\/p>\n<p>&gt;&gt;&gt; class Stack:<br \/>\n&#8230;     def __init__(self):<br \/>\n&#8230;         self.__items &#061; []<br \/>\n&#8230;     def push(self, item):<br \/>\n&#8230;         self.__items.append(item)<br \/>\n&#8230;     def pop(self):<br \/>\n&#8230;         if not self.__items:<br \/>\n&#8230;             raise IndexError(&#034;\u6808\u7a7a\u4e86&#034;)<br \/>\n&#8230;         return self.__items.pop()<br \/>\n&#8230;     def is_empty(self):<br \/>\n&#8230;         return len(self.__items) &#061;&#061; 0<br \/>\n&#8230;     def __len__(self):<br \/>\n&#8230;         return len(self.__items)<br \/>\n&#8230;     def __str__(self):<br \/>\n&#8230;         return &#034;Stack(%s)&#034; % self.__items<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; s &#061; Stack()<br \/>\n&gt;&gt;&gt; s.is_empty()<br \/>\nTrue<br \/>\n&gt;&gt;&gt; s.push(1)<br \/>\n&gt;&gt;&gt; s.push(2)<br \/>\n&gt;&gt;&gt; print(s)<br \/>\nStack([1, 2])<br \/>\n&gt;&gt;&gt; len(s)<br \/>\n2<br \/>\n&gt;&gt;&gt; s.pop()<br \/>\n2<br \/>\n&gt;&gt;&gt; len(s)<br \/>\n1<\/p>\n<p>\u4e94\u9053\u9898\u90fd\u5199\u5f97\u51fa\u6765&#xff0c;\u7c7b\u7684\u57fa\u672c\u529f\u5c31\u7a33\u4e86\u3002<\/p>\n<h4>6.4 \u4e0b\u4e00\u7ae0\u9884\u544a<\/h4>\n<p>\u8fd9\u4e00\u7ae0\u6211\u4eec\u628a\u6570\u636e\u548c\u64cd\u4f5c\u88c5\u8fdb\u4e86\u7c7b&#xff0c;\u4ee3\u7801\u7ec8\u4e8e\u6709\u4e86\u7ed3\u6784\u3002\u53ef\u7a0b\u5e8f\u8dd1\u8d77\u6765\u603b\u4f1a\u9047\u5230\u610f\u5916&#xff0c;\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u7f51\u7edc\u65ad\u4e86\u3001\u7528\u6237\u8f93\u5165\u4e86\u4e2a\u5b57\u7b26\u4e32\u800c\u4f60\u8981\u6574\u6570\u3002\u73b0\u5728\u6211\u4eec\u7684\u5904\u7406\u65b9\u5f0f\u662f raise ValueError \u4e00\u628a\u629b\u51fa\u53bb&#xff0c;\u7136\u540e\u7a0b\u5e8f\u5c31\u5d29\u4e86\u3002<\/p>\n<p>\u7b2c\u516b\u7ae0\u8bb2\u5f02\u5e38\u5904\u7406&#xff0c;\u6559\u7a0b\u5e8f\u600e\u4e48\u4f18\u96c5\u5730\u5e94\u5bf9\u610f\u5916&#xff0c;try\u3001except\u3001finally\u3001raise \u600e\u4e48\u7528&#xff0c;\u81ea\u5b9a\u4e49\u5f02\u5e38\u4ec0\u4e48\u65f6\u5019\u8be5\u5199&#xff0c;\u4ee5\u53ca\u4e00\u4e2a\u6211\u81f3\u4eca\u8fd8\u5728\u7ea0\u7ed3\u7684\u95ee\u9898&#xff0c;\u5230\u5e95\u8be5\u6355\u83b7\u8fd8\u662f\u8be5\u8ba9\u5b83\u5d29\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b2c\u4e03\u7ae0 \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1\u76ee\u5f55\u7b2c\u4e03\u7ae0  \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1<br \/>\n\u2502<br \/>\n\u251c\u2500\u2500 1. \u9762\u5411\u5bf9\u8c61\u6982\u8ff0     \u2192 \u9762\u5411\u8fc7\u7a0b vs \u9762\u5411\u5bf9\u8c61 \/ \u7c7b\u4e0e\u5bf9\u8c61 \/ \u4e09\u5927\u7279\u5f81<br \/>\n\u251c\u2500\u2500 2. \u7c7b\u7684\u5b9a\u4e49\u548c\u4f7f\u7528   \u2192 class \/ __init__ \/ self \/ \u5b9e\u4f8b\u5c5e\u6027\u4e0e\u7c7b\u5c5e\u6027<br \/>\n\u251c\u2500\u2500 3. \u5c5e\u6027 property    \u2192 \u79c1\u6709\u5c5e\u6027 \/ property \/ setter \/ \u53ea\u8bfb\u5c5e\u6027<br \/>\n\u251c\u2500\u2500 4. \u7ee7\u627f             \u2192 \u5355\u7ee7\u627f \/ \u65b9\u6cd5\u91cd\u5199 \/ super \/ \u591a\u7ee7\u627f\u4e0e MRO<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81],"topic":[],"class_list":["post-115663","post","type-post","status-publish","format-standard","hentry","category-server","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u7b2c\u4e03\u7ae0 \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/115663.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u7b2c\u4e03\u7ae0 \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u7b2c\u4e03\u7ae0 \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1\u76ee\u5f55\u7b2c\u4e03\u7ae0 \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1 \u2502 \u251c\u2500\u2500 1. \u9762\u5411\u5bf9\u8c61\u6982\u8ff0   \u2192 \u9762\u5411\u8fc7\u7a0b vs \u9762\u5411\u5bf9\u8c61 \/ \u7c7b\u4e0e\u5bf9\u8c61 \/ \u4e09\u5927\u7279\u5f81 \u251c\u2500\u2500 2. \u7c7b\u7684\u5b9a\u4e49\u548c\u4f7f\u7528  \u2192 class \/ __init__ \/ self \/ \u5b9e\u4f8b\u5c5e\u6027\u4e0e\u7c7b\u5c5e\u6027 \u251c\u2500\u2500 3. \u5c5e\u6027 property  \u2192 \u79c1\u6709\u5c5e\u6027 \/ property \/ setter \/ \u53ea\u8bfb\u5c5e\u6027 \u251c\u2500\u2500 4. \u7ee7\u627f       \u2192 \u5355\u7ee7\u627f \/ \u65b9\u6cd5\u91cd\u5199 \/ super \/ \u591a\u7ee7\u627f\u4e0e MRO\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/115663.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-12T15:24:49+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"30 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/115663.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/115663.html\",\"name\":\"\u7b2c\u4e03\u7ae0 \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-12T15:24:49+00:00\",\"dateModified\":\"2026-10-12T15:24:49+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/115663.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/115663.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/115663.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u7b2c\u4e03\u7ae0 \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u7b2c\u4e03\u7ae0 \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/115663.html","og_locale":"zh_CN","og_type":"article","og_title":"\u7b2c\u4e03\u7ae0 \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u7b2c\u4e03\u7ae0 \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1\u76ee\u5f55\u7b2c\u4e03\u7ae0 \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1 \u2502 \u251c\u2500\u2500 1. \u9762\u5411\u5bf9\u8c61\u6982\u8ff0   \u2192 \u9762\u5411\u8fc7\u7a0b vs \u9762\u5411\u5bf9\u8c61 \/ \u7c7b\u4e0e\u5bf9\u8c61 \/ \u4e09\u5927\u7279\u5f81 \u251c\u2500\u2500 2. \u7c7b\u7684\u5b9a\u4e49\u548c\u4f7f\u7528  \u2192 class \/ __init__ \/ self \/ \u5b9e\u4f8b\u5c5e\u6027\u4e0e\u7c7b\u5c5e\u6027 \u251c\u2500\u2500 3. \u5c5e\u6027 property  \u2192 \u79c1\u6709\u5c5e\u6027 \/ property \/ setter \/ \u53ea\u8bfb\u5c5e\u6027 \u251c\u2500\u2500 4. \u7ee7\u627f       \u2192 \u5355\u7ee7\u627f \/ \u65b9\u6cd5\u91cd\u5199 \/ super \/ \u591a\u7ee7\u627f\u4e0e MRO","og_url":"https:\/\/www.wsisp.com\/helps\/115663.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-12T15:24:49+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"30 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/115663.html","url":"https:\/\/www.wsisp.com\/helps\/115663.html","name":"\u7b2c\u4e03\u7ae0 \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-12T15:24:49+00:00","dateModified":"2026-10-12T15:24:49+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/115663.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/115663.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/115663.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u7b2c\u4e03\u7ae0 \u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/115663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=115663"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/115663\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=115663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=115663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=115663"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=115663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}