{"id":204911,"date":"2025-05-29T13:07:46","date_gmt":"2025-05-29T05:07:46","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204911\/"},"modified":"2025-05-29T13:07:46","modified_gmt":"2025-05-29T05:07:46","slug":"python-%e4%b8%ad-classmethod-%e4%b8%ba%e4%bb%80%e4%b9%88%e6%97%a0%e6%b3%95%e7%9b%b4%e6%8e%a5%e8%b0%83%e7%94%a8-property-%e8%a3%85%e9%a5%b0%e7%9a%84%e5%b1%9e%e6%80%a7%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204911\/","title":{"rendered":"Python \u4e2d @classmethod \u4e3a\u4ec0\u4e48\u65e0\u6cd5\u76f4\u63a5\u8c03\u7528 @property \u88c5\u9970\u7684\u5c5e\u6027\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u4e2d @classmethod \u4e3a\u4ec0\u4e48\u65e0\u6cd5\u76f4\u63a5\u8c03\u7528 @property \u88c5\u9970\u7684\u5c5e\u6027\uff1f<\/h1>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1731987765673c09355bd69.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u4e2d @classmethod \u65e0\u6cd5\u8c03\u7528 @property \u88c5\u9970\u7684\u5c5e\u6027<\/strong><\/p>\n<p>python \u7684 @classmethod \u88c5\u9970\u5668\u5141\u8bb8\u5728\u4e0d\u521b\u5efa\u5b9e\u4f8b\u7684\u60c5\u51b5\u4e0b\u8bbf\u95ee\u548c\u4fee\u6539\u7c7b\u5c5e\u6027\u3002\u4f46\u662f\uff0c\u5728\u4f7f\u7528 @property \u88c5\u9970\u7684\u5c5e\u6027\u65f6\uff0c\u4f1a\u51fa\u73b0 attributeerror \u5f02\u5e38\u3002<\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a @property \u88c5\u9970\u5668\u521b\u5efa\u4e86\u4e00\u79cd\u7279\u6b8a\u65b9\u6cd5\uff0c\u79f0\u4e3a\u5c5e\u6027\u8bbf\u95ee\u5668\u3002\u8be5\u8bbf\u95ee\u5668\u88ab\u8bbe\u8ba1\u4e3a\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u800c\u4e0d\u662f\u5c5e\u6027\u672c\u8eab\u3002\u8fd9\u610f\u5473\u7740\u5f53 @classmethod \u65b9\u6cd5\u5c1d\u8bd5\u8bbf\u95ee @property \u88c5\u9970\u7684\u5c5e\u6027\u65f6\uff0c\u5b83\u5b9e\u9645\u4e0a\u662f\u5728\u8c03\u7528\u5c5e\u6027\u8bbf\u95ee\u5668\uff0c\u800c\u4e0d\u662f\u5c5e\u6027\u672c\u8eab\u3002\u8fd9\u5c31\u4f1a\u5f15\u53d1 attributeerror \u5f02\u5e38\uff0c\u56e0\u4e3a\u5c5e\u6027\u8bbf\u95ee\u5668\u6ca1\u6709\u6240\u9700\u7684\u5c5e\u6027\u3002<\/p>\n<p>\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u662f\u663e\u5f0f\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\uff0c\u7136\u540e\u901a\u8fc7\u8be5\u5b9e\u4f8b\u8bbf\u95ee @property \u88c5\u9970\u7684\u5c5e\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>class MyClass:\n    def __init__(self, value):\n        self._value = value\n\n    @property\n    def value(self):\n        return self._value\n\n    @classmethod\n    def show_value(cls):\n        obj = cls(10)\n        print(obj.value)<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0cshow_value \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b obj\uff0c\u7136\u540e\u901a\u8fc7\u8be5\u5b9e\u4f8b\u8bbf\u95ee value \u5c5e\u6027\u3002\u8fd9\u5c06\u907f\u514d attributeerror \u5f02\u5e38\u3002<\/p>\n<p>\u4e86\u89e3 @classmethod \u548c @property \u6982\u5ff5\u4e4b\u95f4\u7684\u533a\u522b\u975e\u5e38\u91cd\u8981\u3002@classmethod \u7528\u4e8e\u521b\u5efa\u7c7b\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4e0d\u4f9d\u8d56\u4e8e\u4efb\u4f55\u5b9e\u4f8b\uff0c\u800c @property \u7528\u4e8e\u521b\u5efa\u5c5e\u6027\u8bbf\u95ee\u5668\uff0c\u8be5\u8bbf\u95ee\u5668\u53ef\u7528\u4e8e\u83b7\u53d6\u548c\u8bbe\u7f6e\u5c5e\u6027\u503c\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPython \u4e2d @classmethod \u4e3a\u4ec0\u4e48\u65e0\u6cd5\u76f4\u63a5\u8c03\u7528 @property \u88c5\u9970\u7684\u5c5e\u6027\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u4e2d @classm&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204911","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204911"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204911\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}