{"id":202870,"date":"2025-05-22T08:21:55","date_gmt":"2025-05-22T00:21:55","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202870\/"},"modified":"2025-05-22T08:21:55","modified_gmt":"2025-05-22T00:21:55","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%96%ad%e8%a8%80%e6%9d%a5%e6%a3%80%e6%b5%8b%e4%bb%a3%e7%a0%81%e4%b8%ad%e7%9a%84%e9%94%99%e8%af%af%e5%b9%b6%e7%94%9f%e6%88%90%e5%bc%82%e5%b8%b8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202870\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u65ad\u8a00\u6765\u68c0\u6d4b\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u5e76\u751f\u6210\u5f02\u5e38\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528\u65ad\u8a00\u6765\u68c0\u6d4b\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u5e76\u751f\u6210\u5f02\u5e38\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5982\u4f55\u4f7f\u7528\u65ad\u8a00\u6765\u68c0\u6d4b\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u5e76\u751f\u6210\u5f02\u5e38\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u4f7f\u7528\u65ad\u8a00\u53ef\u4ee5\u68c0\u6d4b\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u5e76\u751f\u6210\u5f02\u5e38\uff0c\u7528\u4e8e\u9a8c\u8bc1\u5047\u8bbe\u6216\u6761\u4ef6\u662f\u5426\u6210\u7acb\uff0c\u4e0d\u6210\u7acb\u5219\u89e6\u53d1\u5f02\u5e38\uff08AssertionError\uff09\uff0c\u63d0\u4f9b\u65e9\u671f\u9519\u8bef\u68c0\u6d4b\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\uff0c\u4ee5\u53ca\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729953315671cfe2377064.jpg\" class=\"aligncenter\"><\/p>\n<h2>\u4f7f\u7528\u65ad\u8a00\u6765\u68c0\u6d4b\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u5e76\u751f\u6210\u5f02\u5e38<\/h2>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u65ad\u8a00\u662f\u4e00\u79cd\u7f16\u7a0b\u5b9e\u8df5\uff0c\u7528\u4e8e\u9a8c\u8bc1\u4ee3\u7801\u4e2d\u7684\u5047\u8bbe\u6216\u6761\u4ef6\u662f\u5426\u6210\u7acb\u3002\u5982\u679c\u6761\u4ef6\u4e0d\u6210\u7acb\uff0c\u65ad\u8a00\u5c06\u89e6\u53d1\u4e00\u4e2a\u5f02\u5e38\uff0c\u901a\u77e5\u5f00\u53d1\u8005\u5b58\u5728\u6f5c\u5728\u9519\u8bef\u3002<\/p>\n<p><strong>\u4f7f\u7528\u65ad\u8a00<\/strong><\/p>\n<p>\u5728 Python \u4e2d\uff0c<code>assert<\/code> \u8bed\u53e5\u7528\u4e8e\u521b\u5efa\u65ad\u8a00\uff1a<\/p>\n<pre>assert condition<\/pre>\n<p>\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u8be5\u65ad\u8a00\u5c06\u6210\u529f\uff0c\u5e76\u4e14\u4e0d\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u5426\u5219\uff0c\u5b83\u5c06\u5f15\u53d1<code>AssertionError<\/code>\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u9a8c\u8bc1\u8f93\u5165<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u65ad\u8a00\u6765\u9a8c\u8bc1\u51fd\u6570\u8f93\u5165\uff1a<\/p>\n<pre>def get_max_value(numbers):\n    \"\"\"\u8fd4\u56de\u6570\u5b57\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\u3002\"\"\"\n    \n    assert numbers, \"\u6570\u5b57\u5217\u8868\u4e0d\u80fd\u4e3a\u7a7a\u3002\"\n    \n    return max(numbers)<\/pre>\n<p>\u5728\u8c03\u7528\u8be5\u51fd\u6570\u65f6\uff0c\u5c06\u5f15\u53d1 <code>AssertionError<\/code> \u5f02\u5e38\uff0c\u5982\u679c<code>numbers<\/code>\u5217\u8868\u4e3a\u7a7a\u7684\u8bdd\u3002<\/p>\n<p><strong>\u5176\u4ed6\u7c7b\u578b\u65ad\u8a00<\/strong><\/p>\n<p>Python \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u5efa\u65ad\u8a00\uff1a<\/p>\n<ul>\n<li><code>assertTrue<\/code>\uff1a\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u6210\u529f\u3002\u5426\u5219\uff0c\u5f15\u53d1<code>AssertionError<\/code>\u3002<\/li>\n<li><code>assertFalse<\/code>\uff1a\u5982\u679c\u6761\u4ef6\u4e3a\u5047\uff0c\u5219\u6210\u529f\u3002\u5426\u5219\uff0c\u5f15\u53d1<code>AssertionError<\/code>\u3002<\/li>\n<li><code>assertIs<\/code>\uff1a\u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u5f15\u7528\u76f8\u540c\uff0c\u5219\u6210\u529f\u3002\u5426\u5219\uff0c\u5f15\u53d1<code>AssertionError<\/code>\u3002<\/li>\n<li><code>assertIsNone<\/code>\uff1a\u5982\u679c\u5bf9\u8c61\u4e3a <code>None<\/code>\uff0c\u5219\u6210\u529f\u3002\u5426\u5219\uff0c\u5f15\u53d1 <code>AssertionError<\/code>\u3002<\/li>\n<\/ul>\n<p><strong>\u81ea\u5b9a\u4e49\u9519\u8bef\u6d88\u606f<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u63d0\u4f9b\u81ea\u5b9a\u4e49\u9519\u8bef\u6d88\u606f\u4ee5\u63d0\u4f9b\u66f4\u591a\u4e0a\u4e0b\u6587\uff1a<\/p>\n<pre>assert condition, \"\u81ea\u5b9a\u4e49\u9519\u8bef\u6d88\u606f\"<\/pre>\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n<p>\u4f7f\u7528\u65ad\u8a00\u7684\u4f18\u70b9\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u65e9\u671f\u9519\u8bef\u68c0\u6d4b\uff1a<\/strong> \u65ad\u8a00\u5728\u4ee3\u7801\u6267\u884c\u65f6\u7acb\u5373\u68c0\u67e5\u6761\u4ef6\uff0c\u5141\u8bb8\u5728\u5e55\u540e\u5feb\u901f\u8bc6\u522b\u9519\u8bef\u3002<\/li>\n<li><strong>\u4ee3\u7801\u53ef\u8bfb\u6027\uff1a<\/strong> \u65ad\u8a00\u4f7f\u8c03\u8bd5\u66f4\u5bb9\u6613\uff0c\u56e0\u4e3a\u5b83\u4eec\u6e05\u695a\u5730\u8868\u793a\u4e86\u5bf9\u4ee3\u7801\u5047\u8bbe\u7684\u671f\u671b\u3002<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406\uff1a<\/strong> \u65ad\u8a00\u53ef\u4ee5\u89e6\u53d1\u5f02\u5e38\uff0c\u4ece\u800c\u5141\u8bb8\u6267\u884c\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u3002<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528\u65ad\u8a00\u6765\u68c0\u6d4b\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u5e76&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202870","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202870"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202870\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}