{"id":3304,"date":"2025-06-02T15:49:56","date_gmt":"2025-06-04T08:16:55","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3304"},"modified":"2025-06-04T15:49:56","modified_gmt":"2025-06-04T08:16:55","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84bool%e5%87%bd%e6%95%b8-3","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84bool%e5%87%bd%e6%95%b8-3\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 bool() \u51fd\u6578\uff1a\u5f9e\u57fa\u790e\u5230\u5be6\u6230"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, bool(), \u5e03\u6797\u503c, Python \u6559\u5b78\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 bool() \u51fd\u6578<\/h1>\n<p>Python \u7684 `bool()` \u51fd\u6578\u662f\u7528\u4f86\u5c07\u4efb\u610f\u7269\u4ef6\u8f49\u63db\u6210\u5e03\u6797\u503c\uff08`True` \u6216 `False`\uff09\u7684\u5167\u5efa\u51fd\u6578\u3002\u5b83\u5728\u689d\u4ef6\u5224\u65b7\u548c\u908f\u8f2f\u904b\u7b97\u4e2d\u626e\u6f14\u8457\u91cd\u8981\u89d2\u8272\uff0c\u5e6b\u52a9\u6211\u5011\u5224\u65b7\u7269\u4ef6\u7684\u771f\u507d\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u9019\u500b\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\u3001\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u4ee5\u53ca\u5e38\u898b\u7684\u932f\u8aa4\u6392\u9664\u6280\u5de7\uff0c\u8b93\u4f60\u80fd\u5920\u719f\u7df4\u638c\u63e1\u9019\u500b\u57fa\u790e\u77e5\u8b58\u3002<\/p>\n<h2>bool() \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>`bool()` \u51fd\u6578\u63a5\u53d7\u4efb\u4f55\u7269\u4ef6\u4f5c\u70ba\u53c3\u6578\uff0c\u6839\u64da\u5176\u5167\u5bb9\u8fd4\u56de\u5e03\u6797\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u8f49\u63db\u898f\u5247\uff1a<br \/>\n&#8211; \u82e5\u53c3\u6578\u503c\u70ba `0`\u3001\u7a7a\u5b57\u4e32 `&#8221;&#8221;`\u3001\u7a7a\u5217\u8868 `[]`\u3001\u7a7a\u5b57\u5178 `{}`\u3001\u7a7a\u96c6\u5408 `set()` \u6216 `None`\uff0c\u5247\u8fd4\u56de `False`\u3002<br \/>\n&#8211; \u5426\u5247\uff0c\u8fd4\u56de `True`\u3002<\/p>\n<h2>bool() \u51fd\u6578\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u53ef\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3 `bool()` \u51fd\u6578\u7684\u904b\u4f5c\uff1a<\/p>\n<pre class=\"brush: python\">\n# \u5c07\u6574\u65780\u8f49\u63db\u70ba\u5e03\u6797\u503c\nx = 0\nprint(bool(x))  # \u8f38\u51fa: False\n\n# \u5c07\u7a7a\u5b57\u4e32\u8f49\u63db\u70ba\u5e03\u6797\u503c\ny = \"\"\nprint(bool(y))  # \u8f38\u51fa: False\n\n# \u5c07\u5217\u8868\u8f49\u63db\u70ba\u5e03\u6797\u503c\nz = []\nprint(bool(z))  # \u8f38\u51fa: False\n\n# \u5c07\u5b57\u5178\u8f49\u63db\u70ba\u5e03\u6797\u503c\na = {}\nprint(bool(a))  # \u8f38\u51fa: False\n\n# \u5c07None\u8f49\u63db\u70ba\u5e03\u6797\u503c\nb = None\nprint(bool(b))  # \u8f38\u51fa: False\n<\/pre>\n<p>\u57f7\u884c\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\uff0c\u60a8\u5c07\u767c\u73fe\uff0c\u6240\u6709\u7684\u4f8b\u5b50\u90fd\u8fd4\u56de\u4e86 `False`\uff0c\u56e0\u70ba\u9019\u4e9b\u7269\u4ef6\u90fd\u88ab\u8996\u70ba\u300c\u5047\u300d\u3002<\/p>\n<h2>bool() \u51fd\u6578\u7684\u61c9\u7528<\/h2>\n<p>`bool()` \u51fd\u6578\u5728\u689d\u4ef6\u5224\u65b7\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u9032\u884c\u908f\u8f2f\u5224\u65b7\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\nx = 10\nif bool(x):\n    print(\"x is true\")  # \u9019\u884c\u5c07\u88ab\u57f7\u884c\nelse:\n    print(\"x is false\")\n<\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u7531\u65bc `x` \u7684\u503c\u70ba `10`\uff0c`bool(x)` \u8fd4\u56de `True`\uff0c\u56e0\u6b64\u689d\u4ef6\u6210\u7acb\uff0c\u6703\u8f38\u51fa `&#8221;x is true&#8221;`\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u6280\u5de7<\/h2>\n<p>\u5728\u4f7f\u7528 `bool()` \u51fd\u6578\u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u554f\u984c\uff1a<br \/>\n&#8211; **\u554f\u984c**\uff1a\u671f\u5f85 `bool([])` \u8fd4\u56de `True`\uff0c\u4f46\u5be6\u969b\u4e0a\u8fd4\u56de\u4e86 `False`\u3002<br \/>\n  &#8211; **\u89e3\u6c7a\u65b9\u6cd5**\uff1a\u8a18\u4f4f\u7a7a\u5bb9\u5668\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\u7b49\uff09\u5728 Python \u4e2d\u88ab\u8996\u70ba `False`\u3002\u82e5\u60f3\u8981\u6aa2\u67e5\u5bb9\u5668\u662f\u5426\u70ba\u7a7a\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 `not` \u95dc\u9375\u5b57\u3002<\/p>\n<p>&#8211; **\u554f\u984c**\uff1a\u4f7f\u7528 `bool()` \u6aa2\u67e5\u81ea\u8a02\u7269\u4ef6\u7684\u771f\u507d\u6642\uff0c\u7d50\u679c\u4e0d\u5982\u9810\u671f\u3002<br \/>\n  &#8211; **\u89e3\u6c7a\u65b9\u6cd5**\uff1a\u60a8\u53ef\u4ee5\u5728\u81ea\u8a02\u985e\u5225\u4e2d\u5b9a\u7fa9 `__bool__()` \u6216 `__len__()` \u65b9\u6cd5\u4f86\u63a7\u5236\u7269\u4ef6\u7684\u5e03\u6797\u503c\u8fd4\u56de\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`bool()` \u51fd\u6578\u4e0d\u50c5\u7528\u65bc\u7c21\u55ae\u7684\u689d\u4ef6\u5224\u65b7\uff0c\u9084\u53ef\u4ee5\u5728\u66f4\u8907\u96dc\u7684\u908f\u8f2f\u904b\u7b97\u4e2d\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5c07\u5b83\u8207\u5176\u4ed6\u908f\u8f2f\u904b\u7b97\u7b26\uff08\u5982 `and`\u3001`or`\uff09\u7d50\u5408\u4f7f\u7528\uff0c\u4f86\u5275\u5efa\u66f4\u5f37\u5927\u7684\u689d\u4ef6\u5224\u65b7\u3002<\/p>\n<p>&#8220;`python<br \/>\na = 5<br \/>\nb = 0<br \/>\nif bool(a) and bool(b):<br \/>\n    print(&#8220;Both are true&#8221;)<br \/>\nelse:<br \/>\n    print(&#8220;At least one is false&#8221;)  # \u9019\u884c\u5c07\u88ab\u57f7\u884c<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u7531\u65bc `b` \u70ba `0`\uff0c\u6240\u4ee5\u689d\u4ef6\u5224\u65b7\u8fd4\u56de `False`\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u672c\u6587\u6df1\u5165\u63a2\u8a0e\u4e86 Python \u7684 `bool()` \u51fd\u6578\uff0c\u5305\u62ec\u5176\u7528\u6cd5\u3001\u7bc4\u4f8b\u53ca\u5e38\u898b\u932f\u8aa4\u6392\u9664\u6280\u5de7\u3002\u638c\u63e1 `bool()` \u51fd\u6578\u4e0d\u50c5\u80fd\u5e6b\u52a9\u60a8\u5728\u7a0b\u5f0f\u8a2d\u8a08\u4e2d\u9032\u884c\u6709\u6548\u7684\u908f\u8f2f\u5224\u65b7\uff0c\u9084\u80fd\u63d0\u5347\u60a8\u5c0d Python \u8a9e\u8a00\u7684\u7406\u89e3\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u8a9e\u8a00\u7684\u5176\u4ed6\u529f\u80fd\uff0c\u6b61\u8fce\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78\u6587](https:\/\/vocus.cc\/article\/5f3e2c5efcd8c6e2dc8e4a8c)\uff0c\u8b93\u60a8\u5728 Python \u7684\u5b78\u7fd2\u4e4b\u8def\u4e0a\u66f4\u9032\u4e00\u6b65\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: bool() \u51fd\u6578\u7684\u8fd4\u56de\u503c\u662f\u4ec0\u9ebc\uff1f<\/h3>\n<p>A1: `bool()` \u51fd\u6578\u6703\u8fd4\u56de `True` \u6216 `False`\uff0c\u53d6\u6c7a\u65bc\u50b3\u5165\u7269\u4ef6\u7684\u771f\u507d\u6027\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u6aa2\u67e5\u4e00\u500b\u5217\u8868\u662f\u5426\u70ba\u7a7a\uff1f<\/h3>\n<p>A2: \u53ef\u4ee5\u4f7f\u7528 `if not my_list:` \u4f86\u6aa2\u67e5 `my_list` \u662f\u5426\u70ba\u7a7a\uff0c\u82e5\u70ba\u7a7a\u5247\u689d\u4ef6\u6210\u7acb\u3002<\/p>\n<h3>Q3: \u662f\u5426\u53ef\u4ee5\u81ea\u8a02\u7269\u4ef6\u7684\u5e03\u6797\u503c\uff1f<\/h3>\n<p>A3: \u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5728\u81ea\u8a02\u985e\u5225\u4e2d\u5b9a\u7fa9 `__bool__()` \u6216 `__len__()` \u65b9\u6cd5\u4f86\u63a7\u5236\u7269\u4ef6\u7684\u5e03\u6797\u503c\u8fd4\u56de\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684bool()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u5c07\u4efb\u4f55\u503c\u8f49\u63db\u70ba\u5e03\u6797\u503c\uff0c\u4e26\u63d0\u4f9b\u6709\u95dc\u8a72\u503c\u7684\u6709\u7528\u4fe1\u606f\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39bool()\u51fd\u6578\u7684\u7528\u6cd5\uff0c\u4ee5\u53ca\u5b83\u5982\u4f55\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u7ba1\u7406\u60a8\u7684Python\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3304","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Ri","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3304"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3304\/revisions"}],"predecessor-version":[{"id":12871,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3304\/revisions\/12871"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}