{"id":2976,"date":"2025-05-29T16:43:08","date_gmt":"2025-06-04T09:15:08","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2976"},"modified":"2025-06-04T16:43:08","modified_gmt":"2025-06-04T09:15:08","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84classmethod%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84classmethod%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 classmethod() \u51fd\u6578\uff1a2025 \u6700\u65b0\u6559\u5b78\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, classmethod, class method, Python \u6559\u5b78\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 classmethod()<\/h1>\n<p>Python \u4e2d\u7684 `classmethod()` \u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u51fd\u5f0f\uff0c\u5b83\u5141\u8a31\u4f60\u5c07\u4e00\u500b\u51fd\u5f0f\u6a19\u8a18\u70ba\u985e\u5225\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u7269\u4ef6\u65b9\u6cd5\u3002\u9019\u6a23\u53ef\u4ee5\u5728\u4e0d\u9700\u8981\u5be6\u4f8b\u5316\u7269\u4ef6\u7684\u60c5\u6cc1\u4e0b\uff0c\u76f4\u63a5\u4f7f\u7528\u985e\u5225\u65b9\u6cd5\u3002\u9019\u5c0d\u65bc\u67d0\u4e9b\u8a2d\u8a08\u6a21\u5f0f\u548c\u985e\u5225\u908f\u8f2f\u975e\u5e38\u6709\u5e6b\u52a9\u3002<\/p>\n<h2>classmethod() \u7684\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<p>\u8981\u4f7f\u7528 `classmethod()`\uff0c\u4f60\u9700\u8981\u5728\u5b9a\u7fa9\u51fd\u5f0f\u6642\uff0c\u5c07\u5b83\u6a19\u8a18\u70ba `@classmethod`\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    @classmethod<br \/>\n    def my_method(cls):<br \/>\n        print(&#8220;\u9019\u662f\u4e00\u500b\u985e\u5225\u65b9\u6cd5&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba `my_method` \u7684\u985e\u5225\u65b9\u6cd5\uff0c\u5b83\u6703\u5370\u51fa\u4e00\u500b\u5b57\u4e32\u3002<\/p>\n<p>\u8981\u547c\u53eb\u9019\u500b\u985e\u5225\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u985e\u5225\u540d\u7a31\uff0c\u800c\u4e0d\u662f\u7269\u4ef6\u540d\u7a31\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nMyClass.my_method()<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u5c31\u53ef\u4ee5\u57f7\u884c `my_method` \u9019\u500b\u985e\u5225\u65b9\u6cd5\uff0c\u800c\u4e0d\u9700\u8981\u5148\u5be6\u4f8b\u5316\u7269\u4ef6\u3002<\/p>\n<h2>classmethod() \u7684\u512a\u9ede<\/h2>\n<p>`classmethod()` \u7684\u6700\u5927\u512a\u9ede\u5728\u65bc\u5b83\u80fd\u5920\u8b93\u4f60\u5728\u4e0d\u9700\u8981\u5be6\u4f8b\u5316\u7269\u4ef6\u7684\u60c5\u6cc1\u4e0b\uff0c\u76f4\u63a5\u4f7f\u7528\u985e\u5225\u65b9\u6cd5\u3002\u9019\u5c0d\u65bc\u8a2d\u8a08\u67d0\u4e9b\u985e\u5225\u908f\u8f2f\u4f86\u8aaa\u6703\u66f4\u52a0\u7c21\u4fbf\u3002\u6b64\u5916\uff0c`classmethod()` \u9084\u53ef\u4ee5\u8b93\u4f60\u5728\u4e0d\u540c\u7684\u985e\u5225\u4e2d\u4f7f\u7528\u76f8\u540c\u7684\u51fd\u5f0f\u540d\u7a31\uff0c\u800c\u4e0d\u6703\u7522\u751f\u885d\u7a81\uff0c\u9019\u6a23\u6703\u8b93\u4f60\u7684\u7a0b\u5f0f\u66f4\u6e05\u6670\uff0c\u7dad\u8b77\u4e5f\u66f4\u5bb9\u6613\u3002<\/p>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b\uff1a\u4f7f\u7528 classmethod() \u5efa\u7acb\u5de5\u5ee0\u65b9\u6cd5<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u985e\u5225\u65b9\u6cd5\uff0c`classmethod()` \u4e5f\u975e\u5e38\u9069\u5408\u7528\u4f86\u5be6\u4f5c\u5de5\u5ee0\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 `classmethod()` \u4f86\u751f\u6210\u4e0d\u540c\u7684\u7269\u4ef6\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Person:<br \/>\n    def __init__(self, name, age):<br \/>\n        self.name = name<br \/>\n        self.age = age<\/p>\n<p>    @classmethod<br \/>\n    def from_birth_year(cls, name, birth_year):<br \/>\n        age = 2025 &#8211; birth_year  # \u5047\u8a2d\u7576\u524d\u5e74\u4efd\u70ba 2025<br \/>\n        return cls(name, age)<\/p>\n<p># \u4f7f\u7528\u5de5\u5ee0\u65b9\u6cd5\u5efa\u7acb\u7269\u4ef6<br \/>\nperson1 = Person.from_birth_year(&#8220;Alice&#8221;, 1990)<br \/>\nprint(person1.name)  # Alice<br \/>\nprint(person1.age)   # 35<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c`from_birth_year` \u662f\u4e00\u500b\u985e\u5225\u65b9\u6cd5\uff0c\u5b83\u8b93\u6211\u5011\u53ef\u4ee5\u6839\u64da\u51fa\u751f\u5e74\u4efd\u4f86\u5275\u5efa `Person` \u7269\u4ef6\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u7576\u4f7f\u7528 `classmethod()` \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u932f\u8aa4\uff1a<\/p>\n<p>1. **TypeError: &#8216;classmethod&#8217; object is not callable**\uff1a\u9019\u901a\u5e38\u662f\u56e0\u70ba\u4f60\u5fd8\u8a18\u5728\u65b9\u6cd5\u524d\u52a0\u4e0a `@classmethod` \u88dd\u98fe\u5668\u3002<br \/>\n2. **AttributeError**\uff1a\u5982\u679c\u4f60\u5728\u985e\u5225\u65b9\u6cd5\u4e2d\u5617\u8a66\u8a2a\u554f\u5be6\u4f8b\u5c6c\u6027\uff0c\u6703\u5c0e\u81f4\u6b64\u932f\u8aa4\uff0c\u56e0\u70ba\u985e\u5225\u65b9\u6cd5\u7121\u6cd5\u8a2a\u554f\u5be6\u4f8b\u7684\u5c6c\u6027\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`classmethod()` \u5728\u8a2d\u8a08\u6a21\u5f0f\u4e2d\u7d93\u5e38\u88ab\u4f7f\u7528\uff0c\u4f8b\u5982\u5de5\u5ee0\u6a21\u5f0f\u548c\u55ae\u4f8b\u6a21\u5f0f\u3002\u4f60\u53ef\u4ee5\u901a\u904e\u9019\u4e9b\u6a21\u5f0f\u4f86\u9032\u4e00\u6b65\u7406\u89e3\u5982\u4f55\u4f7f\u7528\u985e\u5225\u65b9\u6cd5\u4f86\u512a\u5316\u4f60\u7684\u7a0b\u5f0f\u8a2d\u8a08\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u4e86\u89e3\u66f4\u591a\u95dc\u65bc Python \u7684\u6559\u5b78\uff0c\u5efa\u8b70\u53c3\u8003 [Vocus Python \u6559\u5b78](https:\/\/vocus.cc) \u6216 [Miner Python \u6559\u5b78](https:\/\/miner.tw) \u4ee5\u7372\u5f97\u66f4\u6df1\u5165\u7684\u8cc7\u6e90\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Python \u4e2d\u7684 `classmethod()` \u4f7f\u5f97\u985e\u5225\u65b9\u6cd5\u7684\u4f7f\u7528\u8b8a\u5f97\u975e\u5e38\u7c21\u55ae\uff0c\u80fd\u5920\u5728\u4e0d\u9700\u8981\u5be6\u4f8b\u5316\u7269\u4ef6\u7684\u60c5\u6cc1\u4e0b\u4f7f\u7528\uff0c\u4e26\u4e14\u5141\u8a31\u5728\u4e0d\u540c\u985e\u5225\u4e2d\u4f7f\u7528\u76f8\u540c\u7684\u51fd\u5f0f\u540d\u7a31\u800c\u4e0d\u6703\u885d\u7a81\u3002\u9019\u4e9b\u7279\u6027\u4f7f\u5f97\u7a0b\u5f0f\u66f4\u5177\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: classmethod() \u548c staticmethod() \u6709\u4ec0\u9ebc\u5340\u5225\uff1f**<br \/>\nA1: `classmethod()` \u5141\u8a31\u4f60\u8a2a\u554f\u985e\u5225\u672c\u8eab\uff0c\u800c `staticmethod()` \u5247\u4e0d\u9700\u8981\u8a2a\u554f\u985e\u5225\u6216\u5be6\u4f8b\uff0c\u50c5\u50c5\u662f\u5c07\u65b9\u6cd5\u6a19\u8a18\u70ba\u975c\u614b\u3002<\/p>\n<p>**Q2: \u4f55\u6642\u61c9\u8a72\u4f7f\u7528 classmethod()\uff1f**<br \/>\nA2: \u7576\u4f60\u9700\u8981\u4e00\u500b\u65b9\u6cd5\u4f86\u8a2a\u554f\u6216\u4fee\u6539\u985e\u5225\u72c0\u614b\uff0c\u6216\u8005\u60f3\u8981\u5275\u5efa\u5de5\u5ee0\u65b9\u6cd5\u6642\uff0c\u61c9\u8a72\u4f7f\u7528 `classmethod()`\u3002<\/p>\n<p>**Q3: classmethod() \u53ef\u4ee5\u7528\u65bc\u54ea\u7a2e\u985e\u578b\u7684\u61c9\u7528\uff1f**<br \/>\nA3: `classmethod()` \u9069\u5408\u7528\u65bc\u9700\u8981\u5171\u4eab\u985e\u5225\u72c0\u614b\u6216\u5275\u5efa\u8907\u96dc\u7269\u4ef6\u7684\u61c9\u7528\u5834\u666f\uff0c\u4f8b\u5982\u5de5\u5ee0\u6a21\u5f0f\u548c\u55ae\u4f8b\u6a21\u5f0f\u7b49\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684Classmethod()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u5728\u985e\u5225\u4e2d\u5b9a\u7fa9\u975c\u614b\u65b9\u6cd5\uff0c\u4e26\u4e14\u53ef\u4ee5\u8b93\u4f60\u5728\u985e\u5225\u4e2d\u5b9a\u7fa9\u53ef\u4ee5\u88ab\u5b50\u985e\u5225\u7e7c\u627f\u7684\u65b9\u6cd5\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u6703\u8a73\u7d30\u4ecb\u7d39Classmethod()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\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-2976","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-M0","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2976","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=2976"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2976\/revisions"}],"predecessor-version":[{"id":12990,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2976\/revisions\/12990"}],"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=2976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}