{"id":181895,"date":"2023-07-31T18:22:12","date_gmt":"2023-07-31T10:22:12","guid":{"rendered":"http:\/\/www.idc.net\/help\/181895\/"},"modified":"2023-07-31T18:22:12","modified_gmt":"2023-07-31T10:22:12","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython%e7%b1%bb%e5%af%b9%e8%b1%a1%e5%92%8c%e5%ae%9e%e4%be%8b%e5%af%b9%e8%b1%a1%e6%98%af%e4%b8%80%e6%a0%b7%e7%9a%84%e5%90%97","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/181895\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1apython\u7c7b\u5bf9\u8c61\u548c\u5b9e\u4f8b\u5bf9\u8c61\u662f\u4e00\u6837\u7684\u5417"},"content":{"rendered":"<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/article\/000\/000\/004\/5e6731f935787109.jpg\" class=\"aligncenter\"><\/p>\n<p>\u9762\u5411\u5bf9\u8c61\u6700\u91cd\u8981\u7684\u6982\u5ff5\u5c31\u662f\u7c7b\uff08Class\uff09\u548c\u5b9e\u4f8b\uff08Instance\uff09\uff0c\u5fc5\u987b\u7262\u8bb0\u7c7b\u662f\u62bd\u8c61\u7684\u6a21\u677f\uff0c\u6bd4\u5982Student\u7c7b\uff0c\u800c\u5b9e\u4f8b\u662f\u6839\u636e\u7c7b\u521b\u5efa\u51fa\u6765\u7684\u4e00<\/p>\n<p>\u4e2a\u4e2a\u5177\u4f53\u7684\u201c\u5bf9\u8c61\u201d\uff0c\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u62e5\u6709\u76f8\u540c\u7684\u65b9\u6cd5\uff0c\u4f46\u5404\u81ea\u7684\u6570\u636e\u53ef\u80fd\u4e0d\u540c\u3002<\/p>\n<p>\u63a8\u8350\uff1aPython\u6559\u7a0b<\/p>\n<p>\u4ecd\u4ee5Student\u7c7b\u4e3a\u4f8b\uff0c\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u7c7b\u662f\u901a\u8fc7class\u5173\u952e\u5b57\uff1a<\/p>\n<pre>class&nbsp;Student(object):\n&nbsp;&nbsp;&nbsp;&nbsp;pass<\/pre>\n<p>class\u540e\u9762\u7d27\u63a5\u7740\u662f\u7c7b\u540d\uff0c\u5373Student\uff0c\u7c7b\u540d\u901a\u5e38\u662f\u5927\u5199\u5f00\u5934\u7684\u5355\u8bcd\uff0c\u7d27\u63a5\u7740\u662f(object)\uff0c\u8868\u793a\u8be5\u7c7b\u662f\u4ece\u54ea\u4e2a\u7c7b\u7ee7\u627f\u4e0b\u6765\u7684\uff0c\u7ee7\u627f\u7684\u6982\u5ff5\u6211<\/p>\n<p>\u4eec\u540e\u9762\u518d\u8bb2\uff0c\u901a\u5e38\uff0c\u5982\u679c\u6ca1\u6709\u5408\u9002\u7684\u7ee7\u627f\u7c7b\uff0c\u5c31\u4f7f\u7528object\u7c7b\uff0c\u8fd9\u662f\u6240\u6709\u7c7b\u6700\u7ec8\u90fd\u4f1a\u7ee7\u627f\u7684\u7c7b\u3002<\/p>\n<p>\u5b9a\u4e49\u597d\u4e86Student\u7c7b\uff0c\u5c31\u53ef\u4ee5\u6839\u636eStudent\u7c7b\u521b\u5efa\u51faStudent\u7684\u5b9e\u4f8b\uff0c\u521b\u5efa\u5b9e\u4f8b\u662f\u901a\u8fc7\u7c7b\u540d+()\u5b9e\u73b0\u7684\uff1a<\/p>\n<pre>&gt;&gt;&gt;&nbsp;bart&nbsp;=&nbsp;Student()\n&gt;&gt;&gt;&nbsp;bart\n&lt;__main__.Student&nbsp;object&nbsp;at&nbsp;0x10a67a590&gt;\n&gt;&gt;&gt;&nbsp;Student\n&lt;class&nbsp;'__main__.Student'&gt;<\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u53d8\u91cfbart\u6307\u5411\u7684\u5c31\u662f\u4e00\u4e2aStudent\u7684\u5b9e\u4f8b\uff0c\u540e\u9762\u76840x10a67a590\u662f\u5185\u5b58\u5730\u5740\uff0c\u6bcf\u4e2aobject\u7684\u5730\u5740\u90fd\u4e0d\u4e00\u6837\uff0c\u800cStudent\u672c\u8eab\u5219<\/p>\n<p>\u662f\u4e00\u4e2a\u7c7b\u3002<\/p>\n<p>\u53ef\u4ee5\u81ea\u7531\u5730\u7ed9\u4e00\u4e2a\u5b9e\u4f8b\u53d8\u91cf\u7ed1\u5b9a\u5c5e\u6027\uff0c\u6bd4\u5982\uff0c\u7ed9\u5b9e\u4f8bbart\u7ed1\u5b9a\u4e00\u4e2aname\u5c5e\u6027\uff1a<\/p>\n<pre>&gt;&gt;&gt;&nbsp;bart.name&nbsp;=&nbsp;'Bart&nbsp;Simpson'\n&gt;&gt;&gt;&nbsp;bart.name\n'Bart&nbsp;Simpson'<\/pre>\n<p>\u7531\u4e8e\u7c7b\u53ef\u4ee5\u8d77\u5230\u6a21\u677f\u7684\u4f5c\u7528\uff0c\u56e0\u6b64\uff0c\u53ef\u4ee5\u5728\u521b\u5efa\u5b9e\u4f8b\u7684\u65f6\u5019\uff0c\u628a\u4e00\u4e9b\u6211\u4eec\u8ba4\u4e3a\u5fc5\u987b\u7ed1\u5b9a\u7684\u5c5e\u6027\u5f3a\u5236\u586b\u5199\u8fdb\u53bb\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u7279\u6b8a\u7684__init__\u65b9\u6cd5\uff0c\u5728\u521b\u5efa\u5b9e\u4f8b\u7684\u65f6\u5019\uff0c\u5c31\u628aname\uff0cscore\u7b49\u5c5e\u6027\u7ed1\u4e0a\u53bb\uff1a<\/p>\n<pre>class&nbsp;Student(object):\n\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__init__(self,&nbsp;name,&nbsp;score):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name&nbsp;=&nbsp;name\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.score&nbsp;=&nbsp;score<\/pre>\n<p>\u6ce8\u610f\u5230__init__\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u6c38\u8fdc\u662fself\uff0c\u8868\u793a\u521b\u5efa\u7684\u5b9e\u4f8b\u672c\u8eab\uff0c\u56e0\u6b64\uff0c\u5728__init__\u65b9\u6cd5\u5185\u90e8\uff0c\u5c31\u53ef\u4ee5\u628a\u5404\u79cd\u5c5e\u6027\u7ed1\u5b9a\u5230self\uff0c\u56e0\u4e3aself<\/p>\n<p>\u5c31\u6307\u5411\u521b\u5efa\u7684\u5b9e\u4f8b\u672c\u8eab\u3002<\/p>\n<p>\u6709\u4e86__init__\u65b9\u6cd5\uff0c\u5728\u521b\u5efa\u5b9e\u4f8b\u7684\u65f6\u5019\uff0c\u5c31\u4e0d\u80fd\u4f20\u5165\u7a7a\u7684\u53c2\u6570\u4e86\uff0c\u5fc5\u987b\u4f20\u5165\u4e0e__init__\u65b9\u6cd5\u5339\u914d\u7684\u53c2\u6570\uff0c\u4f46self\u4e0d\u9700\u8981\u4f20\uff0cPython\u89e3\u91ca\u5668\u81ea\u5df1\u4f1a\u628a\u5b9e\u4f8b\u53d8\u91cf\u4f20\u8fdb\u53bb\uff1a<\/p>\n<pre>&gt;&gt;&gt;&nbsp;bart&nbsp;=&nbsp;Student('Bart&nbsp;Simpson',&nbsp;59)\n&gt;&gt;&gt;&nbsp;bart.name\n'Bart&nbsp;Simpson'\n&gt;&gt;&gt;&nbsp;bart.score\n59<\/pre>\n<p>\u66f4\u591a\u6280\u672f\u8bf7\u5173\u6ce8Python\u89c6\u9891\u6559\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9762\u5411\u5bf9\u8c61\u6700\u91cd\u8981\u7684\u6982\u5ff5\u5c31\u662f\u7c7b\uff08Class\uff09\u548c\u5b9e\u4f8b\uff08Instance\uff09\uff0c\u5fc5\u987b\u7262\u8bb0\u7c7b\u662f\u62bd\u8c61\u7684\u6a21\u677f\uff0c\u6bd4\u5982Student [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":181896,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-181895","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/181895","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=181895"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/181895\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/181896"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=181895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=181895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=181895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}