{"id":678,"date":"2026-03-14T20:41:00","date_gmt":"2026-03-14T12:41:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=678"},"modified":"2026-07-14T20:57:45","modified_gmt":"2026-07-14T12:57:45","slug":"python-%e9%ad%94%e6%9c%af%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/03\/14\/python-%e9%ad%94%e6%9c%af%e6%96%b9%e6%b3%95\/","title":{"rendered":"python \u9b54\u672f\u65b9\u6cd5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u7c7b\u578b\u8f6c\u6362<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u73b0\u7c7b\u578b\u8f6c\u6362\u9b54\u672f\u65b9\u6cd5\uff0c\u8ba9\u5bf9\u8c61\u652f\u6301 <code>int()<\/code>\u3001<code>float()<\/code>\u3001<code>bool()<\/code> \u7b49\u8f6c\u6362\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Money:<br> &nbsp; &nbsp;def __init__(self, amount):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.amount = amount<br>\u200b<br> &nbsp; &nbsp;def __int__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return int(self.amount)<br>\u200b<br> &nbsp; &nbsp;def __float__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return float(self.amount)<br>\u200b<br> &nbsp; &nbsp;def __bool__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return self.amount != 0<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"Money({self.amount})\"<br>\u200b<br>\u200b<br>m = Money(100.5)<br>print(int(m)) &nbsp; &nbsp; &nbsp;# 100<br>print(float(m)) &nbsp; &nbsp;# 100.5<br>print(bool(m)) &nbsp; &nbsp; # True<br>\u200b<br>m0 = Money(0)<br>print(bool(m0)) &nbsp; &nbsp;# False<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u6838\u5fc3\u539f\u7406\uff1a\u7c7b\u578b\u8f6c\u6362\u5185\u7f6e\u51fd\u6570\u4f1a\u81ea\u52a8\u8c03\u7528\u5bf9\u5e94\u9b54\u672f\u65b9\u6cd5<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u5e95\u5c42\u6267\u884c\u903b\u8f91<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f60\u6267\u884c <code>int(obj)<\/code> \/ <code>float(obj)<\/code> \/ <code>bool(obj)<\/code> \u65f6\uff0cPython \u4e0d\u662f\u76f4\u63a5\u66b4\u529b\u8f6c\u6362\uff0c\u800c\u662f\u4e00\u5957\u56fa\u5b9a\u89c4\u5219\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>int(m)<\/code> \u2192 \u5185\u90e8\u4f1a\u5c1d\u8bd5\u8c03\u7528 <code>m.__int__()<\/code><\/li>\n\n\n\n<li><code>float(m)<\/code> \u2192 \u5185\u90e8\u4f1a\u5c1d\u8bd5\u8c03\u7528 <code>m.__float__()<\/code><\/li>\n\n\n\n<li><code>bool(m)<\/code> \u2192 \u5185\u90e8\u4f1a\u5c1d\u8bd5\u8c03\u7528 <code>m.__bool__()<\/code><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u9b54\u672f\u65b9\u6cd5\u662f<strong>\u534f\u8bae\u63a5\u53e3<\/strong>\uff1a\u53ea\u8981\u4f60\u7684\u7c7b\u5b9e\u73b0\u4e86\u5bf9\u5e94\u540d\u5b57\u7684\u53cc\u4e0b\u5212\u7ebf\u65b9\u6cd5\uff0c\u7cfb\u7edf\u5185\u7f6e\u8f6c\u6362\u51fd\u6570\u5c31\u4f1a\u81ea\u52a8\u53bb\u6267\u884c\u4f60\u5199\u7684\u81ea\u5b9a\u4e49\u903b\u8f91\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5206\u6b65\u62c6\u89e3 <code>int(m)<\/code> \u5b8c\u6574\u6d41\u7a0b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>m = Money(100.5)\nprint(int(m))\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u8c03\u7528\u5185\u7f6e\u51fd\u6570 <code>int(m)<\/code><\/li>\n\n\n\n<li>Python \u67e5\u627e <code>m<\/code> \u5bf9\u8c61\u6709\u6ca1\u6709\u5b9a\u4e49 <code>__int__<\/code> \u65b9\u6cd5<\/li>\n\n\n\n<li>\u627e\u5230\u4f60\u5728 <code>Money<\/code> \u91cc\u5199\u7684 <code>def __int__(self): return int(self.amount)<\/code><\/li>\n\n\n\n<li>\u6267\u884c\u8be5\u65b9\u6cd5\uff0c\u62ff\u5230\u8fd4\u56de\u503c <code>100<\/code> \u4f5c\u4e3a\u6700\u7ec8\u7ed3\u679c<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u540c\u7406\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>float(m)<\/code> \u89e6\u53d1 <code>m.__float__()<\/code><\/li>\n\n\n\n<li><code>bool(m)<\/code> \u4f18\u5148\u89e6\u53d1 <code>m.__bool__()<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u8865\u5145 bool \u7684\u5907\u7528\u89c4\u5219\uff08\u62d3\u5c55\uff09<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u7c7b<strong>\u6ca1\u6709\u5b9e\u73b0 <code>__bool__<\/code><\/strong>\uff0cPython \u4f1a\u964d\u7ea7\u8c03\u7528 <code>__len__<\/code> \u5224\u65ad\u957f\u5ea6\u662f\u5426\u4e3a 0\uff1b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8fde <code>__len__<\/code> \u4e5f\u6ca1\u6709\uff0c\u9ed8\u8ba4\u5168\u90e8\u89c6\u4e3a <code>True<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Test:\n    def __len__(self):\n        return 0\n\nt = Test()\nprint(bool(t)) # False\uff0c\u8d70__len__<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u533a\u5206\uff1a\u5185\u7f6e\u51fd\u6570 \u548c \u7c7b\u9b54\u672f\u65b9\u6cd5\u7684\u5173\u7cfb<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>int<\/code> \/ <code>float<\/code> \/ <code>bool<\/code> \u662f\u9876\u5c42\u7cfb\u7edf\u5de5\u5177\u51fd\u6570\uff0c\u5b83\u4eec<strong>\u517c\u5bb9\u6240\u6709\u81ea\u5b9a\u4e49\u7c7b<\/strong>\uff0c\u9760\u7edf\u4e00\u7684\u9b54\u672f\u65b9\u6cd5\u534f\u8bae\u5b9e\u73b0\u6269\u5c55\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u539f\u751f\u6570\u5b57\uff08int\/float\uff09\u5185\u90e8\u81ea\u5e26\u5e95\u5c42\u903b\u8f91\uff1b<\/li>\n\n\n\n<li>\u81ea\u5b9a\u4e49\u7c7b\u60f3\u8981\u652f\u6301\u7c7b\u578b\u8f6c\u6362\uff0c\u53ea\u8981\u5b9e\u73b0\u5bf9\u5e94 <code>__xxx__<\/code> \u9b54\u672f\u65b9\u6cd5\uff0c\u7cfb\u7edf\u51fd\u6570\u81ea\u52a8\u9002\u914d\u4f60\u7684\u903b\u8f91\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b80\u5355\u7c7b\u6bd4\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>int()<\/code> \u50cf\u4e00\u4e2a\u901a\u7528\u8f6c\u6362\u5668\uff0c\u5b83\u7ea6\u5b9a\u4e86\u4e00\u5957\u6807\u51c6\uff1a\u300c\u60f3\u8981\u88ab\u6211\u8f6c\u6210\u6574\u6570\uff0c\u4f60\u5c31\u63d0\u4f9b <code>__int__<\/code> \u65b9\u6cd5\u300d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u7684 <code>Money<\/code> \u9075\u5b88\u4e86\u8fd9\u4e2a\u7ea6\u5b9a\uff0c\u6240\u4ee5 <code>int(m)<\/code> \u4f1a\u81ea\u52a8\u6267\u884c\u4f60\u5199\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u53cd\u5411\u9a8c\u8bc1\uff1a\u5220\u6389\u9b54\u672f\u65b9\u6cd5\u4f1a\u62a5\u9519<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u628a <code>__int__<\/code> \u5220\u6389\u540e\u518d\u6267\u884c <code>int(m)<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Money:\n    def __init__(self, amount):\n        self.amount = amount\n\n# \u5220\u6389 __int__\nm = Money(100.5)\nprint(int(m))\n# TypeError: int() argument must be a string, a bytes-like object or a number, not 'Money'\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u627e\u4e0d\u5230 <code>__int__<\/code> \u534f\u8bae\u65b9\u6cd5\uff0c\u89e3\u91ca\u5668\u4e0d\u77e5\u9053\u600e\u4e48\u628a Money \u5bf9\u8c61\u8f6c\u6574\u6570\uff0c\u76f4\u63a5\u629b\u7c7b\u578b\u9519\u8bef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7c7b\u578b\u8f6c\u6362 \u5b9e\u73b0\u7c7b\u578b\u8f6c\u6362\u9b54\u672f\u65b9\u6cd5\uff0c\u8ba9\u5bf9\u8c61\u652f\u6301 int..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,14],"tags":[54],"class_list":["post-678","post","type-post","status-publish","format-standard","hentry","category-python","category-back","tag-python"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/python\/\" rel=\"category tag\">Python<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/\" rel=\"category tag\">\u540e\u7aef<\/a>","tag_info":"\u540e\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=678"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/678\/revisions"}],"predecessor-version":[{"id":679,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/678\/revisions\/679"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}