{"id":3506,"date":"2025-06-01T15:05:17","date_gmt":"2025-06-04T07:29:16","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3506"},"modified":"2025-06-04T15:05:17","modified_gmt":"2025-06-04T07:29:16","slug":"%e5%ad%b8%e7%bf%92python%e4%b8%ad%e7%9a%84self%e5%87%bd%e6%95%b8%ef%bc%9a%e6%b7%b1%e5%85%a5%e4%ba%86%e8%a7%a3%e5%ae%83%e7%9a%84%e5%8a%9f%e8%83%bd%e8%88%87%e6%87%89%e7%94%a8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e5%ad%b8%e7%bf%92python%e4%b8%ad%e7%9a%84self%e5%87%bd%e6%95%b8%ef%bc%9a%e6%b7%b1%e5%85%a5%e4%ba%86%e8%a7%a3%e5%ae%83%e7%9a%84%e5%8a%9f%e8%83%bd%e8%88%87%e6%87%89%e7%94%a8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 self \u53c3\u6578\uff1a\u529f\u80fd\u3001\u61c9\u7528\u8207\u6700\u4f73\u5be6\u8e10\uff082025 \u66f4\u65b0\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, self, self \u53c3\u6578, Python \u7a0b\u5f0f\u6559\u5b78\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 self \u53c3\u6578<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`self` \u662f\u4e00\u500b\u7279\u6b8a\u7684\u53c3\u6578\uff0c\u7528\u65bc\u5728\u985e\u5225\u4e2d\u5b58\u53d6\u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002\u6bcf\u7576\u4f60\u5728\u985e\u5225\u4e2d\u5b9a\u7fa9\u4e00\u500b\u65b9\u6cd5\u6642\uff0c`self` \u53c3\u6578\u6703\u81ea\u52d5\u50b3\u5165\uff0c\u9019\u8b93\u4f60\u80fd\u5920\u5f15\u7528\u7576\u524d\u5be6\u4f8b\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u4ec0\u9ebc\u662f self \u53c3\u6578\uff1f<\/h2>\n<p>`self` \u4e26\u4e0d\u662f Python \u7684\u95dc\u9375\u5b57\uff0c\u800c\u662f\u4e00\u500b\u6163\u4f8b\u7528\u4f86\u4ee3\u8868\u985e\u5225\u7684\u5be6\u4f8b\u3002\u5b83\u4f7f\u4f60\u80fd\u5920\u5728\u985e\u5225\u5167\u90e8\u8a2a\u554f\u5c6c\u6027\u548c\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u8003\u616e\u4ee5\u4e0b\u985e\u5225\u5b9a\u7fa9\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def my_method(self):<br \/>\n        print(&#8220;Hello World!&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c`my_method` \u65b9\u6cd5\u4e2d\u7684 `self` \u53c3\u6578\u4f7f\u4f60\u80fd\u5920\u8a2a\u554f\u540c\u4e00\u985e\u5225\u4e2d\u7684\u5176\u4ed6\u5c6c\u6027\u6216\u65b9\u6cd5\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 self \u53c3\u6578<\/h2>\n<p>\u5728 Python \u4e2d\uff0c\u7576\u4f60\u5275\u5efa\u4e00\u500b\u985e\u5225\u4e26\u5b9a\u7fa9\u65b9\u6cd5\u6642\uff0c\u5fc5\u9808\u5c07 `self` \u4f5c\u70ba\u7b2c\u4e00\u500b\u53c3\u6578\u50b3\u905e\u3002\u9019\u662f\u5fc5\u8981\u7684\uff0c\u56e0\u70ba\u5b83\u544a\u8a34 Python \u9019\u500b\u65b9\u6cd5\u662f\u91dd\u5c0d\u54ea\u500b\u5be6\u4f8b\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u66f4\u5b8c\u6574\u7684\u793a\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __init__(self, name):<br \/>\n        self.name = name  # \u4f7f\u7528 self \u4f86\u5efa\u7acb\u5be6\u4f8b\u5c6c\u6027<\/p>\n<p>    def greet(self):<br \/>\n        print(f&#8221;Hello, {self.name}!&#8221;)  # \u4f7f\u7528 self \u4f86\u8a2a\u554f\u5be6\u4f8b\u5c6c\u6027<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c`__init__` \u65b9\u6cd5\u662f\u521d\u59cb\u5316\u65b9\u6cd5\uff0c`self.name` \u662f\u4e00\u500b\u5c6c\u6027\uff0c`greet` \u65b9\u6cd5\u4f7f\u7528 `self` \u8a2a\u554f\u9019\u500b\u5c6c\u6027\u3002<\/p>\n<h2>\u4f7f\u7528 self \u7684\u512a\u9ede<\/h2>\n<p>\u4f7f\u7528 `self` \u53c3\u6578\u7684\u4e3b\u8981\u512a\u9ede\u5305\u62ec\uff1a<\/p>\n<p>1. **\u7c21\u6f54\u6027**\uff1a\u4f60\u4e0d\u9700\u8981\u5728\u6bcf\u500b\u65b9\u6cd5\u4e2d\u986f\u5f0f\u50b3\u905e\u5be6\u4f8b\u3002<br \/>\n2. **\u53ef\u8b80\u6027**\uff1a\u8b93\u7a0b\u5f0f\u78bc\u66f4\u52a0\u6e05\u6670\uff0c\u56e0\u70ba\u65b9\u6cd5\u660e\u78ba\u544a\u8a34\u4f60\u5b83\u662f\u91dd\u5c0d\u54ea\u500b\u5be6\u4f8b\u3002<br \/>\n3. **\u9748\u6d3b\u6027**\uff1a\u53ef\u4ee5\u5728\u540c\u4e00\u985e\u5225\u4e2d\u5b9a\u7fa9\u591a\u500b\u65b9\u6cd5\u800c\u7121\u9700\u91cd\u8907\u53c3\u6578\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u5e38\u898b\u554f\u984c<\/h2>\n<p>1. **\u5fd8\u8a18\u4f7f\u7528 self**\uff1a\u5982\u679c\u5728\u65b9\u6cd5\u4e2d\u5fd8\u8a18\u4f7f\u7528 `self` \u53c3\u6578\uff0c\u6703\u5c0e\u81f4 `NameError` \u932f\u8aa4\uff0c\u56e0\u70ba Python \u7121\u6cd5\u8b58\u5225\u5f9e\u672a\u5b9a\u7fa9\u7684\u8b8a\u6578\u3002<br \/>\n2. **\u4f7f\u7528 self \u4e4b\u5f8c\u7684\u932f\u8aa4**\uff1a\u78ba\u4fdd\u5728\u985e\u5225\u5167\u90e8\u6b63\u78ba\u4f7f\u7528 `self`\uff0c\u4f8b\u5982\u5728\u65b9\u6cd5\u5167\u90e8\u8981\u4f7f\u7528 `self.attribute` \u4f86\u8a2a\u554f\u5c6c\u6027\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u4e86\u89e3 `self` \u53c3\u6578\u5f8c\uff0c\u4f60\u53ef\u4ee5\u9032\u4e00\u6b65\u63a2\u7d22\u985e\u5225\u7684\u7e7c\u627f\u8207\u591a\u578b\u6027\u5982\u4f55\u8207 `self` \u4e00\u8d77\u5de5\u4f5c\u3002\u6b64\u5916\uff0c\u5b78\u7fd2\u5982\u4f55\u5728\u985e\u5225\u4e2d\u4f7f\u7528\u5c6c\u6027\u88dd\u98fe\u5668\uff08\u5982 `@property`\uff09\u4f86\u9032\u4e00\u6b65\u63d0\u5347\u4f60\u7684 Python \u6280\u80fd\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u5b78\u7fd2 Python \u7684\u9032\u968e\u4e3b\u984c\uff0c\u8acb\u53c3\u8003\u9019\u7bc7\u6559\u5b78\uff1a[Python \u985e\u5225\u8207\u7269\u4ef6\u5c0e\u5411](https:\/\/vocus.cc\/article\/6050b9c2b29bda001c1a2334)\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>`self` \u53c3\u6578\u5728 Python \u985e\u5225\u4e2d\u81f3\u95dc\u91cd\u8981\uff0c\u5b83\u4f7f\u4f60\u80fd\u5920\u9748\u6d3b\u5730\u5b58\u53d6\u548c\u64cd\u4f5c\u7269\u4ef6\u7684\u5c6c\u6027\u8207\u65b9\u6cd5\u3002\u6b63\u78ba\u4f7f\u7528 `self` \u53ef\u4ee5\u589e\u5f37\u7a0b\u5f0f\u78bc\u7684\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: \u70ba\u4ec0\u9ebc\u4e00\u5b9a\u8981\u5728\u65b9\u6cd5\u4e2d\u4f7f\u7528 self \u53c3\u6578\uff1f**<br \/>\nA1: `self` \u53c3\u6578\u4f7f\u4f60\u80fd\u5920\u8a2a\u554f\u7576\u524d\u5be6\u4f8b\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u8b93 Python \u77e5\u9053\u4f60\u7684\u65b9\u6cd5\u662f\u91dd\u5c0d\u54ea\u500b\u5be6\u4f8b\u7684\u3002<\/p>\n<p>**Q2: self \u548c\u985e\u5225\u540d\u7a31\u6709\u4ec0\u9ebc\u5340\u5225\uff1f**<br \/>\nA2: `self` \u662f\u6307\u7576\u524d\u5be6\u4f8b\u7684\u5f15\u7528\uff0c\u800c\u985e\u5225\u540d\u7a31\u662f\u985e\u5225\u7684\u975c\u614b\u5f15\u7528\u3002`self` \u5141\u8a31\u4f60\u5728\u5be6\u4f8b\u4e2d\u64cd\u4f5c\u5c6c\u6027\uff0c\u800c\u985e\u5225\u540d\u7a31\u5247\u7528\u65bc\u8a2a\u554f\u985e\u5225\u5c64\u7d1a\u7684\u65b9\u6cd5\u548c\u5c6c\u6027\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u5728\u985e\u5225\u4e2d\u4f7f\u7528\u591a\u500b\u65b9\u6cd5\uff1f**<br \/>\nA3: \u4f60\u53ef\u4ee5\u5728\u985e\u5225\u4e2d\u5b9a\u7fa9\u591a\u500b\u65b9\u6cd5\uff0c\u4e26\u5728\u6bcf\u500b\u65b9\u6cd5\u4e2d\u4f7f\u7528 `self` \u4f86\u5f15\u7528\u76f8\u540c\u7684\u5be6\u4f8b\u5c6c\u6027\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5b9a\u7fa9\u4e00\u500b `set_name` \u65b9\u6cd5\u4f86\u6539\u8b8a `name` \u5c6c\u6027\uff0c\u7136\u5f8c\u4f7f\u7528 `greet` \u65b9\u6cd5\u4f86\u986f\u793a\u66f4\u65b0\u5f8c\u7684\u540d\u7a31\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c07\u6df1\u5165\u89e3\u91cbPython\u4e2d\u7684self()\u51fd\u6578\uff0c\u8a73\u7d30\u4ecb\u7d39\u5b83\u7684\u529f\u80fd\u8207\u61c9\u7528\uff0c\u8b93\u5b78\u751f\u80fd\u5920\u66f4\u52a0\u4e86\u89e3\u5b83\u7684\u7528\u9014\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u7bc4\u4f8b\u4f86\u8b93\u5b78\u751f\u66f4\u52a0\u6df1\u5165\u7684\u7406\u89e3\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-3506","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-Uy","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3506","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=3506"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3506\/revisions"}],"predecessor-version":[{"id":3507,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3506\/revisions\/3507"}],"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=3506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}