{"id":183869,"date":"2023-08-03T01:10:31","date_gmt":"2023-08-02T17:10:31","guid":{"rendered":"http:\/\/www.idc.net\/help\/183869\/"},"modified":"2023-08-03T01:10:31","modified_gmt":"2023-08-02T17:10:31","slug":"%e5%9c%a8-python-%e4%b8%ad%e4%bd%bf%e7%94%a8staticmethod%e8%a3%85%e9%a5%b0%e5%99%a8%e5%ae%9a%e4%b9%89%e9%9d%99%e6%80%81%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/183869\/","title":{"rendered":"\u5728 Python \u4e2d\u4f7f\u7528@staticmethod\u88c5\u9970\u5668\u5b9a\u4e49\u9759\u6001\u65b9\u6cd5"},"content":{"rendered":"<p><code>@staticmethod<\/code>\u662f\u4e00\u4e2a\u5185\u7f6e\u7684\u88c5\u9970\u5668\uff0c\u5b83\u5728 Python \u7684\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\u3002 \u9759\u6001\u65b9\u6cd5\u4e0d\u63a5\u6536\u4efb\u4f55\u5f15\u7528\u53c2\u6570\uff0c\u65e0\u8bba\u5b83\u662f\u7531\u7c7b\u7684\u5b9e\u4f8b\u8c03\u7528\u8fd8\u662f\u7531\u7c7b\u672c\u8eab\u8c03\u7528\u3002<\/p>\n<h2>@staticmethod \u7279\u6027<\/h2>\n<ul>\n<li>\u5728\u7c7b\u4e2d\u58f0\u660e\u9759\u6001\u65b9\u6cd5\u3002<\/li>\n<li>\u5b83\u4e0d\u80fd\u6709<code>cls<\/code>\u6216<code>self<\/code>\u53c2\u6570\u3002<\/li>\n<li>\u9759\u6001\u65b9\u6cd5\u65e0\u6cd5\u8bbf\u95ee\u7c7b\u5c5e\u6027\u6216\u5b9e\u4f8b\u5c5e\u6027\u3002<\/li>\n<li>\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528<code>ClassName.MethodName()<\/code>\u8c03\u7528\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>object.MethodName()<\/code>\u8c03\u7528\u3002<\/li>\n<li>\u5b83\u53ef\u4ee5\u8fd4\u56de\u7c7b\u7684\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u7684\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u5728\u7c7b\u4e2d\u5b9a\u4e49\u9759\u6001\u65b9\u6cd5:<\/p>\n<p>Example: Define Static Method <\/p>\n<pre><code>class Student:\n    name = 'unknown' # class attribute\n\n    def __init__(self):\n        self.age = 20  # instance attribute\n\n    @staticmethod\n    def tostring():\n        print('Student Class') <\/code><\/pre>\n<p>\u4e0a\u9762\uff0c<code>Student<\/code>\u7c7b\u4f7f\u7528<code>@staticmethod<\/code>\u88c5\u9970\u5668\u5c06<code>tostring()<\/code>\u65b9\u6cd5\u58f0\u660e\u4e3a\u9759\u6001\u65b9\u6cd5\u3002 \u6ce8\u610f\u4e0d\u80fd\u6709<code>self<\/code>\u6216<code>cls<\/code>\u53c2\u6570\u3002<\/p>\n<p>\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528<code>ClassName.MethodName()<\/code>\u6216<code>object.MethodName()<\/code>\u8c03\u7528\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/p>\n<p>Example: Calling Class Method using Object <\/p>\n<pre><code>&gt;&gt;&gt; Student.tostring()\n'Student Class'\n&gt;&gt;&gt; Student().tostring() \n'Student Class'\n&gt;&gt;&gt; std = Student()\n&gt;&gt;&gt; std.tostring()\n'Student Class' <\/code><\/pre>\n<p>\u9759\u6001\u65b9\u6cd5\u65e0\u6cd5\u8bbf\u95ee\u7c7b\u5c5e\u6027\u6216\u5b9e\u4f8b\u5c5e\u6027\u3002\u5982\u679c\u5c1d\u8bd5\u8fd9\u6837\u505a\uff0c\u5c06\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<p>Example: Static Method <\/p>\n<pre><code>class Student:\n    name = 'unknown' # class attribute\n\n    def __init__(self):\n        self.age = 20  # instance attribute\n\n    @staticmethod\n    def tostring():\n        print('name=',name,'age=',self.age) <\/code><\/pre>\n<p>\u5f53\u60a8\u8c03\u7528\u4e0a\u9762\u7684\u9759\u6001\u65b9\u6cd5\u65f6\uff0c\u4e0b\u9762\u5c06\u662f\u8f93\u51fa\u3002<\/p>\n<pre><code>&gt;&gt;&gt; Student.tostring()\nTraceback (most recent call last):\n  File \"&lt;pyshell#22&gt;\", line 1, in &lt;module&gt;\n    Student.tostring()\n  File \"&lt;pyshell#21&gt;\", line 7, in display\n    print('name=',name,'age=',self.age)\nNameError: name 'name' is not defined <\/code><\/pre>\n<h2>@classmethod vs @staticmethod<\/h2>\n<p>\u4e0b\u8868\u5217\u51fa\u4e86\u7c7b\u65b9\u6cd5\u4e0e\u9759\u6001\u65b9\u6cd5\u7684\u533a\u522b:<\/p>\n<table>\n<tbody>\n<tr>\n<th>@classmethod<\/th>\n<th>@staticmethod<\/th>\n<\/tr>\n<tr>\n<td>\u58f0\u660e\u4e00\u4e2a\u7c7b\u65b9\u6cd5\u3002<\/td>\n<td>\u58f0\u660e\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u5b83\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u5c5e\u6027\uff0c\u4f46\u4e0d\u80fd\u8bbf\u95ee\u5b9e\u4f8b\u5c5e\u6027\u3002<\/td>\n<td>\u5b83\u4e0d\u80fd\u8bbf\u95ee\u7c7b\u5c5e\u6027\u6216\u5b9e\u4f8b\u5c5e\u6027\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u53ef\u4ee5\u4f7f\u7528<code>ClassName.MethodName()<\/code>\u6216<code>object.MethodName()<\/code>\u6765\u8c03\u7528\u3002<\/td>\n<td>\u53ef\u4ee5\u4f7f\u7528<code>ClassName.MethodName()<\/code>\u6216<code>object.MethodName()<\/code>\u6765\u8c03\u7528\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u5b83\u53ef\u4ee5\u7528\u6765\u58f0\u660e\u8fd4\u56de\u7c7b\u5bf9\u8c61\u7684\u5de5\u5382\u65b9\u6cd5\u3002<\/td>\n<td>\u5b83\u53ef\u4ee5\u8fd4\u56de\u7c7b\u7684\u5bf9\u8c61\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>@staticmethod\u662f\u4e00\u4e2a\u5185\u7f6e\u7684\u88c5\u9970\u5668\uff0c\u5b83\u5728 Python \u7684\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\u3002 \u9759\u6001\u65b9\u6cd5\u4e0d\u63a5\u6536\u4efb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-183869","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/183869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=183869"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/183869\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=183869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=183869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=183869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}