{"id":174657,"date":"2023-07-29T22:58:26","date_gmt":"2023-07-29T14:58:26","guid":{"rendered":"http:\/\/www.idc.net\/help\/174657\/"},"modified":"2023-07-29T22:58:26","modified_gmt":"2023-07-29T14:58:26","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython%e6%96%b0%e5%bc%8f%e7%b1%bb%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/174657\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1apython\u65b0\u5f0f\u7c7b\u662f\u4ec0\u4e48"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/456\/308\/987\/1615878109940747.png\" class=\"aligncenter\"><\/p>\n<p><strong>1\u3001\u8bf4\u660e<\/strong><\/p>\n<p>python3.x\u7684\u6240\u6709\u7c7b\u90fd\u4f1a\u81ea\u52a8\u8f6c\u6362\u4e3a\u4e00\u4e2a\u65b0\u5f0f\u7c7b\uff0c\u4e0d\u8bba\u662f\u5426\u6709\u7ee7\u627fobject\u5bf9\u8c61\u3002<\/p>\n<p>python2.x\u5fc5\u987b\u663e\u5f0f\u5730\u6307\u5b9a\u7c7b\u7ee7\u627fobject\u7236\u7c7b\u624d\u8868\u793a\u65b0\u5f0f\u7c7b\u3002<\/p>\n<p><strong>2\u3001\u5b9e\u4f8b<\/strong><\/p>\n<pre>#&nbsp;newstyle.py,python\u73af\u5883\u4e3a2.xclass&nbsp;Classic:\n&nbsp;&nbsp;&nbsp;&nbsp;\"\"\"\n&nbsp;&nbsp;&nbsp;&nbsp;python2.x\u9ed8\u8ba4\u7c7b\u4e3a\u7ecf\u5178\u7c7b\n&nbsp;&nbsp;&nbsp;&nbsp;\u7531\u4e8e__getatt__&nbsp;\u4e0e&nbsp;__getattribute__\u529f\u80fd\u6548\u679c\u4e00\u6837\uff0c\u8fd9\u91cc\u53ea\u7528__getattr__\u6f14\u793a\n&nbsp;&nbsp;&nbsp;&nbsp;\"\"\"\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__getattr__(self,&nbsp;method_name):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(\"call&nbsp;Classic&nbsp;__getattr__,it&nbsp;would&nbsp;call&nbsp;built-in[%s]&nbsp;method&nbsp;\"&nbsp;%&nbsp;method_name)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;getattr(self.__name,method_name)class&nbsp;NewStyleClass(object):&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__init__(self):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.__name&nbsp;=&nbsp;\"newstyle&nbsp;name\"\n&nbsp;&nbsp;&nbsp;&nbsp;\"\"\"\n&nbsp;&nbsp;&nbsp;&nbsp;python2.x\u9700\u8981\u6307\u660e\u4e3a\u65b0\u5f0f\u7c7b\uff0cpython3.x\u9ed8\u8ba4\u4e3a\u65b0\u5f0f\u7c7b\n&nbsp;&nbsp;&nbsp;&nbsp;\"\"\"\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__getattr__(self,&nbsp;item):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(\"call&nbsp;NewStyle&nbsp;__getattr__,it&nbsp;would&nbsp;call&nbsp;built-in[%s]&nbsp;method&nbsp;\"&nbsp;%item)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;getattr(self.__name,item)def&nbsp;test_dir():\n&nbsp;&nbsp;&nbsp;&nbsp;C&nbsp;=&nbsp;Classic()\n&nbsp;&nbsp;&nbsp;&nbsp;N&nbsp;=&nbsp;NewStyleClass()\n&nbsp;&nbsp;&nbsp;&nbsp;print(dir(C)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u7ecf\u5178\u7c7b\u5185\u7f6e\u6709__getattr__\u65b9\u6cd5\n&nbsp;&nbsp;&nbsp;&nbsp;print(dir(N)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u65b0\u5f0f\u7c7b\u7684\u5185\u7f6e\u65b9\u6cd5\u7ee7\u627fobject\u5bf9\u8c61&gt;&gt;&gt;&nbsp;python&nbsp;newstyle.py<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662fpython\u65b0\u5f0f\u7c7b\u7684\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002<span>\u66f4\u591aPython\u5b66\u4e60\u6307\u8def\uff1a<\/span><span>\u540e\u6d6a\u4e91python\u6559\u7a0b<\/span><br \/><strong><\/strong><\/p>\n<p><span style=\"font-size: 14px\">\u672c\u6587\u6559\u7a0b\u64cd\u4f5c\u73af\u5883\uff1awindows7\u7cfb\u7edf\u3001Python 3.9.1\uff0cDELL G3\u7535\u8111\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u8bf4\u660e python3.x\u7684\u6240\u6709\u7c7b\u90fd\u4f1a\u81ea\u52a8\u8f6c\u6362\u4e3a\u4e00\u4e2a\u65b0\u5f0f\u7c7b\uff0c\u4e0d\u8bba\u662f\u5426\u6709\u7ee7\u627fobject\u5bf9\u8c61\u3002 python2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":174658,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-174657","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\/174657","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=174657"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/174657\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/174658"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=174657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=174657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=174657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}