{"id":19962,"date":"2023-03-07T22:07:25","date_gmt":"2023-03-07T14:07:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=19962"},"modified":"2023-07-30T23:05:07","modified_gmt":"2023-07-30T15:05:07","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88python%e5%9c%a8%e7%b1%bb%e4%b8%8b%e5%ae%9a%e4%b9%89%e5%87%bd%e6%95%b0%e8%a6%81%e7%94%a8self%e5%8f%82%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/19962.html","title":{"rendered":"\u4e3a\u4ec0\u4e48Python\u5728\u7c7b\u4e0b\u5b9a\u4e49\u51fd\u6570\u8981\u7528self\u53c2\u6570"},"content":{"rendered":"<blockquote class=\"wp-block-quote\">\n<p>Python\u5728\u7c7b\u4e0b\u5b9a\u4e49\u51fd\u6570\u8981\u7528self\u53c2\u6570\u662f\u56e0\u4e3a\u7c7b\u7684\u65b9\u6cd5\u4e0e\u666e\u901a\u7684\u51fd\u6570\u7279\u522b\u7684\u533a\u522b\u662f\u5fc5\u987b\u6709\u4e00\u4e2a\u989d\u5916\u7684\u540d\u5217\u524d\u8305\u4e2a\u53c2\u6570\u540d\u79f0\uff0c\u4f46\u662f\u5728\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u65f6\u4e0d\u9700\u4e3a\u8fd9\u4e2a\u53c2\u6570\u8d4b\u503c\uff0cPython\u4f1a\u63d0\u4f9b\u8fd9\u4e2a\u503c\u3002\u8fd9\u4e2a\u7279\u522b\u7684\u53d8\u91cf\u6307\u5bf9\u8c61\u672c\u8eab\uff0c\u6309\u7167\u60ef\u4f8b\u5b83\u7684\u540d\u79f0\u662fself\u3002<\/p>\n<\/blockquote>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img fetchpriority=\"high\" alt=\"\" class=\"wp-image-19963\" decoding=\"async\" height=\"421\" sizes=\"(max-width: 680px) 100vw, 680px\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/03\/\u6dfb\u52a0\u6807\u9898-7-2.png?x-oss-process=image\/auto-orient,1\/format,webp\" srcset=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/03\/\u6dfb\u52a0\u6807\u9898-7-2.png?x-oss-process=image\/auto-orient,1\/format,webp 680w, https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/03\/\u6dfb\u52a0\u6807\u9898-7-2-300x186.png 300w\" width=\"680\"\/><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001Python\u5728\u7c7b\u4e0b\u5b9a\u4e49\u51fd\u6570\u8981\u7528self\u53c2\u6570<\/strong><\/h2>\n<p>Python\u5728\u7c7b\u4e0b\u5b9a\u4e49\u51fd\u6570\u8981\u7528self\u53c2\u6570\u662f\u56e0\u4e3a\u7c7b\u7684\u65b9\u6cd5\u4e0e\u666e\u901a\u7684\u51fd\u6570\u7279\u522b\u7684\u533a\u522b\u662f\u5fc5\u987b\u6709\u4e00\u4e2a\u989d\u5916\u7684\u540d\u5217\u524d\u8305\u4e2a\u53c2\u6570\u540d\u79f0\uff0c\u4f46\u662f\u5728\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u65f6\u4e0d\u9700\u4e3a\u8fd9\u4e2a\u53c2\u6570\u8d4b\u503c\uff0cPython\u4f1a\u63d0\u4f9b\u8fd9\u4e2a\u503c\u3002\u8fd9\u4e2a\u7279\u522b\u7684\u53d8\u91cf\u6307\u5bf9\u8c61\u672c\u8eab\uff0c\u6309\u7167\u60ef\u4f8b\u5b83\u7684\u540d\u79f0\u662fself\u3002<\/p>\n<p>\u867d\u7136\u4f60\u53ef\u4ee5\u7ed9\u8fd9\u4e2a\u53c2\u6570\u4efb\u4f55\u540d\u79f0\uff0c\u4f46\u662f\u00a0\u5f3a\u70c8\u5efa\u8bae\u00a0\u4f60\u4f7f\u7528self\u8fd9\u4e2a\u540d\u79f0\u2014\u2014\u5176\u4ed6\u540d\u79f0\u90fd\u662f\u4e0d\u8d5e\u6210\u4f60\u4f7f\u7528\u7684\u3002\u4f7f\u7528\u4e00\u4e2a\u6807\u51c6\u7684\u540d\u79f0\u6709\u5f88\u591a\u4f18\u70b9\u2014\u2014\u4f60\u7684\u7a0b\u5e8f\u8bfb\u8005\u53ef\u4ee5\u8fc5\u901f\u8bc6\u522b\u5b83\uff0c\u5982\u679c\u4f7f\u7528self\u7684\u8bdd\uff0c\u8fd8\u6709\u4e9bIDE\uff08\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff09\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u3002<\/p>\n<p>Python\u4e2d\u7684self\u7b49\u4ef7\u4e8eC++\u4e2d\u7684self\u6307\u9488\u548cJava\u3001C#\u4e2d\u7684this\u53c2\u8003\u3002<\/p>\n<p>\u4f60\u4e00\u5b9a\u5f88\u5947\u602aPython\u5982\u4f55\u7ed9self\u8d4b\u503c\u4ee5\u53ca\u4e3a\u4f55\u4f60\u4e0d\u9700\u8981\u7ed9\u5b83\u8d4b\u503c\u3002\u4e3e\u4e00\u4e2a\u4f8b\u5b50\u4f1a\u4f7f\u6b64\u53d8\u5f97\u6e05\u6670\u3002\u5047\u5982\u4f60\u6709\u4e00\u4e2a\u7c7b\u79f0\u4e3aMyClass\u548c\u8fd9\u4e2a\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8bMyObject\u3002\u5f53\u4f60\u8c03\u7528\u8fd9\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5MyObject.method(arg1, arg2)\u7684\u65f6\u5019\uff0c\u8fd9\u4f1a\u7531Python\u81ea\u52a8\u8f6c\u4e3aMyClass.method(MyObject, arg1, arg2)\u2014\u2014\u8fd9\u5c31\u662fself\u7684\u539f\u7406\u4e86\u3002<\/p>\n<p>\u8fd9\u4e5f\u610f\u5473\u7740\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u4e0d\u9700\u8981\u53c2\u6570\u7684\u65b9\u6cd5\uff0c\u4f60\u8fd8\u662f\u5f97\u7ed9\u8fd9\u4e2a\u65b9\u6cd5\u5b9a\u4e49\u4e00\u4e2aself\u53c2\u6570\u3002<\/p>\n<p>\u6211\u8fd9\u91cc\u8865\u5145\u4e00\u4e0b\u51e0\u4e2a\u5b9e\u4f8b\u7a0b\u5e8f\u4ee5\u52a0\u6df1\u7406\u89e3\uff1a<\/p>\n<p>PHP<\/p>\n<figure class=\"wp-block-table\">\n<table>\n<tbody>\n<tr>\n<td>1 2 3 4 5<\/td>\n<td>class Person: def sayHi(self): \u00a0\u00a0print \u2018Hello,how are you?\u2019 p=Person() p.sayHi()<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>\u7ed3\u679c\u663e\u793a\u4e3a\uff1a<\/p>\n<p>Hello,how are you?<\/p>\n<p>\u6539\u4e00\u4e0b<\/p>\n<p>PHP<\/p>\n<figure class=\"wp-block-table\">\n<table>\n<tbody>\n<tr>\n<td>1 2 3 4 5<\/td>\n<td>class Person: def sayHi(self): \u00a0\u00a0print \u2018Hello,how are you?\u2019 p=Person() Person.sayHi(p)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>\u7ed3\u662f\u540c\u4e0a\uff01<\/p>\n<p>\u518d\u6539\u4e00\u4e0b\uff1a\uff08\u53bb\u6389self,\u4f1a\u600e\u4e48\u6837\u5462\uff1f\uff09<\/p>\n<p>PHP<\/p>\n<figure class=\"wp-block-table\">\n<table>\n<tbody>\n<tr>\n<td>1 2 3 4 5 6<\/td>\n<td>class Person: def sayHi(): \u00a0\u00a0print \u2018Hello,how are you?\u2019 p=Person() Person.sayHi(p) p.sayHi()<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>\u51fa\u73b0\u5982\u4e0b\u9519\u8bef\u4fe1\u606f\uff1a<\/p>\n<p>Traceback (most recent call last):<br \/>File \u201cC:\\pro\\Person2.py\u201d, line 5, in ?<br \/>Person.sayHi(p)<br \/>TypeError: sayHi() takes no arguments (1 given)<\/p>\n<p>__name__\u662f\u7528\u6765\u8bc6\u522b\u4e00\u4e2a\u6a21\u5757\u662f\u76f4\u63a5\u8fd0\u884c\u8fd8\u662f\u4f5c\u4e3a\u4e00\u822c\u7684\u6a21\u5757\u88ab\u5bfc\u5165\u7684\u72b6\u6001\u3002\u5f53\u4e00\u4e2a\u6a21\u5757\u662f\u76f4\u63a5\u8fd0\u884c\u65f6\uff0c__name__\u5c31\u7b49\u4e8e__main__\uff0c\u5982\u679c\u5b83\u662f\u4f5c\u4e3a\u4e00\u822c\u6a21\u5757\u88ab\u5bfc\u5165\u65f6\uff0c__name__\u5c31\u662f\u6a21\u5757\u672c\u8eab\u7684\u540d\u5b57\u3002<\/p>\n<p>\u793a\u4f8b:<\/p>\n<p>PHP<\/p>\n<figure class=\"wp-block-table\">\n<table>\n<tbody>\n<tr>\n<td>1 2<\/td>\n<td>if __name__ == \u201c__main__\u201d: run()<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>self\u662f\u4e00\u79cd\u7ea6\u5b9a\u3002\u5728Python\u4e2d\uff0c\u7c7b\u65b9\u6cd5\u7684\u540d\u5217\u524d\u8305\u4e2a\u53c2\u6570\u8868\u793a\u5bf9\u8c61\u672c\u8eab\uff0c\u5728Python\u4e2d\u4e00\u822c\u4f7f\u7528self\u3002\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528this.<\/p>\n<p>\u793a\u4f8b:<\/p>\n<p>a\u662f\u4f60\u60f3\u7528\u7684\u65b9\u6cd5\u3002\u4f46self\u662f\u4e00\u79cd\u7ea6\u5b9a\u3002\u5728\u8c03\u7528\u4e00\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u65f6\uff0c\u5bf9\u8c61\u672c\u8eab\u88ab\u4f5c\u4e3aself\u53c2\u6570\u4f20\u5165\u3002\u5982\uff1a<\/p>\n<p>PHP<\/p>\n<figure class=\"wp-block-table\">\n<table>\n<tbody>\n<tr>\n<td>1 2 3 4 5<\/td>\n<td>class A: \u00a0\u00a0\u00a0\u00a0def p(self, name): \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0print name a=A() a.p(\u2018aa\u2019)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>\u8fd9\u65f6\uff0c\u76f8\u5f53\u4e8eA.p(a, \u2018aa\u2019)<\/p>\n<p>Python\u7684\u5bf9\u8c61\u4e0d\u4e00\u5b9a\u8981\u6709__init__\u65b9\u6cd5\uff0c\u65e0\u8bba\u5b83\u662f\u57fa\u7c7b\u8fd8\u662f\u5b50\u7c7b\u3002\u56e0\u4e3a__init__\u4e0d\u662f\u6784\u9020\u51fd\u6570\uff0cPython\u5bf9\u8c61\u5728\u8c03\u7528__init__\u7684\u65f6\u5019\u5df2\u88ab\u6784\u9020\u51fa\u6765\uff0c\u79f0\u4e3a\u201c\u521d\u59cb\u5316\u201d\u6bd4\u8f83\u5408\u9002\u3002<\/p>\n<p>\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u662f\u5b50\u7c7b\uff0c\u4e14\u9700\u8981\u6709__init__\uff0c\u90a3\u4e48\u5728__init__\u7684\u540d\u5217\u524d\u8305\u4e2a\u8bed\u53e5\u5c31\u662f\u7236\u7c7b\u7684__init__\uff08\u9664\u975e\u7236\u7c7b\u662f\u50cfdict\u4e00\u6837\u7684\u5185\u7f6e\u6570\u636e\u7c7b\u578b\u5219\u4e0d\u9700\u8981__init__\uff09\u3002\u8c03\u7528\u5b8c\u540e\u518d\u8fdb\u884c\u5b50\u7c7b\u7684\u76f8\u5173\u521d\u59cb\u5316\uff0c\u4f5c\u4e3a\u65b9\u6cd5\u8986\u76d6\u3002<\/p>\n<p>\u4e00\u822c\u5728\u5b50\u7c7b\u8986\u76d6\u201c\u4e13\u6709\u7c7b\u65b9\u6cd5\u201d\uff0c\u5e76\u89e6\u53d1\u76f8\u5173\u52a8\u4f5c\uff0c\u8fd9\u6837\u4e0d\u5fc5\u4f7f\u7528__init__\u4e5f\u80fd\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u521d\u59cb\u5316\u3002<\/p>\n<p>\u5982\u679c\u5b50\u7c7b\u7684\u65b9\u6cd5\u8986\u76d6\u4e86\u7236\u7c7b\uff0c\u60f3\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u600e\u4e48\u5417\uff1fPython\u6ca1\u6709super\u554a\u3002\u5f88\u7b80\u5355\uff0c\u76f4\u63a5\u201c\u7236\u7c7b\u540d.\u65b9\u6cd5\u201d\u5373\u53ef\u3002<\/p>\n<p>\u4e00\u822c\u5728\u7c7b\u5b9a\u4e49\u4e2d\u65b9\u6cd5\u4e4b\u5916\u7684\u5c5e\u6027\u53eb\u201c\u7c7b\u5c5e\u6027\u201d\uff0c\u5b83\u8ddf\u5b9a\u4e49\u5728\u65b9\u6cd5\u5185\u7684\u5c5e\u6027\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f\u201c \u7c7b\u5c5e\u6027\u201d\u53ef\u4ee5\u4f7f\u5f97\u4e0d\u5fc5\u751f\u6210\u5b9e\u4f8b\u4e5f\u53ef\u4ee5\u8c03\u7528\uff0c\u800c\u5d4c\u5728\u65b9\u6cd5\u5185\u7684\uff08\u7279\u522b\u662f\u7531__init__\u521d\u59cb\u5316\u540e\u4ea7\u751f\u7684\u5c5e\u6027\uff09\uff0c\u4e00\u5b9a\u5f97\u4ea7\u751f\u5b9e\u4f8b\u624d\u53ef\u4ee5\u8c03\u7528\u4e4b\u3002<\/p>\n<p>\u6240\u6709\u7684\u7c7b\u7684\u5b9e\u4f8b\u90fd\u4eab\u6709\u7edf\u4e00\u7684\u201c\u7c7b\u5c5e\u6027\u201d\uff0c\u90a3\u8fd9\u201c\u7c7b\u5c5e\u6027\u201d\u662f\u5426\u5c31\u662f\u7c7b\u7684\u5e38\u91cf\uff1f\u4e0d\u662f\uff01\u901a\u8fc7\u5728\u4efb\u610f\u5b9e\u4f8b\u8c03\u7528self.__class__.NAME\u5c31\u53ef\u4ee5\u4fee\u6539\u201c\u7c7b\u5c5e\u6027\u201d\uff0c\u4fee\u6539\u540e\u6240\u6709\u7684\u5b9e\u4f8b\u90fd\u5c06\u5e94\u7528\u8fd9\u4e00\u65b0\u7684\u201c\u7c7b\u5c5e\u6027\u201d\u3002self.__class__\u5373\u4ee3\u8868\u7c7b\u7684\u5f15\u7528\uff0c\u4e0d\u4ee3\u8868\u4efb\u4f55\u5b9e\u4f8b\uff0c\u5b9e\u4f8b\u5c31\u662fself\u3002<\/p>\n<p><strong>\u5ef6\u4f38\u9605\u8bfb\uff1a<\/strong><\/p>\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001\u5783\u573e\u56de\u6536\u673a\u5236<\/strong><\/h2>\n<p>\u5f53\u5185\u5b58\u4e2d\u6709\u4e0d\u518d\u4f7f\u7528\u7684\u90e8\u5206\u65f6\uff0c\u5783\u573e\u6536\u96c6\u5668\u5c31\u4f1a\u628a\u4ed6\u4eec\u6e05\u7406\u6389\u3002\u5b83\u4f1a\u53bb\u68c0\u67e5\u90a3\u4e9b\u5f15\u7528\u8ba1\u6570\u4e3a0\u7684\u5bf9\u8c61\uff0c\u7136\u540e\u6e05\u9664\u5176\u5728\u5185\u5b58\u7684\u7a7a\u95f4\u3002\u5f53\u7136\u9664\u4e86\u5f15\u7528\u8ba1\u6570\u4e3a0\u7684\u4f1a\u88ab\u6e05\u9664\uff0c\u8fd8\u6709\u4e00\u79cd\u60c5\u51b5\u4e5f\u4f1a\u88ab\u5783\u573e\u6536\u96c6\u5668\u6e05\u6389\uff1a\u5f53\u4e24\u4e2a\u5bf9\u8c61\u76f8\u4e92\u5f15\u7528\u65f6\uff0c\u4ed6\u4eec\u672c\u8eab\u5176\u4ed6\u7684\u5f15\u7528\u5df2\u7ecf\u4e3a0\u4e86\u3002<\/p>\n<p>\u5783\u573e\u56de\u6536\u673a\u5236\u8fd8\u6709\u4e00\u4e2a\u5faa\u73af\u5783\u573e\u56de\u6536\u5668, \u786e\u4fdd\u91ca\u653e\u5faa\u73af\u5f15\u7528\u5bf9\u8c61(a\u5f15\u7528b, b\u5f15\u7528a, \u5bfc\u81f4\u5176\u5f15\u7528\u8ba1\u6570\u6c38\u8fdc\u4e0d\u4e3a0)\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5728\u7c7b\u4e0b\u5b9a\u4e49\u51fd\u6570\u8981\u7528self\u53c2\u6570\u662f\u56e0\u4e3a\u7c7b\u7684\u65b9\u6cd5\u4e0e\u666e\u901a\u7684\u51fd\u6570\u7279\u522b\u7684\u533a\u522b\u662f\u5fc5\u987b\u6709\u4e00\u4e2a\u989d\u5916\u7684\u540d\u5217\u524d\u8305\u4e2a\u53c2\u6570 [&hellip;]","protected":false},"author":1,"featured_media":19963,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/19962"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=19962"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/19962\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/19963"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=19962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=19962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=19962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}