{"id":1573,"date":"2023-02-06T23:16:50","date_gmt":"2023-02-06T15:16:50","guid":{"rendered":"http:\/\/zhang.mba\/?p=1573"},"modified":"2023-02-06T23:17:38","modified_gmt":"2023-02-06T15:17:38","slug":"python-si-you-han-shu","status":"publish","type":"post","link":"https:\/\/zhang.mba\/index.php\/2023\/02\/06\/23\/16\/50\/1573\/python-si-you-han-shu\/python\/zhangzhiqi\/","title":{"rendered":"Python\u79c1\u6709\u51fd\u6570"},"content":{"rendered":"<p>\u5728 Python \u4e2d\uff0c\u79c1\u6709\u51fd\u6570\u662f\u4e00\u79cd\u4e0d\u80fd\u5728\u5916\u90e8\u4ee3\u7801\u4e2d\u8bbf\u95ee\u7684\u51fd\u6570\uff0c\u5b83\u4eec\u53ea\u80fd\u5728\u7c7b\u7684\u5185\u90e8\u4f7f\u7528\u3002\u79c1\u6709\u51fd\u6570\u7684\u547d\u540d\u4ee5\u4e24\u4e2a\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u4f8b\u5982\uff1a<code>__private_function<\/code>\u3002<\/p>\n<p>\u79c1\u6709\u51fd\u6570\u7684\u4e3b\u8981\u76ee\u7684\u662f\u4fdd\u62a4\u7c7b\u7684\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\uff0c\u53ea\u516c\u5f00\u5fc5\u8981\u7684\u63a5\u53e3\u3002\u5b83\u4eec\u4e0d\u80fd\u88ab\u76f4\u63a5\u8bbf\u95ee\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728\u7c7b\u7684\u5185\u90e8\u8fdb\u884c\u4efb\u610f\u7684\u66f4\u6539\uff0c\u800c\u4e0d\u5f71\u54cd\u5bf9\u8be5\u7c7b\u7684\u4f7f\u7528\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-Python\">class MyClass:\n    def __private_function(self):\n        print(&quot;This is a private function.&quot;)\n    \n    def public_function(self):\n        print(&quot;This is a public function.&quot;)\n        self.__private_function()\n\nmy_object = MyClass()\nmy_object.public_function()\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>MyClass<\/code> \u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u79c1\u6709\u51fd\u6570 <code>__private_function<\/code> \u548c\u4e00\u4e2a\u516c\u5171\u51fd\u6570 <code>public_function<\/code>\u3002\u5f53\u6211\u4eec\u8c03\u7528 <code>public_function<\/code> \u65f6\uff0c\u5b83\u5c06\u8c03\u7528\u79c1\u6709\u51fd\u6570\u5e76\u6253\u5370\u8f93\u51fa\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u8bd5\u56fe\u76f4\u63a5\u8bbf\u95ee\u79c1\u6709\u51fd\u6570\uff0c\u4f8b\u5982 <code>my_object.__private_function()<\/code>\uff0c\u5c06\u5f15\u53d1 <code>AttributeError<\/code> \u9519\u8bef\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u79c1\u6709\u51fd\u6570\u5e76\u4e0d\u662f\u771f\u6b63\u7684\u79c1\u6709\u7684\uff0c\u5b83\u4eec\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7\u7c7b\u7684\u5bf9\u8c61\u8bbf\u95ee\uff0c\u4f46\u662f\u5fc5\u987b\u6dfb\u52a0\u4e00\u4e2a\u4e0b\u5212\u7ebf\u548c\u7c7b\u540d\u524d\u7f00\u3002\u4f8b\u5982\uff0c\u5982\u679c\u79c1\u6709\u51fd\u6570\u7684\u540d\u79f0\u662f <code>__private_function<\/code>\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7 <code>_MyClass__private_function<\/code> \u8bbf\u95ee\u5b83\u3002<\/p>\n<p>\u5c3d\u7ba1\u79c1\u6709\u51fd\u6570\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\uff0c\u4f46\u662f\u5b83\u4eec\u4ecd\u7136\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u79c1\u6709\u51fd\u6570\u53ef\u4ee5\u63d0\u4f9b\u7c7b\u7684\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\uff0c\u5e76\u4e14\u53ea\u66b4\u9732\u5fc5\u8981\u7684\u63a5\u53e3\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u79c1\u6709\u51fd\u6570\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5c01\u88c5\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\uff0c\u79c1\u6709\u51fd\u6570\u662f\u4e00\u79cd\u4e0d\u80fd\u5728\u5916\u90e8\u4ee3\u7801\u4e2d\u8bbf\u95ee\u7684\u51fd\u6570\uff0c\u5b83\u4eec\u53ea\u80fd\u5728\u7c7b\u7684\u5185\u90e8\u4f7f\u7528\u3002\u79c1\u6709\u51fd\u6570\u7684\u547d\u540d\u4ee5\u4e24\u4e2a\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u4f8b\u5982\uff1a__private_function\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1573"}],"collection":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/comments?post=1573"}],"version-history":[{"count":0,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1573\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/media?parent=1573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/categories?post=1573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/tags?post=1573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}