{"id":150134,"date":"2024-05-06T10:55:37","date_gmt":"2024-05-06T02:55:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/150134.html"},"modified":"2024-05-06T10:55:42","modified_gmt":"2024-05-06T02:55:42","slug":"python%e4%b8%8b%e5%88%92%e7%ba%bf%e6%80%8e%e4%b9%88%e6%89%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/150134.html","title":{"rendered":"python\u4e0b\u5212\u7ebf\u600e\u4e48\u6253"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27143525\/da657f0f-090e-4934-b45c-53adbb9c54df.webp\" alt=\"python\u4e0b\u5212\u7ebf\u600e\u4e48\u6253\" \/><\/p>\n<p><p>Python \u4e2d\u7684\u4e0b\u5212\u7ebf <code>_<\/code> \u53ef\u4ee5\u76f4\u63a5\u5728\u82f1\u6587\u8f93\u5165\u72b6\u6001\u4e0b\u901a\u8fc7\u952e\u76d8\u6309\u952e\u83b7\u53d6\uff0c\u4e00\u822c\u4f4d\u4e8e\u952e\u76d8\u53f3\u4e0a\u89d2\u5b57\u6bcd <code>P<\/code> \u4e0b\u65b9\u3001\u56de\u8f66\u952e\u4e0a\u65b9\u7684\u90a3\u4e2a\u6309\u952e\uff0c<strong>\u901a\u5e38\u9700\u8981\u6309\u4f4f <code>Shift<\/code> \u952e\u540c\u65f6\u6309 <code>_<\/code> \u952e\u6765\u8f93\u5165<\/strong>\u3002\u4e0b\u5212\u7ebf\u5728 Python \u7f16\u7801\u4e2d\u7ecf\u5e38\u88ab\u7528\u4f5c\u53d8\u91cf\u540d\u7684\u4e00\u90e8\u5206\u6216\u8868\u793a\u7279\u5b9a\u7684\u547d\u540d\u7ea6\u5b9a\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u5212\u7ebf\u5728 Python \u4e2d\u6709\u51e0\u79cd\u4e0d\u540c\u7684\u7528\u9014\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e<strong>\u53d8\u91cf\u547d\u540d\u3001\u51fd\u6570\u547d\u540d\u3001\u7c7b\u6210\u5458\u7684\u8868\u793a\u3001\u5bf9\u907f\u514d\u547d\u540d\u51b2\u7a81\u7684\u5904\u7406\u3001\u8868\u793a\u4e0d\u5173\u6ce8\u7684\u53d8\u91cf<\/strong>\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u53d8\u91cf\u547d\u540d\u4e0e\u4e0b\u5212\u7ebf<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u4e0b\u5212\u7ebf\u7ecf\u5e38\u7528\u4e8e\u53d8\u91cf\u547d\u540d\u3002\u5b83\u53ef\u4ee5\u7528\u5728\u53d8\u91cf\u540d\u7684\u5f00\u59cb\u5904\u3001\u4e2d\u95f4\u3001\u6216\u672b\u5c3e\u3002\u6839\u636e\u5176\u4f4d\u7f6e\u548c\u4f7f\u7528\u7684\u4e60\u60ef\uff0c\u4e0b\u5212\u7ebf\u5728 Python \u4e2d\u626e\u6f14\u4e0d\u540c\u7684\u89d2\u8272\u3002<\/p>\n<\/p>\n<p><h4>\u5355\u4e2a\u4e0b\u5212\u7ebf\u7684\u5f00\u5934<\/h4>\n<\/p>\n<p><p>\u901a\u5e38\u8868\u793a\u4e00\u4e2a\u53d8\u91cf\u662f\u4e34\u65f6\u7684\u6216\u4e0d\u91cd\u8981\u7684\u3002\u4f8b\u5982\uff0c\u5728\u5faa\u73af\u4e2d\u4ec5\u4ec5\u60f3\u8981\u7d22\u5f15\u800c\u4e0d\u5173\u5fc3\u5177\u4f53\u503c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>_<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for _ in range(5):<\/p>\n<p>    # \u6267\u884c\u64cd\u4f5c\uff0c\u4f46\u4e0d\u5173\u5fc3\u5faa\u73af\u53d8\u91cf\u7684\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc <code>_<\/code> \u4ec5\u4ec5\u7528\u4f5c\u4e00\u4e2a\u904d\u5386\u6b21\u6570\u7684\u6307\u793a\uff0c\u6ca1\u6709\u8fdb\u4e00\u6b65\u7684\u7528\u9014\u3002<\/p>\n<\/p>\n<p><h4>\u5355\u4e2a\u4e0b\u5212\u7ebf\u7684\u7ed3\u5c3e<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\uff0cPython \u4e2d\u4e00\u4e2a\u53d8\u91cf\u7684\u540d\u5b57\u4e0e Python \u5173\u952e\u5b57\u51b2\u7a81\uff0c\u6bd4\u5982 <code>class<\/code>\uff0c\u6211\u4eec\u4e0d\u80fd\u7528 <code>class<\/code> \u4f5c\u4e3a\u53d8\u91cf\u540d\u56e0\u4e3a\u5b83\u662f Python \u7684\u4fdd\u7559\u5b57\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5355\u4e2a\u4e0b\u5212\u7ebf\u7ed3\u5c3e\u6765\u907f\u514d\u8fd9\u6837\u7684\u51b2\u7a81\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class_ = MyClass()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u7528\u6cd5\u5141\u8bb8\u7a0b\u5e8f\u5458\u4f7f\u7528\u5177\u6709\u7279\u5b9a\u610f\u4e49\u7684\u5355\u8bcd\u4f5c\u4e3a\u53d8\u91cf\u540d\u3002<\/p>\n<\/p>\n<p><h4>\u53cc\u4e0b\u5212\u7ebf\u7684\u5f00\u5934\uff08\u4e0d\u5305\u62ec\u672b\u5c3e\uff09<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u7528\u6cd5\u51fa\u73b0\u5728\u7c7b\u7684\u6210\u5458\u547d\u540d\u4e2d\uff0c\u610f\u5728\u8868\u793a\u8fd9\u662f\u4e00\u4e2a\u79c1\u6709\u6210\u5458\u3002\u5728 Python \u4e2d\uff0c\u7ea6\u5b9a\u4ee5\u53cc\u4e0b\u5212\u7ebf\u5f00\u5934\u7684\u6210\u5458\u4e3a\u79c1\u6709\uff0c\u5916\u90e8\u4e0d\u53ef\u76f4\u63a5\u8bbf\u95ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.__private_member = 42<\/p>\n<h2><strong>\u5916\u90e8\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee __private_member<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Python \u5b9e\u9645\u4e0a\u4f1a\u6539\u540d\u79c1\u6709\u6210\u5458\uff0c\u4ee5\u7c7b\u540d\u5f00\u5934\uff0c\u9632\u6b62\u5b50\u7c7b\u7684\u547d\u540d\u51b2\u7a81\u3002\u8fd9\u53eb\u505a\u540d\u79f0\u6539\u5199\uff08name mangling\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u51fd\u6570\u547d\u540d\u4e0e\u4e0b\u5212\u7ebf<\/h3>\n<\/p>\n<p><p>\u540c\u53d8\u91cf\u547d\u540d\u7c7b\u4f3c\uff0c\u4e0b\u5212\u7ebf\u4e5f\u7528\u4e8e\u51fd\u6570\u547d\u540d\uff0c\u4ee5\u4fbf\u533a\u5206\u79c1\u6709\u51fd\u6570\u3001\u5185\u90e8\u51fd\u6570\u548c\u7279\u6b8a\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u79c1\u6709\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u79c1\u6709\u51fd\u6570\u5728\u5b9a\u4e49\u65f6\u4f7f\u7528\u53cc\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u8868\u793a\u8fd9\u4e2a\u51fd\u6570\u4ec5\u5e94\u5728\u7c7b\u5185\u90e8\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __private_function(self):<\/p>\n<p>        # \u79c1\u6709\u51fd\u6570\u7684\u5b9e\u73b0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u9b54\u672f\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u4ee5\u53cc\u4e0b\u5212\u7ebf\u5f00\u5934\u548c\u7ed3\u5c3e\u7684\u51fd\u6570\u662f Python \u7684\u7279\u6b8a\u65b9\u6cd5\uff0c\u5e38\u89c1\u7684\u6709 <code>__init__<\/code>\u3001<code>__str__<\/code>\u3001<code>__repr__<\/code> \u7b49\u3002\u8fd9\u4e9b\u662f\u9884\u8bbe\u7684\u65b9\u6cd5\uff0c\u5b83\u4eec\u6709\u7279\u6b8a\u7684\u8c03\u7528\u65b9\u5f0f\u548c\u7528\u9014\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7c7b\u6210\u5458\u7684\u8868\u793a<\/h3>\n<\/p>\n<p><p>\u5728\u7c7b\u5b9a\u4e49\u4e2d\uff0c\u4e0d\u540c\u5f62\u5f0f\u7684\u4e0b\u5212\u7ebf\u4ee3\u8868\u4e0d\u540c\u7684\u6210\u5458\u7c7b\u578b\u3002\u79c1\u6709\u6210\u5458\u3001\u53d7\u4fdd\u62a4\u7684\u6210\u5458\u4ee5\u53ca\u516c\u5171\u6210\u5458\u7684\u547d\u540d\u4e60\u60ef\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><h4>\u79c1\u6709\u6210\u5458<\/h4>\n<\/p>\n<p><p>\u524d\u9762\u5df2\u7ecf\u63d0\u5230\uff0c\u79c1\u6709\u6210\u5458\u4ee5\u53cc\u4e0b\u5212\u7ebf\u5f00\u5934\u3002\u8fd9\u8868\u660e\u8be5\u6210\u5458\u4e0d\u60f3\u88ab\u5916\u90e8\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h4>\u53d7\u4fdd\u62a4\u7684\u6210\u5458<\/h4>\n<\/p>\n<p><p>\u901a\u5e38\uff0c\u53d7\u4fdd\u62a4\u7684\u6210\u5458\u4ee5\u5355\u4e0b\u5212\u7ebf\u5f00\u5934\u3002\u8fd9\u662f\u4e00\u79cd\u7ea6\u5b9a\uff0c\u7528\u4e8e\u63d0\u793a\u5176\u4ed6\u7a0b\u5e8f\u5458\u8fd9\u4e2a\u6210\u5458\u5e94\u8be5\u5c0f\u5fc3\u8bbf\u95ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def _protected_member(self):<\/p>\n<p>        # \u53d7\u4fdd\u62a4\u7684\u6210\u5458\u5b9e\u73b0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u516c\u5171\u6210\u5458<\/h4>\n<\/p>\n<p><p>\u65e0\u7279\u6b8a\u4e0b\u5212\u7ebf\u524d\u7f00\u7684\u6210\u5458\u88ab\u8ba4\u4e3a\u662f\u516c\u5171\u7684\uff0c\u53ef\u4ee5\u81ea\u7531\u8bbf\u95ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def public_member(self):<\/p>\n<p>        # \u516c\u5171\u6210\u5458\u5b9e\u73b0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u907f\u514d\u547d\u540d\u51b2\u7a81\u4e0e\u547d\u540d\u7ea6\u5b9a<\/h3>\n<\/p>\n<p><p>\u5728 Python \u7f16\u7a0b\u4e2d\uff0c\u4e0b\u5212\u7ebf\u9664\u4e86\u5728\u53d8\u91cf\u3001\u51fd\u6570\u3001\u7c7b\u6210\u5458\u7684\u547d\u540d\u4e2d\u53d1\u6325\u4f5c\u7528\u4e4b\u5916\uff0c\u8fd8\u5e38\u7528\u4e8e\u7ef4\u6301\u547d\u540d\u7684\u6e05\u6670\u548c\u907f\u514d\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h4><strong>all<\/strong> \u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728\u6a21\u5757\u4e2d\uff0c<code>__all__<\/code> \u662f\u4e00\u4e2a\u4ee5\u5b57\u7b26\u4e32\u5217\u8868\u5f62\u5f0f\u5b58\u5728\uff0c\u7528\u4e8e\u5b9a\u4e49\u6a21\u5757\u5bfc\u51fa\u54ea\u4e9b\u7b26\u53f7\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">__all__ = [&#039;a_public_function&#039;, &#039;_a_protected_function&#039;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u63a7\u5236\u54ea\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\u5728\u4f7f\u7528 <code>from module import *<\/code> \u65f6\u88ab\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><h4>\u907f\u514d\u4e0e\u5185\u7f6e\u51fd\u6570\u7684\u51b2\u7a81<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4e0b\u5212\u7ebf\u662f\u907f\u514d\u4e0e Python \u5185\u7f6e\u51fd\u6570\u51b2\u7a81\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c<code>min<\/code>\u3001<code>max<\/code> \u662f\u5185\u7f6e\u51fd\u6570\uff0c\u5982\u679c\u4f60\u5b9a\u4e49\u4e86\u540c\u540d\u7684\u53d8\u91cf\uff0c\u53ef\u80fd\u4f1a\u8986\u76d6\u6389\u8fd9\u4e9b\u51fd\u6570\uff0c\u4ece\u800c\u5f15\u53d1\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u4e0b\u5212\u7ebf\u5728 Python \u4e2d\u4e0d\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b57\u7b26\uff0c\u800c\u662f\u627f\u8f7d\u4e86\u591a\u91cd\u7f16\u7801\u4e60\u60ef\u4e0e\u542b\u4e49\u3002\u5b83\u5728\u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\u3001\u7ef4\u62a4\u6027\u4e2d\u8d77\u5230\u5173\u952e\u4f5c\u7528\uff0c\u4e3a\u53d8\u91cf\u3001\u51fd\u6570\u3001\u7c7b\u6210\u5458\u7b49\u63d0\u4f9b\u4e86\u66f4\u591a\u5c42\u6b21\u7684\u4fe1\u606f\u3002\u7a0b\u5e8f\u5458\u5e94\u7406\u89e3\u548c\u9075\u5b88\u8fd9\u4e9b\u7ea6\u5b9a\uff0c\u4ee5\u7f16\u5199\u9ad8\u8d28\u91cf\u3001\u7b26\u5408\u6807\u51c8\u7684 Python \u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u4e3a\u4ec0\u4e48\u5728Python\u4e2d\u8981\u4f7f\u7528\u4e0b\u5212\u7ebf\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u4e0b\u5212\u7ebf\u6709\u591a\u79cd\u7528\u9014\u3002\u9996\u5148\uff0c\u5b83\u53ef\u4ee5\u7528\u4f5c\u53d8\u91cf\u540d\u6216\u51fd\u6570\u540d\u7684\u4e00\u90e8\u5206\uff0c\u4f8b\u5982<code>my_variable<\/code>\u6216<code>my_function<\/code>\u3002\u6b64\u5916\uff0c\u5728\u547d\u540d\u7ea6\u5b9a\u4e2d\uff0c\u4f7f\u7528\u5355\u4e0b\u5212\u7ebf\u8868\u793a\u201c\u79c1\u6709\u201d\u6216\u201c\u5185\u90e8\u201d\u5c5e\u6027\uff0c\u800c\u4f7f\u7528\u53cc\u4e0b\u5212\u7ebf\u8868\u793a\u201c\u540d\u79f0\u4fee\u9970\u201d\u3002\u5728Python\u4e2d\uff0c\u8fd9\u4e9b\u7ea6\u5b9a\u5bf9\u4e8e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ec4\u7ec7\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5370\u4e0b\u5212\u7ebf\uff1f<\/strong><\/p>\n<p>\u8981\u5728Python\u4e2d\u6253\u5370\u4e0b\u5212\u7ebf\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528print\u51fd\u6570\u548c\u5b57\u7b26\u4e32\u8fde\u63a5\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u8981\u6253\u537010\u4e2a\u4e0b\u5212\u7ebf\uff0c\u60a8\u53ef\u4ee5\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code>print(&quot;_&quot; * 10)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u4e00\u884c\u5305\u542b10\u4e2a\u4e0b\u5212\u7ebf\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u5e26\u6709\u4e0b\u5212\u7ebf\u7684\u591a\u884c\u6ce8\u91ca\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e09\u4e2a\u5f15\u53f7\uff08\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\uff09\u6765\u521b\u5efa\u591a\u884c\u6ce8\u91ca\u3002\u4f8b\u5982\uff0c\u8981\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u4e0b\u5212\u7ebf\u7684\u591a\u884c\u6ce8\u91ca\uff0c\u60a8\u53ef\u4ee5\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code>&quot;&quot;&quot;\n\u8fd9\u662f\u4e00\u4e2a\u591a\u884c\u6ce8\u91ca\u793a\u4f8b\uff1a\n\n\u8fd9\u662f\u4e00\u6761\u6ce8\u91ca\u4e0b\u9762\u6709\u4e00\u4e2a\u4e0b\u5212\u7ebf\uff1a______\n\n\u4e0b\u5212\u7ebf\u53ef\u4ee5\u7528\u6765\u5f3a\u8c03\u91cd\u8981\u4fe1\u606f\u3002\n\n&quot;&quot;&quot;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python \u4e2d\u7684\u4e0b\u5212\u7ebf _ \u53ef\u4ee5\u76f4\u63a5\u5728\u82f1\u6587\u8f93\u5165\u72b6\u6001\u4e0b\u901a\u8fc7\u952e\u76d8\u6309\u952e\u83b7\u53d6\uff0c\u4e00\u822c\u4f4d\u4e8e\u952e\u76d8\u53f3\u4e0a\u89d2\u5b57\u6bcd P \u4e0b\u65b9\u3001\u56de\u8f66 [&hellip;]","protected":false},"author":3,"featured_media":150143,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/150134"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=150134"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/150134\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/150143"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=150134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=150134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=150134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}