{"id":668,"date":"2026-03-12T15:29:00","date_gmt":"2026-03-12T07:29:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=668"},"modified":"2026-07-14T16:41:38","modified_gmt":"2026-07-14T08:41:38","slug":"python%e5%8f%af%e8%b0%83%e7%94%a8%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/03\/12\/python%e5%8f%af%e8%b0%83%e7%94%a8%e5%af%b9%e8%b1%a1\/","title":{"rendered":"python\u53ef\u8c03\u7528\u5bf9\u8c61"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u8ba9\u5bf9\u8c61\u53d8\u6210\u53ef\u8c03\u7528\u5bf9\u8c61<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b63\u5e38\u60c5\u51b5\u5b9e\u4f8b\u53ea\u662f\u5bf9\u8c61\uff0c\u53ea\u80fd\u901a\u8fc7.\u8bbf\u95ee\u5c5e\u6027\u6216\u65b9\u6cd5<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7c7b\u4e2d\u5b9a\u4e49 <code>__call__<\/code> \u65b9\u6cd5\uff0c<strong>\u5b9e\u4f8b<\/strong>\u5c31\u53d8\u6210\u4e86\u53ef\u8c03\u7528\u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Adder:<br> &nbsp; &nbsp;def __init__(self, n):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.n = n<br>\u200b<br> &nbsp; &nbsp;def __call__(self, x):<br> &nbsp; &nbsp; &nbsp; &nbsp;return self.n + x<br>\u200b<br>add_5 = Adder(5)<br>print(callable(add_5)) &nbsp; # True<br># add_5(10) \u7b49\u6548\u4e8e Adder.__call__(add_5, 10)<br>print(add_5(10)) &nbsp; &nbsp; &nbsp; &nbsp; # 15\uff0c\u50cf\u51fd\u6570\u4e00\u6837\u8c03\u7528<br>print(add_5(100)) &nbsp; &nbsp; &nbsp; &nbsp;# 105<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5173\u952e\u7406\u89e3\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>__call__<\/code> \u8ba9<strong>\u5b9e\u4f8b<\/strong>\u53ef\u4ee5\u50cf\u51fd\u6570\u4e00\u6837\u88ab\u8c03\u7528<\/li>\n\n\n\n<li>\u8c03\u7528\u5b9e\u4f8b\u65f6\uff0c\u4f20\u5165\u7684\u53c2\u6570\u4f1a\u4f20\u7ed9 <code>__call__<\/code> \u65b9\u6cd5<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b9e\u73b0\u53ef\u914d\u7f6e\u7684\u51fd\u6570\u5bf9\u8c61<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Multiplier:<br> &nbsp; &nbsp;def __init__(self, factor):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.factor = factor<br>\u200b<br> &nbsp; &nbsp;def __call__(self, value):<br> &nbsp; &nbsp; &nbsp; &nbsp;return self.factor * value<br>\u200b<br>double = Multiplier(2)<br>triple = Multiplier(3)<br>\u200b<br>print(double(5)) &nbsp; # 10<br>triple(5) &nbsp; # 15<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5b9e\u73b0\u72b6\u6001\u4fdd\u6301\u7684\u56de\u8c03\u51fd\u6570<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Logger:<br> &nbsp; &nbsp;def __init__(self, prefix):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.prefix = prefix<br> &nbsp; &nbsp; &nbsp; &nbsp;self.log_count = 0<br>\u200b<br> &nbsp; &nbsp;def __call__(self, message):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.log_count += 1<br> &nbsp; &nbsp; &nbsp; &nbsp;print(f\"&#91;{self.prefix}] #{self.log_count}: {message}\")<br>\u200b<br>error_log = Logger(\"ERROR\")<br>error_log(\"\u6587\u4ef6\u672a\u627e\u5230\") &nbsp; &nbsp; # &#91;ERROR] #1: \u6587\u4ef6\u672a\u627e\u5230<br>error_log(\"\u7f51\u7edc\u8fde\u63a5\u5931\u8d25\") &nbsp; # &#91;ERROR] #2: \u7f51\u7edc\u8fde\u63a5\u5931\u8d25<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4ee3\u7801\u7684\u8f93\u51fa\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class A:\n    def __call__(self):\n        print(\"A called\")\n\nclass B(A):\n    def __call__(self):\n        print(\"B called\")\n        super().__call__()\n\nb = B()\nb()\n#B called\n#A called\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8ba9\u5bf9\u8c61\u53d8\u6210\u53ef\u8c03\u7528\u5bf9\u8c61 \u6b63\u5e38\u60c5\u51b5\u5b9e\u4f8b\u53ea\u662f\u5bf9\u8c61\uff0c\u53ea\u80fd\u901a..<\/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":[],"class_list":["post-668","post","type-post","status-publish","format-standard","hentry","category-python","category-back"],"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\/668","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=668"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/668\/revisions"}],"predecessor-version":[{"id":669,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/668\/revisions\/669"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}