{"id":153,"date":"2022-02-20T11:25:08","date_gmt":"2022-02-20T03:25:08","guid":{"rendered":"https:\/\/www.daogu.work\/?p=153"},"modified":"2022-02-20T14:28:38","modified_gmt":"2022-02-20T06:28:38","slug":"python%e4%b8%adclass%e7%9a%84__init__%e4%b8%8eself%e7%ae%80%e5%8d%95%e8%a7%a3%e9%87%8a","status":"publish","type":"post","link":"https:\/\/daogu.work\/?p=153","title":{"rendered":"python\u4e2dclass\u7684__init__\u4e0eself\u7b80\u5355\u89e3\u91ca"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u80cc\u666f\u7b80\u4ecb<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>python\u4e2d\u7684class,\u4e2d\u6587\u7ffb\u8bd1\u6210\u7c7b\u3002\u800c\u4e0e\u7c7b\u76f8\u5bf9\u5e94\u7684function\u5219\u79f0\u4e3a\u51fd\u6570\u3002\u4e4b\u524d\u4e00\u76f4\u5199\u51fd\u6570\uff0c\u4e0d\u600e\u4e48\u613f\u610f\u5199class\uff0c\u771f\u5b9e\u7684\u539f\u56e0\u662f\uff0c\u5728\u4f7f\u7528python\u4e4b\u524d\uff0c\u4f7f\u7528\u7684R\u8bed\u8a00\uff0c\u662f\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u5199\u6cd5\uff0c\u57fa\u672c\u6ca1\u600e\u4e48\u63a5\u89e6\u8fc7\u7c7b\u3002\u4e5f\u5c31\u4e0d\u77e5\u9053\u600e\u4e48\u5199\u7c7b\uff0c\u770b\u5230\u522b\u4eba\u7684\u4ee3\u7801\uff0c\u5bf9\u4e8eself\u548cinit\u8fd9\u4e24\u4e2a\u4e00\u76f4\u96be\u4ee5\u7406\u89e3\u3002<\/li><li>\u6b63\u597d\u6700\u8fd1\u8981\u5199class,\u7d22\u6027\u4e0d\u518d\u9003\u907f\uff0c\u6574\u7406\u4e00\u4e0b\u601d\u8def\uff0c\u8bb0\u5f55\u4e0b\u6765\u3002\u4e3b\u8981\u662f\u4e0b\u9762\u8fd9\u4e2a\u7f51\u7ad9\u8bb2\u5f97\u6bd4\u8f83\u901a\u4fd7\u6613\u61c2\uff0c\u6211\u4e5f\u4e3b\u8981\u662f\u5c06\u5b83\u7ffb\u8bd1\u8fc7\u6765\u4ecb\u7ecd\u4e00\u4e0b\u3002<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u4e2a\u7b80\u5355\u7684class\u662f\u4ec0\u4e48\u6837\u5b50\u7684\uff1f<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyNewClass:\n    '''This is a docstring. I have created a new class'''\n    pass<\/code><\/pre>\n\n\n\n<p>\u51fd\u6570\u662f\u7528def\u5b9a\u4e49\u7684\uff0c\u7c7b\u662f\u7528class\u5b9a\u4e49\u7684\u3002\u51fd\u6570\u662f\u6709\u53c2\u6570\u7684\uff0c\u7c7b\u4e0d\u9700\u8981\u52a0\u53c2\u6570\u548c\u62ec\u53f7\u3002<\/p>\n\n\n\n<p>\u8bf4\u7c7b\u4e0d\u9700\u8981\u52a0\u53c2\u6570\u548c\u62ec\u53f7\u8fd9\u8bdd\u5e76\u4e0d\u51c6\u786e\uff0c\u5176\u5b9e\u662f\u7701\u7565\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyNewClass(Object):<\/code><\/pre>\n\n\n\n<p>\u5176\u5b9e\u5b8c\u6574\u7684\u5199\u6cd5\u662f\u4e0a\u9762\u7684\u8fd9\u79cd\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u600e\u4e48\u5199class?<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u7406\u89e3<code>__init__()<\/code>\u51fd\u6570<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>init<\/strong> \u4ee5\u5efa\u9020\u623f\u5c4b\u4e3a\u4f8b<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7528\u6765\u521d\u59cb\u5316\u5b9e\u4f8b<\/li><li>\u4efb\u4f55\u7528\u6b64\u7c7b\u8fdb\u884c\u6784\u5efa\u7684\u5b9e\u4f8b\uff0c\u90fd\u4f1a\u6709\u8fd9\u4e9b\u7279\u5f81<\/li><li>\u5728\u7c7b\u751f\u6210\u4e00\u4e2a\u5b9e\u4f8b\u65f6(\u901a\u8fc7\u56fe\u7eb8\u8bbe\u8ba1\u7684\u623f\u5c4b\uff0c\u6784\u5efa\u597d\u4e4b\u540e\uff0c\u90fd\u4f1a\u914d\u5907\u57fa\u672c\u7684\u53a8\u536b\u8bbe\u65bd\uff0c\u90fd\u662f\u5f00\u53d1\u5546\u5b9a\u5236\u7684)\uff0c\u4f1a\u9996\u5148\u8fd0\u884c<strong>init<\/strong>\u51fd\u6570\u91cc\u9762\u7684\u5185\u5bb9\u3002<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u7406\u89e3__\u5f00\u5934\u7684\u51fd\u6570<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">__\u5f00\u5934\u7684\u51fd\u6570(\u79c1\u6709\u51fd\u6570\uff0c\u5c40\u90e8\u53d8\u91cf)<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>\u53ea\u6709\u5728\u8fd9\u4e2a\u7c7b\u91cc\u9762\u624d\u53ef\u4ee5\u4f7f\u7528\uff0c\u5728\u751f\u6210\u5b9e\u4f8b\u540e\uff0c\u65e0\u6cd5\u901a\u8fc7\u5b9e\u4f8b\u6765\u8c03\u7528\u6b64\u51fd\u6570\u3002<\/li><li><strong>init<\/strong>\u4e5f\u662f\u4e24\u9053\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u610f\u5473\u7740\u4e0d\u80fd\u901a\u8fc7\u5916\u8c03\u7528<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u7406\u89e3self<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u8981\u7406\u89e3self\uff0c\u8981\u5148\u7406\u89e3\u7c7b\u7684\u8fd0\u884c\u673a\u5236<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>self\u662f\u4e00\u79cd\u901a\u4fd7\u7684\u7ea6\u5b9a\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u7528self,\u53ea\u4e0d\u8fc7\u5927\u5bb6\u90fd\u4f7f\u7528\u8fd9\u4e2a\u4f5c\u4e3a\u9ed8\u8ba4\u7684\u53c2\u6570<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">self\u7684\u4f5c\u7528(\u4ee5\u5efa\u9020\u623f\u5c4b\u4e3a\u4f8b)<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7c7b\u53ef\u4ee5\u76f8\u5f53\u4e8e\u8bbe\u8ba1\u56fe\u7eb8<\/li><li>\u5b9e\u4f8b\u5c31\u662f\u5efa\u597d\u7684\u5177\u4f53\u623f\u5b50<\/li><li>\u800cself\u5c31\u662f\u5b9e\u4f8b\uff0c\u5177\u4f53\u7684\u623f\u5b50<\/li><li>\u5047\u5b9a\u6709a,b,c\u4e09\u5bb6\uff0c\u90a3\u4e48\u5728\u751f\u6210\u5b9e\u4f8b\u7684\u65f6\u5019\uff0c\u7b2c\u4e00\u4e2a\u5b9e\u4f8ba\u5728\u7c7b\u5b9e\u4f8b\u5316\u7684\u8fc7\u7a0b\u4e2d\uff0cself\u5c31\u7b49\u4e8ea,\u5728b\u8fdb\u884c\u7c7b\u5b9e\u4f8b\u5316\u7684\u8fc7\u7a0b\u4e2d\uff0cself\u5c31\u7b49\u4e8eb<\/li><li>\u4e5f\u53ef\u4ee5\u7406\u89e3\u6210self\u5c31\u662f\u4e00\u4e2a\u7c7b\u4e2d\u7684\u5168\u5c40\u53d8\u91cf<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003\u6587\u732e<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.programiz.com\/python-programming\/class#\/\">Python Objects and Classes<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f\u7b80\u4ecb python\u4e2d\u7684class,\u4e2d\u6587\u7ffb\u8bd1\u6210\u7c7b\u3002\u800c\u4e0e\u7c7b\u76f8\u5bf9\u5e94\u7684function\u5219\u79f0\u4e3a\u51fd\u6570\u3002\u4e4b\u524d\u4e00\u76f4\u5199\u51fd\u6570\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[33,7,32],"class_list":["post-153","post","type-post","status-publish","format-standard","hentry","category-unclassified","tag-class","tag-python","tag-self"],"_links":{"self":[{"href":"https:\/\/daogu.work\/index.php?rest_route=\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daogu.work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daogu.work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daogu.work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daogu.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=153"}],"version-history":[{"count":2,"href":"https:\/\/daogu.work\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":155,"href":"https:\/\/daogu.work\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions\/155"}],"wp:attachment":[{"href":"https:\/\/daogu.work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daogu.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daogu.work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}