

{"id":91,"date":"2021-06-20T17:17:48","date_gmt":"2021-06-20T09:17:48","guid":{"rendered":"https:\/\/www.52dixiaowo.com\/python\/?p=91"},"modified":"2021-06-20T18:55:20","modified_gmt":"2021-06-20T10:55:20","slug":"python%e7%9a%84%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.52dixiaowo.com\/python\/post-91.html","title":{"rendered":"python\u7684\u7c7b\u4e0e\u5bf9\u8c61"},"content":{"rendered":"\n<p>python\u652f\u6301\u9762\u5411\u5bf9\u8c61, \u5176\u4e2d<strong>\u7c7b<\/strong>\u7684\u6982\u5ff5\u548c\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u76f8\u540c<\/p>\n\n\n\n<p>\u7c7b, \u5728python\u4e2d, \u548c\u6a21\u5757\u5dee\u4e0d\u591a, \u53ef\u4ee5\u7406\u89e3\u4e3a\u5de5\u5177\u5305, \u88ab\u5bfc\u5165\u4f7f\u7528, \u4f46\u53c8\u4e0d\u662f\u5b8c\u5168\u76f8\u540c<\/p>\n\n\n\n<h2>\u7c7b\u4e0e\u9762\u5411\u5bf9\u8c61<\/h2>\n\n\n\n<p>\u7c7b\u7684\u5b9a\u4e49, \u6765\u6e90\u4e8e\u751f\u6d3b, \u6bd4\u5982\u4eba\u7c7b, \u9e1f\u7c7b, \u662f\u4e00\u79cd\u62bd\u8c61\u7684\u5b9a\u4e49( \u5e76\u4e0d\u662f\u4e00\u4e2a\u771f\u5b9e\u7684\u4e2a\u4f53), \u4f46\u53c8\u786e\u5b9e\u6709\u7279\u5b9a\u7684\u884c\u4e3a\u548c\u7279\u5f81. \u6bd4\u5982\u4eba\u7c7b\u4e0d\u662f\u6307\u4f60\u6216\u8005\u6211, \u4f46\u4e00\u8bf4\u5230\u4eba\u7c7b, \u5c31\u4f1a\u60f3\u5230, \u4e00\u5bf9\u624b\u4e00\u5bf9\u811a, \u76f4\u7acb\u884c\u8d70, \u5403\u996d\u8bf4\u8bdd\u7b49<\/p>\n\n\n\n<p>\u5728\u7c7b\u4e2d, \u67092\u4e2a\u91cd\u8981\u7684\u4e1c\u897f, \u5206\u522b\u662f:<\/p>\n\n\n\n<ul><li>\u7279\u5f81 &#8211;&gt; \u4e3b\u8981\u662f\u6307\u4e00\u4e9b\u9759\u6001\u7684\u4e1c\u897f, \u6bd4\u5982\u5934, \u8033\u6735, \u7b49\u7b49, \u8fd9\u91cc\u7684\u9759\u6001\u6307\u7684\u662f\u4e3b\u89c2\u9759\u6001, \u4ec5\u4ec5\u8868\u793a\u5b58\u5728\u8fd9\u6837\u4e00\u4e2a\u4e1c\u897f, \u5728\u7c7b\u4e2d\u8868\u793a\u4e3a\u5c5e\u6027<\/li><li>\u884c\u4e3a &#8211;&gt;  \u4e3b\u8981\u662f\u4e3b\u89c2\u52a8\u6001\u7684\u4e1c\u897f, \u6bd4\u5982\u4eba\u53ef\u4ee5\u8bf4\u8bdd, \u8fd9\u662f\u4e00\u4e2a\u80fd\u529b, \u5728\u7c7b\u4e2d\u8868\u793a\u4e3a\u65b9\u6cd5<\/li><\/ul>\n\n\n\n<p>\u5176\u5b9e, \u8fd9\u8ddf\u6a21\u5757\u975e\u5e38\u76f8\u4f3c, \u53c2\u8003\u4ee5\u524d\u5b66\u7684random\u6a21\u5757, <\/p>\n\n\n\n<ul><li>\u9762\u5411\u8fc7\u7a0b &#8211;&gt; \u751f\u6210\u968f\u673a\u6570, \u624b\u52a8\u5199\u4ee3\u7801\u5b9e\u73b0<\/li><li>\u9762\u5411\u5bf9\u8c61 &#8211;&gt; \u751f\u6210\u968f\u673a\u6570, \u627e\u5de5\u5177\u5305\u5b9e\u73b0<\/li><\/ul>\n\n\n\n<p>\u9002\u5f53\u7684\u628a\u4ee3\u7801\u5c01\u88c5\u6210\u7c7b, \u53ef\u4ee5\u628a\u957f\u4ee3\u7801\u6070\u5f53\u7684\u5212\u5206, \u903b\u8f91\u66f4\u6e05\u6670<\/p>\n\n\n\n<p><strong>ps:<\/strong> \u5728python\u4e2d, \u57fa\u7840\u7684\u6570\u636e\u7c7b\u578b\u4e5f\u662f\u7c7b, \u6bd4\u5982\u6574\u578b\u7c7b, \u5217\u8868\u90fd\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u79cd&#8221;\u7c7b&#8221;, \u5728ipython\u4e2d, \u53ef\u4ee5\u4f7f\u7528dir(\u53d8\u91cf)\u6765\u67e5\u770b\u6240\u6709\u7684\u5c5e\u6027\u548c\u65b9\u6cd5, \u5185\u7f6e\u65b9\u6cd5\u540d(__\u65b9\u6cd5\u540d__)<\/p>\n\n\n\n<p>\u4e00\u4e9b\u5e38\u89c1\u7684\u5185\u7f6e\u65b9\u6cd5<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td>__new__<\/td><td>\u521b\u5efa\u5bf9\u8c61\u65f6, \u88ab\u81ea\u52a8\u8c03\u7528<\/td><\/tr><tr><td>__init__<\/td><td>\u5bf9\u8c61\u521d\u59cb\u5316\u65f6, \u88ab\u81ea\u52a8\u8c03\u7528<\/td><\/tr><tr><td>__del__<\/td><td>\u5bf9\u8c61\u9500\u6bc1\u524d, \u88ab\u81ea\u52a8\u8c03\u7528<\/td><\/tr><tr><td>__str__<\/td><td>\u8fd4\u56de\u5bf9\u8c61\u7684\u63cf\u8ff0\u4fe1\u606f, print\u51fd\u6570\u9ed8\u8ba4\u8c03\u7528<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>__str__\u76f8\u5f53\u4e8e\u5176\u4ed6\u8bed\u8a00\u7684toString()\u65b9\u6cd5, \u9ed8\u8ba4\u6253\u537016\u8fdb\u5236\u5730\u5740<\/p>\n\n\n\n<h2>\u7c7b\u7684\u8bbe\u8ba1<\/h2>\n\n\n\n<p>\u547d\u540d\u662f\u4e00\u4e2a\u91cd\u8981\u54f2\u5b66, \u5728python\u4e2d, \u7c7b\u5e94\u8be5\u6ee1\u8db3<strong>\u5927\u9a7c\u5cf0<\/strong>\u547d\u540d\u7b56\u7565<\/p>\n\n\n\n<p>\u6240\u8c13\u5927\u9a7c\u5cf0\u547d\u540d, \u662f\u6307\u6bcf\u4e2a\u5355\u8bcd\u7684\u9996\u5b57\u6bcd\u90fd\u662f\u5927\u5199\u7684, \u6bd4\u5982 KeyWord, DataUtils, \u5355\u8bcd\u4e2d\u95f4\u6ca1\u6709\u4e0b\u5212\u7ebf\u5206\u9694<\/p>\n\n\n\n<p>\u6b64\u5916, \u8981\u8003\u8651\u5c5e\u6027\u548c\u65b9\u6cd5, \u4e5f\u5c31\u662f\u7c7b\u4e2d\u6709\u4ec0\u4e48\u7279\u5f81\u548c\u65b9\u6cd5<\/p>\n\n\n\n<p><strong>\u7c7b\u57fa\u672c\u5b9a\u4e49\u8bed\u6cd5<\/strong><\/p>\n\n\n\n<p>\u4ec5\u5305\u542b\u65b9\u6cd5\u7684\u7c7b\u5b9a\u4e49\u5982\u4e0b, \u5148\u8bb0\u4f4f\u65b9\u6cd5\u7b2c\u4e00\u4e2a\u53c2\u6570\u5fc5\u987b\u662fself<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class \u7c7b\u540d:\n    def \u65b9\u6cd51(self,\u53c2\u6570\u5217\u8868):\n        pass\n    def \u65b9\u6cd52(self,\u53c2\u6570\u5217\u8868):\n        pass<\/code><\/pre>\n\n\n\n<p>\u548c\u6a21\u5757\u4e0d\u540c\u7684\u662f, \u7c7b\u4e0d\u80fd\u76f4\u63a5\u88ab\u8c03\u7528, \u800c\u662f\u5fc5\u987b<strong>\u5bf9\u8c61<\/strong>\u8c03\u7528, \u8fd9\u91cc\u6682\u4e0d\u7ec6\u8bf4, \u7b80\u5355\u683c\u5f0f\u4e3a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5bf9\u8c61\u53d8\u91cf = \u7c7b\u540d()<\/code><\/pre>\n\n\n\n<p><strong>\u7c7b\u5b9a\u4e49\u5b9e\u4f8b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person:\n    def eat(self, something):\n        print(\"\u5403:\"+something)\n    def sleep(self, hours):\n        print(\"\u6bcf\u5929\u7761:\"+str(hours)+\"\u5c0f\u65f6\")\n\n#\u8c03\u7528\u7c7b, \u521b\u5efa\u5bf9\u8c61\nxiaoming = Person()\nxiaoming.eat(\"\u6c34\u679c\")\nxiaoming.sleep(8)\n\n#\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u521b\u5efa\u65e0\u6570\u5bf9\u8c61, \u53ef\u4ee5\u770b\u6210\u662f\u4e00\u4e2a\u6a21\u677f\nxiaohong = Person()\nxiaohong.eat(\"\u725b\u5976\")\nxiaohong.sleep(10)<\/code><\/pre>\n\n\n\n<h2>\u7c7b\u7684\u5c5e\u6027<\/h2>\n\n\n\n<p>\u5c5e\u6027, \u662f\u7c7b\u7684\u91cd\u8981\u7ec4\u6210\u4e4b\u4e00, python\u7684\u7c7b\u5c5e\u6027, \u548c\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u6709\u4e00\u4e9b\u4e0d\u592a\u4e00\u6837<\/p>\n\n\n\n<p>\u5728python\u4e2d, \u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <strong>.\u5c5e\u6027\u540d<\/strong> \u7684\u65b9\u5f0f, \u8c03\u7528, \u4fee\u6539, \u6216\u589e\u52a0<\/p>\n\n\n\n<p>\u4f8b\u5982, \u4ee5\u4e0b\u5b9e\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>xiaoming.name=\"\u5c0f\u660e\"\nprint(xiaoming.name)\nxiaoming.name=\"\u5927\u660e\"\nprint(xiaoming.name)<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f, \u8fd9\u79cd\u65b9\u5f0f\u662f\u4e0d\u63a8\u8350\u7684, \u8fd9\u6837\u4ec5\u4ec5\u4f5c\u7528\u4e8e\u5bf9\u8c61\u5b9e\u4f8b\u4e0a, \u800c\u6ca1\u6709\u5728\u7c7b\u4e0a<\/p>\n\n\n\n<p>\u5b9a\u4e49\u5728\u7c7b\u4e0a, \u8bed\u6cd5\u5927\u6982\u662f\u8fd9\u6837<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class \u7c7b\u540d:\n    \n    \u53d8\u91cf1 = \u503c1\n    \u53d8\u91cf2 = \u503c2\n\n    def \u65b9\u6cd51(self):\n        pass\n\n    def \u65b9\u6cd52(self):\n        pass<\/code><\/pre>\n\n\n\n<p>\u5b9a\u4e49\u5728\u7c7b\u4e2d, \u4e0e\u7c7b\u65b9\u6cd5\u5e76\u6392\u7684\u53eb\u7c7b\u5c5e\u6027, \u4f46\u8fd9\u6837\u7684\u6210\u5458\u5c5e\u6027\u5f88\u4e0d\u7075\u6d3b, \u4e0b\u4e00\u8282\u5c06\u548c\u7c7b\u6784\u9020\u65b9\u6cd5\u4e00\u8d77\u8bb2\u89e3<\/p>\n\n\n\n<p><strong>ps:<\/strong> \u7c7b\u7684\u5c5e\u6027, \u5f80\u5f80\u4e5f\u53eb<strong>\u6210\u5458\u53d8\u91cf<\/strong>, \u7c7b\u7684\u65b9\u6cd5, \u4e5f\u53eb<strong>\u6210\u5458\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<h2>python\u7684\u5bf9\u8c61<\/h2>\n\n\n\n<p>\u5bf9\u8c61, \u662f\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b, \u4e5f\u5373\u662f\u4e00\u4e2a\u771f\u5b9e\u7684\u4e2a\u4f53<\/p>\n\n\n\n<p>\u4f8b\u5982, \u4eba\u7c7b\u662f\u6a21\u7cca\u7684\u6982\u5ff5, \u90a3\u4e48&#8221;\u5f20\u4e09&#8221;\u5c31\u662f\u4e00\u4e2a\u771f\u5b9e\u4e2a\u4f53, \u524d\u8005\u662f\u7c7b, \u540e\u8005\u662f\u5bf9\u8c61<\/p>\n\n\n\n<p>\u5bf9\u8c61\u7531\u7c7b\u521b\u5efa\u800c\u6765, \u4e0a\u9762\u7684\u4f8b\u5b50\u5c31\u6709\u7528\u5230, \u6bd4\u5982<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person:\n    def eat(self, something):\n        print(\"\u5403:\"+something)\n    def sleep(self, hours):\n        print(\"\u6bcf\u5929\u7761:\"+str(hours)+\"\u5c0f\u65f6\")\n\n#\u8c03\u7528\u7c7b, \u521b\u5efa\u5bf9\u8c61\nzhangsan= Person()   #zhangsan\u662f\u4e00\u4e2a\u5bf9\u8c61\nlisi = Person()      #lisi\u4e5f\u662f\u4e00\u4e2a\u5bf9\u8c61<\/code><\/pre>\n\n\n\n<p>\u5bf9\u8c61\u5fc5\u987b\u7531\u7c7b\u521b\u5efa\u800c\u6765, \u5bf9\u8c61\u662f\u771f\u5b9e\u5360\u6709\u5185\u5b58\u7684, \u800c\u7c7b\u662f\u521b\u5efa\u5bf9\u8c61\u7684\u6a21\u677f, \u5b83\u4eec\u662f\u4e92\u76f8\u4f9d\u5b58\u7684, \u7b80\u5355\u7406\u89e3\u4e3a: <strong>\u6ca1\u6709\u7c7b\u5c31\u65e0\u6cd5\u521b\u5efa\u5bf9\u8c61, \u6ca1\u6709\u5bf9\u8c61\u5c31\u65e0\u6cd5\u771f\u6b63\u6267\u884c<\/strong><\/p>\n\n\n\n<p>\u5176\u5b9e\u7c7b\u548c\u5bf9\u8c61\u90fd\u53ea\u662f\u4e00\u4e2a\u79f0\u547c\u800c\u5df2, \u6bcf\u4e00\u6837\u771f\u5b9e\u5b58\u5728\u7684\u90fd\u53ef\u4ee5\u662f\u5bf9\u8c61, \u6bcf\u4e00\u79cd\u62bd\u8c61\u7684\u4e1c\u897f\u90fd\u53ef\u4ee5\u7406\u89e3\u4e3a\u7c7b\u3002 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>python\u652f\u6301\u9762\u5411\u5bf9\u8c61, \u5176\u4e2d\u7c7b\u7684\u6982\u5ff5\u548c\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u76f8\u540c \u7c7b, \u5728python\u4e2d, \u548c\u6a21\u5757\u5dee\u4e0d\u591a, \u53ef\u4ee5\u7406\u89e3&hellip; <a href=\"https:\/\/www.52dixiaowo.com\/python\/post-91.html\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">python\u7684\u7c7b\u4e0e\u5bf9\u8c61<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts\/91"}],"collection":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":0,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}