{"id":2842,"date":"2025-05-29T17:18:22","date_gmt":"2025-06-04T09:38:22","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2842"},"modified":"2025-06-04T17:18:22","modified_gmt":"2025-06-04T09:38:22","slug":"%e4%bd%bf%e7%94%a8python%e5%b0%87%e6%95%b8%e5%80%bc%e6%88%96%e5%b0%8d%e8%b1%a1%e8%bd%89%e6%8f%9b%e7%82%ba%e5%b8%83%e7%88%be%e5%80%bc","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%bd%bf%e7%94%a8python%e5%b0%87%e6%95%b8%e5%80%bc%e6%88%96%e5%b0%8d%e8%b1%a1%e8%bd%89%e6%8f%9b%e7%82%ba%e5%b8%83%e7%88%be%e5%80%bc\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 `bool()` \u51fd\u6578\uff1a\u503c\u8f49\u63db\u70ba\u5e03\u723e\u503c\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, bool(), \u5e03\u723e\u503c, Python \u6559\u5b78\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 `bool()` \u51fd\u6578\uff1a\u503c\u8f49\u63db\u70ba\u5e03\u723e\u503c\u7684\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`bool()` \u51fd\u6578\u662f\u4e00\u500b\u91cd\u8981\u7684\u5167\u5efa\u51fd\u6578\uff0c\u7528\u65bc\u5c07\u5404\u7a2e\u6578\u503c\u6216\u5c0d\u8c61\u8f49\u63db\u70ba\u5e03\u723e\u503c\u3002\u9019\u5c0d\u65bc\u63a7\u5236\u6d41\u7a0b\u548c\u908f\u8f2f\u904b\u7b97\u975e\u5e38\u6709\u7528\u3002\u672c\u6587\u5c07\u5e36\u60a8\u6df1\u5165\u63a2\u8a0e `bool()` \u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\u3001\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u4ee5\u53ca\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<p>## `bool()` \u51fd\u6578\u7684\u57fa\u672c\u6982\u5ff5<\/p>\n<p>`bool()` \u51fd\u6578\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u985e\u578b\u7684\u53c3\u6578\uff0c\u5305\u62ec\u6578\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\uff0c\u4e26\u5c07\u5176\u8f49\u63db\u70ba\u5e03\u723e\u503c\u3002\u7576\u503c\u70ba\u300c\u771f\u300d\u6642\uff0c\u51fd\u6578\u8fd4\u56de `True`\uff1b\u7576\u503c\u70ba\u300c\u5047\u300d\u6642\uff0c\u5247\u8fd4\u56de `False`\u3002<\/p>\n<p>## `bool()` \u51fd\u6578\u7684\u7bc4\u4f8b<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u7bc4\u4f8b\uff0c\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3 `bool()` \u51fd\u6578\u7684\u7528\u6cd5\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5c07\u6578\u5b57\u8f49\u63db\u70ba\u5e03\u723e\u503c<br \/>\nprint(bool(1))      # True<br \/>\nprint(bool(0))      # False<\/p>\n<p># \u5c07\u5b57\u7b26\u4e32\u8f49\u63db\u70ba\u5e03\u723e\u503c<br \/>\nprint(bool(&#8220;Hello&#8221;))  # True<br \/>\nprint(bool(&#8220;&#8221;))        # False<\/p>\n<p># \u5c07\u5217\u8868\u8f49\u63db\u70ba\u5e03\u723e\u503c<br \/>\nprint(bool([&#8220;apple&#8221;, &#8220;banana&#8221;]))  # True<br \/>\nprint(bool([]))                     # False<\/p>\n<p># \u5c07\u5b57\u5178\u8f49\u63db\u70ba\u5e03\u723e\u503c<br \/>\nprint(bool({&#8220;name&#8221;: &#8220;John&#8221;, &#8220;age&#8221;: 25}))  # True<br \/>\nprint(bool({}))                            # False<br \/>\n&#8220;`<\/p>\n<p>\u5f9e\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u770b\u5230\uff0c`bool()` \u51fd\u6578\u80fd\u5920\u9748\u6d3b\u5730\u8655\u7406\u4e0d\u540c\u985e\u578b\u7684\u6578\u64da\uff0c\u4e26\u6839\u64da\u5176\u5167\u5bb9\u8fd4\u56de\u9069\u7576\u7684\u5e03\u723e\u503c\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/p>\n<p>\u5728\u4f7f\u7528 `bool()` \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>1. **\u7121\u6cd5\u8b58\u5225\u7684\u985e\u578b**\uff1a\u78ba\u4fdd\u50b3\u905e\u7d66 `bool()` \u7684\u53c3\u6578\u662f\u652f\u63f4\u7684\u6578\u64da\u985e\u578b\u3002\u5c0d\u65bc\u81ea\u5b9a\u7fa9\u5c0d\u8c61\uff0c\u60a8\u53ef\u4ee5\u901a\u904e\u5be6\u73fe `__bool__()` \u6216 `__len__()` \u65b9\u6cd5\u4f86\u5b9a\u7fa9\u5176\u5e03\u723e\u503c\u3002<\/p>\n<p>2. **\u6df7\u6dc6\u7a7a\u503c\u8207\u5047\u503c**\uff1a\u8acb\u6ce8\u610f\uff0c\u7a7a\u5b57\u7b26\u4e32\u3001\u7a7a\u5217\u8868\u548c\u7a7a\u5b57\u5178\u6703\u88ab\u8996\u70ba\u5047\u503c\uff0c\u9019\u53ef\u80fd\u6703\u5c0e\u81f4\u908f\u8f2f\u932f\u8aa4\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`bool()` \u51fd\u6578\u5728\u8a31\u591a\u60c5\u5883\u4e2d\u90fd\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5728\u689d\u4ef6\u8a9e\u53e5\u4e2d\u4f7f\u7528\u5b83\u4f86\u5224\u65b7\u8b8a\u91cf\u662f\u5426\u5b58\u5728\u6216\u662f\u5426\u6709\u6548\u3002\u9019\u5728\u958b\u767c API \u6216\u8655\u7406\u7528\u6236\u8f38\u5165\u6642\u5c24\u70ba\u91cd\u8981\u3002<\/p>\n<p>\u5982\u679c\u60a8\u60f3\u4e86\u89e3\u66f4\u591a Python \u7684\u9032\u968e\u6982\u5ff5\uff0c\u53ef\u4ee5\u53c3\u8003 [\u9019\u88e1](https:\/\/miner.tw\/python-tutorial)\u3002<\/p>\n<p>## \u7e3d\u7d50<\/p>\n<p>Python \u7684 `bool()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u4e14\u9748\u6d3b\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5c07\u5404\u7a2e\u6578\u64da\u985e\u578b\u8f49\u63db\u70ba\u5e03\u723e\u503c\u3002\u4e86\u89e3\u5176\u5de5\u4f5c\u539f\u7406\u548c\u6700\u4f73\u5be6\u8e10\u5c07\u6709\u52a9\u65bc\u60a8\u5728 Python \u958b\u767c\u4e2d\u66f4\u6709\u6548\u5730\u8655\u7406\u908f\u8f2f\u904b\u7b97\u3002<\/p>\n<p>\u5e0c\u671b\u672c\u6587\u5c0d\u60a8\u6709\u6240\u5e6b\u52a9\uff0c\u5982\u679c\u60a8\u6709\u4efb\u4f55\u7591\u554f\uff0c\u6b61\u8fce\u5728\u4e0b\u65b9\u7559\u8a00\u8a0e\u8ad6\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `bool()` \u51fd\u6578\u7684\u8fd4\u56de\u503c\u6709\u54ea\u4e9b\uff1f**<br \/>\nA1: `bool()` \u51fd\u6578\u8fd4\u56de `True` \u6216 `False`\uff0c\u5177\u9ad4\u53d6\u6c7a\u65bc\u50b3\u905e\u7d66\u5b83\u7684\u503c\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u81ea\u5b9a\u7fa9\u985e\u5225\u7684\u5e03\u723e\u503c\uff1f**<br \/>\nA2: \u60a8\u53ef\u4ee5\u5728\u985e\u5225\u4e2d\u5be6\u73fe `__bool__()` \u65b9\u6cd5\u4f86\u5b9a\u7fa9\u5176\u5e03\u723e\u503c\uff0c\u6216\u8005\u5be6\u73fe `__len__()` \u65b9\u6cd5\uff0c\u8fd4\u56de\u503c\u70ba 0 \u6642\u88ab\u8996\u70ba\u5047\u3002<\/p>\n<p>**Q3: `bool()` \u51fd\u6578\u662f\u5426\u50c5\u9650\u65bc\u7279\u5b9a\u6578\u64da\u985e\u578b\uff1f**<br \/>\nA3: \u4e0d\uff0c`bool()` \u51fd\u6578\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u6578\u64da\u985e\u578b\uff0c\u5305\u62ec\u81ea\u5b9a\u7fa9\u5c0d\u8c61\uff0c\u4f46\u8981\u78ba\u4fdd\u8a72\u5c0d\u8c61\u6b63\u78ba\u5be6\u73fe\u4e86\u5e03\u723e\u908f\u8f2f\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Python\u7684bool()\u51fd\u6578\u5c07\u6578\u503c\u6216\u5c0d\u8c61\u8f49\u63db\u70ba\u5e03\u723e\u503c\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7406\u89e3\u7a0b\u5e8f\u7684\u57f7\u884c\u6d41\u7a0b\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39bool()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4f86\u89e3\u91cb\u5b83\u7684\u529f\u80fd\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-2842","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-JQ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2842","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=2842"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2842\/revisions"}],"predecessor-version":[{"id":13023,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2842\/revisions\/13023"}],"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=2842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}