{"id":6134,"date":"2022-08-23T21:34:58","date_gmt":"2022-08-23T13:34:58","guid":{"rendered":"http:\/\/139.9.1.231\/?p=6134"},"modified":"2022-08-24T10:14:10","modified_gmt":"2022-08-24T02:14:10","slug":"python-all","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/08\/23\/python-all\/","title":{"rendered":"python \u5305\u3001\u6a21\u5757\u7684\u4e66\u5199 \u4ee5\u53ca __all__ \u53d8\u91cf\u7684\u7528\u6cd5"},"content":{"rendered":"\n<h1>\u4e00\u3001\u6a21\u5757<\/h1>\n\n\n\n<p>\u76f8\u4fe1\u4f7f\u7528\u8fc7Python\u7f16\u5199\u4ee3\u7801\u7684\u540c\u5b66\uff0c\u4f1a\u7ecf\u5e38\u5728\u6587\u4ef6\u5934\u770b\u5230\u8fd9\u6837\u7684import \u2026\uff0c\u662f\u7684\uff0c\u8fd9\u5c31\u662f\u5bfc\u5165\u6a21\u5757\u7684\u8bed\u53e5\uff0c\u800c\u6bcf\u4e00\u4e2a\u540e\u7f00\u540d\u4e3a.py\u7684\u6587\u4ef6\u90fd\u662f\u4e00\u4e2a\u6a21\u5757\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import jieba\nimport os\u3000\n<\/code><\/pre>\n\n\n\n<h2><a><\/a><a><\/a>1. \u4ec0\u4e48\u662f\u6a21\u5757\uff1f<\/h2>\n\n\n\n<p>\u2003\u2003\u903b\u8f91\u4e0a\u6765\u8bf4\u6a21\u5757\u662f\u4e00\u7ec4\u529f\u80fd\u7684\u7ec4\u5408\uff1b\u5b9e\u8d28\u4e0a\u4e00\u4e2a\u6a21\u5757\u5c31\u662f\u4e00\u4e2a\u5305\u542b\u4e86python\u5b9a\u4e49\u548c\u58f0\u660e\u7684\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u5c31\u662f\u6a21\u5757\u540d\u5b57\u52a0\u4e0a.py\u7684\u540e\u7f00\u3002<\/p>\n\n\n\n<p>import\u52a0\u8f7d\u7684\u6a21\u5757\u5206\u4e3a\u56db\u4e2a\u901a\u7528\u7c7b\u522b\uff1a<br><br>a. \u4f7f\u7528python\u7f16\u5199\u7684\u4ee3\u7801\uff08.py\u6587\u4ef6\uff09\uff1b<br>b. \u5df2\u88ab\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93\u6216DLL\u7684C\u6216C++\u6269\u5c55\uff1b<br>c. \u5305\u597d\u4e00\u7ec4\u6a21\u5757\u7684\u5305<br>d. \u4f7f\u7528C\u7f16\u5199\u5e76\u94fe\u63a5\u5230python\u89e3\u91ca\u5668\u7684\u5185\u7f6e\u6a21\u5757\uff1b<\/p>\n\n\n\n<p><strong>\u5982\u4f55\u4f7f\u7528\u6a21\u5757\uff1f<\/strong><br>\u3000\u3000\u60f3\u8981\u4f7f\u7528\u6a21\u5757\uff0c\u5fc5\u987b\u5148\u8981\u5c06\u6a21\u5757\u52a0\u8f7d\u8fdb\u6765\uff0c\u53ef\u4ee5\u901a\u8fc7\u5173\u952e\u5b57&nbsp;import&nbsp;\u6216&nbsp;from\u8fdb\u884c\u52a0\u8f7d\uff1b\u9700\u8981\u6ce8\u610f\u7684\u662f\u6a21\u5757\u548c\u5f53\u524d\u6587\u4ef6\u5728\u4e0d\u540c\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u3002<\/p>\n\n\n\n<h2><a><\/a><a><\/a>2. \u6a21\u5757\u7684\u6784\u6210<\/h2>\n\n\n\n<p>\u2003\u2003\u6a21\u5757\u53ef\u4ee5\u5305\u542b\u53ef\u6267\u884c\u7684\u8bed\u53e5\u548c\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u8fd9\u4e9b\u8bed\u53e5\u7684\u76ee\u7684\u662f\u521d\u59cb\u5316\u6a21\u5757\uff0c\u5b83\u4eec\u53ea\u5728\u6a21\u5757\u540d\u7b2c\u4e00\u6b21\u9047\u5230\u5bfc\u5165import\u8bed\u53e5\u65f6\u624d\u6267\u884c\uff08<strong>import\u8bed\u53e5\u662f\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u7684\u4efb\u610f\u4f4d\u7f6e\u4f7f\u7528\u7684,\u4e14\u9488\u5bf9\u540c\u4e00\u4e2a\u6a21\u5757\u5f88import\u591a\u6b21,\u4e3a\u4e86\u9632\u6b62\u4f60\u91cd\u590d\u5bfc\u5165\uff0cpython\u7684\u4f18\u5316\u624b\u6bb5\u662f\uff1a\u7b2c\u4e00\u6b21\u5bfc\u5165\u540e\u5c31\u5c06\u6a21\u5757\u540d\u52a0\u8f7d\u5230\u5185\u5b58\u4e86\uff0c\u540e\u7eed\u7684import\u8bed\u53e5\u4ec5\u662f\u5bf9\u5df2\u7ecf\u52a0\u8f7d\u5927\u5185\u5b58\u4e2d\u7684\u6a21\u5757\u5bf9\u8c61\u589e\u52a0\u4e86\u4e00\u6b21\u5f15\u7528\uff0c\u4e0d\u4f1a\u91cd\u65b0\u6267\u884c\u6a21\u5757\u5185\u7684\u8bed\u53e5<\/strong>\uff09<\/p>\n\n\n\n<h1>\u4e8c\u3001\u6a21\u5757\u7684\u5bfc\u5165<\/h1>\n\n\n\n<p><strong>1\u3001\u5bfc\u5165\u6574\u4e2a\u6a21\u5757<\/strong><\/p>\n\n\n\n<p>\u2003\u2003\u6bd4\u5982\u6211\u4eec\u6709\u4e00\u4e2amyModule\u7684\u6587\u4ef6\u5939\uff0c\u91cc\u9762\u6709\u4e00\u4e2afirst.py\u6587\u4ef6\uff0c\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 1\ndef myfun(s):\n    print(s + 1)\n<\/code><\/pre>\n\n\n\n<p>\u2003\u2003\u5728myModule\u7684\u6587\u4ef6\u5939\u4e0b\u6253\u5f00\u7ec8\u7aef\/cmd\uff0c\u8f93\u5165python\u8fdb\u5165\u547d\u4ee4\u884c\u4ea4\u4e92\u6a21\u5f0f<br>\u5199\u5b8c\u6a21\u5757\u5bfc\u5165\u7684\u8bed\u53e5\u4e4b\u540e\uff0c\u63a5\u7740\u5c31\u53ef\u4ee5\u8c03\u7528\u8be5\u6a21\u5757\u4e0b\u7684\u51fd\u6570\u4e86\u3002\u8c03\u7528\u65b9\u5f0f\u4e3a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; import first\n&gt;&gt;&gt; a\nTraceback (most recent call last):\n  File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;\nNameError: name 'a' is not defined\n&gt;&gt;&gt; first.a\n1\n&gt;&gt;&gt; first.myfun(2)\n3\n<\/code><\/pre>\n\n\n\n<p><img src=\"https:\/\/img-blog.csdnimg.cn\/20200404123418136.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><br><strong>2\u3001\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\/\u53d8\u91cf<\/strong><\/p>\n\n\n\n<p>\u2003\u2003\u6240\u4ee5\u8bf4first.py\u6587\u4ef6\u5c31\u662f\u4e00\u4e2a\u6a21\u5757\uff0c\u53ef\u4ee5\u7528import\u5bfc\u5165\uff0c\u91cc\u9762\u53d8\u91cf\u548c\u65b9\u6cd5\u90fd\u8981\u7528first.\u524d\u7f00\u6765\u5f15\u7528\uff0c\u5982\u679c\u60f3\u4e0d\u4f7f\u7528\u8fd9\u4e2a\u524d\u7f00\u6216\u662f\u6211\u4eec\u53ea\u662f\u60f3\u8981\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u51fd\u6570\uff0c\u5c31\u53ef\u4ee5\u53ea\u5bfc\u5165\u8be5\u53d8\u91cf\u6216\u51fd\u6570\u3002\u5bfc\u5165\u65b9\u5f0f\u4e3a\uff1afrom module_name import function_name\u3002<br>\u2003\u2003\u5982\u679c\u5bfc\u5165\u7684\u662f\u53d8\u91cf\uff0c\u5c31\u53ef\u4ee5\u76f4\u63a5\u8f93\u5165\u53d8\u91cf\u540d\u6765\u83b7\u5f97\u53d8\u91cf\u7684\u503c\uff1b\u5982\u679c\u76f4\u63a5\u5bfc\u5165\u7684\u662f\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528function_name() \u7684\u65b9\u5f0f\u8c03\u7528\u51fd\u6570\uff0c\u65e0\u9700\u5728\u51fd\u6570\u540d\u524d\u9762\u52a0\u4e0a\u6a21\u5757\u540d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em># \u5bfc\u5165\u53d8\u91cf<\/em>\n&gt;&gt;&gt; from first import a\n&gt;&gt;&gt; a\n1\n<em># \u5bfc\u5165\u51fd\u6570<\/em>\n&gt;&gt;&gt; from first import myfun\n&gt;&gt;&gt; myfun(3)\n4\n<em># \u4e00\u6b21\u5bfc\u5165\u591a\u4e2a\u53d8\u91cf<\/em>\n&gt;&gt;&gt; from first import a,myfun\n&gt;&gt;&gt; a\n1\n&gt;&gt;&gt; myfun(5)\n6\n<em># \u5bfc\u5165\u6a21\u5757\u4e2d\u5168\u90e8\u53d8\u91cf<\/em>\n&gt;&gt;&gt; from first import *\n&gt;&gt;&gt; a\n1\n&gt;&gt;&gt; myfun(5)\n6\n&gt;&gt;&gt;\n<\/code><\/pre>\n\n\n\n<p><strong>3\u3001\u4f7f\u7528as\u7ed9\u6a21\u5757\u6307\u5b9a\u522b\u540d<\/strong><\/p>\n\n\n\n<p>\u2003\u2003\u53ef\u4ee5\u5728\u540e\u9762\u4f7f\u7528as\u7ed9\u51fd\u6570\u6307\u5b9a\u522b\u540d\u3002\u53e5\u5f0f\u5982\uff1aimport module_name as new_name\uff0c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; import first as f\n&gt;&gt;&gt; f.a\n1\n&gt;&gt;&gt; f.myfun(6)\n7\n<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u5bfc\u5165\u51fd\u6570\u7684\u57fa\u7840\u4e0a\uff0c\u53ef\u4ee5\u5728\u540e\u9762\u7528as\u8bed\u53e5\u7ed9\u5bfc\u5165\u7684\u51fd\u6570\u6307\u5b9a\u522b\u540d\u3002\u53e5\u5f0f\u5982\uff1afrom module_name import function_name as new_function\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; from first import myfun as add\n&gt;&gt;&gt; add(8)\n9<\/code><\/pre>\n\n\n\n<h1>\u4e09\u3001\u5305\u3001\u5e93<\/h1>\n\n\n\n<p><strong>\u6a21\u5757(module)<\/strong>&nbsp;\u5176\u5b9e\u5c31\u662fpy\u6587\u4ef6\uff0c\u91cc\u9762\u5b9a\u4e49\u4e86\u4e00\u4e9b\u51fd\u6570\u3001\u7c7b\u3001\u53d8\u91cf\u7b49\u3002<br><strong>\u5305(package)<\/strong>&nbsp;\u662f\u591a\u4e2a\u6a21\u5757\u7684\u805a\u5408\u4f53\u5f62\u6210\u7684\u6587\u4ef6\u5939\uff0c\u91cc\u9762\u53ef\u4ee5\u662f\u591a\u4e2apy\u6587\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u5d4c\u5957\u6587\u4ef6\u5939\u3002<br><strong>\u5e93<\/strong>\u662f\u53c2\u8003\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u7684\u8bf4\u6cd5\uff0c\u662f\u6307\u5b8c\u6210\u4e00\u5b9a\u529f\u80fd\u7684\u4ee3\u7801\u96c6\u5408\uff0c\u5728python\u4e2d\u7684\u5f62\u5f0f\u5c31\u662f\u6a21\u5757\u548c\u5305\u3002<\/p>\n\n\n\n<p>\u4e00\u4e2a\u5305\u7684\u67b6\u6784\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sound\/                          Top-level package\n      __init__.py               Initialize the sound package\n      formats\/                  Subpackage for file format conversions\n              __init__.py\n              wavread.py\n              wavwrite.py\n              aiffread.py\n              aiffwrite.py\n              auread.py\n              auwrite.py\n              ...\n      effects\/                  Subpackage for sound effects\n              __init__.py\n              echo.py\n              surround.py\n              reverse.py\n              ...\n      filters\/                  Subpackage for filters\n              __init__.py\n              equalizer.py\n              vocoder.py\n              karaoke.py\n              ...<\/pre>\n\n\n\n<p>Python<strong> \u53ea\u628a\u542b&nbsp;<code>__init__.py<\/code>&nbsp;\u6587\u4ef6\u7684\u76ee\u5f55\u5f53\u6210\u5305\u3002\u8fd9\u6837\u53ef\u4ee5\u9632\u6b62\u4ee5&nbsp;<code>string<\/code>&nbsp;\u7b49\u901a\u7528\u540d\u79f0\u547d\u540d\u7684\u76ee\u5f55\uff0c\u65e0\u610f\u4e2d\u5c4f\u853d\u51fa\u73b0\u5728\u540e\u65b9\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\u7684\u6709\u6548\u6a21\u5757\u3002 \u6700\u7b80\u60c5\u51b5\u4e0b\uff0c<code>__init__.py<\/code>&nbsp;\u53ea\u662f\u4e00\u4e2a\u7a7a\u6587\u4ef6\uff0c\u4f46\u8be5\u6587\u4ef6\u4e5f\u53ef\u4ee5\u6267\u884c\u5305\u7684\u521d\u59cb\u5316\u4ee3\u7801\uff0c\u6216\u8bbe\u7f6e&nbsp;<code>__all__<\/code>&nbsp;\u53d8\u91cf\uff0c\u8be6\u89c1\u4e0b\u6587\u3002<\/strong><\/p>\n\n\n\n<h1>\u56db\u3001\u5305\u7684\u5bfc\u5165<\/h1>\n\n\n\n<p>\u5bfc\u5165\u5305\u7684\u672c\u8d28\uff1a<strong>\u5bfc\u5165\u4e00\u4e2a\u5305\u5c31\u662f\u6267\u884c\u5305\u4e0b\u7684__init__.py\u6587\u4ef6<\/strong><\/p>\n\n\n\n<p><strong>\u53ea\u8981\u4e00\u4e2a\u6587\u4ef6\u5939\u4e0b\u9762\u6709\u4e2a__init__.py \u6587\u4ef6\uff0c\u90a3\u4e48\u8fd9\u4e2a\u6587\u4ef6\u5939\u5c31\u53ef\u4ee5\u770b\u505a\u662f\u4e00\u4e2a\u5305<\/strong>\u3002<\/p>\n\n\n\n<p>\u5305\u5bfc\u5165\u7684\u8fc7\u7a0b\u548c\u6a21\u5757\u7684\u57fa\u672c\u4e00\u81f4\uff0c\u53ea\u662f\u5bfc\u5165\u5305\u7684\u65f6\u5019\u4f1a\u6267\u884c\u6b64\u5305\u76ee\u5f55\u4e0b\u7684 init.py \u800c\u4e0d\u662f\u6a21\u5757\u91cc\u9762\u7684\u8bed\u53e5\u4e86\u3002\u53e6\u5916\uff0c\u5982\u679c\u53ea\u662f\u5355\u7eaf\u7684\u5bfc\u5165\u5305\uff0c\u800c\u5305\u7684 init.py \u4e2d\u53c8\u6ca1\u6709\u660e\u786e\u7684\u5176\u4ed6\u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u90a3\u4e48\u6b64\u5305\u4e0b\u9762\u7684\u6a21\u5757\u662f\u4e0d\u4f1a\u81ea\u52a8\u5bfc\u5165\u7684\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\u9700\u8981\u6ce8\u610f\u4e24\u70b9<\/p>\n\n\n\n<ol><li>__ init__ .py\u6587\u4ef6\u7f16\u5199\u65f6\uff0c\u5982\u679c\u8981\u5728__init__.py\u4e2d\u5bfc\u5165\u5176\u4ed6\u6a21\u5757\u4e2d\u7684\u53d8\u91cf\uff0c\u5373\u4f7f__ init__.py\u6587\u4ef6\u548cabcd.py\u6587\u4ef6\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u5939\u4e0b\uff0c\u4e5f\u4e0d\u80fdfrom abcd import b\uff0c\u8981\u4eceabcd\u6587\u4ef6\u4ece\u54ea\u91cc\u6765\u5f00\u59cb\u5199\uff0c<strong>\u5373\u4ece\u5305\u7684\u540d\u79f0\u5f00\u59cb<\/strong>\uff0cfrom folder.abcd import b\u3002<\/li><li>folder\u6587\u4ef6\u5939\u91cc\u7684\u5d4c\u5957\u6587\u4ef6\u5939\u5185\u4e0d\u9700\u8981\u65b0\u5efa__init__.py\u6587\u4ef6\u5373\u53ef\u50cf\u6a21\u5757\u4e00\u6837\u8c03\u7528\uff0c\u4f46\u662f\u4e00\u822c\u8fd8\u662f\u8981\u65b0\u5efa\u8fd9\u4e2a\u6587\u4ef6\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bfc\u5165\u5e38\u7528\u53d8\u91cf\u3002<\/li><li>i<strong>nit<\/strong>.py\u6587\u4ef6\u5176\u5b9e\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u6587\u4ef6\uff0c\u5b83\u76f8\u5f53\u4e8e\u540d\u4e3afolder\u6a21\u5757\uff0c\u5373\u5982\u679c\u4f7f\u7528import folder\u5219\u53ef\u4ee5\u8c03\u7528\u5728__init__.py\u6587\u4ef6\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u3002<\/li><\/ol>\n\n\n\n<h2>\u4e94\u3001<strong>__ all __<\/strong><\/h2>\n\n\n\n<p id=\"index-8\">\u4f7f\u7528&nbsp;<code>from&nbsp;sound.effects&nbsp;import&nbsp;*<\/code>&nbsp;\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u8be5\u8bed\u53e5\u5728\u6587\u4ef6\u7cfb\u7edf\u67e5\u627e\u5e76\u5bfc\u5165\u5305\u7684\u6240\u6709\u5b50\u6a21\u5757\u3002\u8fd9\u9879\u64cd\u4f5c\u82b1\u8d39\u7684\u65f6\u95f4\u8f83\u957f\uff0c\u5e76\u4e14\u5bfc\u5165\u5b50\u6a21\u5757\u53ef\u80fd\u4f1a\u4ea7\u751f\u4e0d\u5fc5\u8981\u7684\u526f\u4f5c\u7528\uff0c\u8fd9\u79cd\u526f\u4f5c\u7528\u53ea\u6709\u5728\u663e\u5f0f\u5bfc\u5165\u5b50\u6a21\u5757\u65f6\u624d\u4f1a\u53d1\u751f\u3002<\/p>\n\n\n\n<p>\u552f\u4e00\u7684\u89e3\u51b3\u65b9\u6848\u662f\u63d0\u4f9b\u5305\u7684\u663e\u5f0f\u7d22\u5f15\u3002<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/reference\/simple_stmts.html#import\"><code>import<\/code><\/a>&nbsp;\u8bed\u53e5\u4f7f\u7528\u5982\u4e0b\u60ef\u4f8b\uff1a\u5982\u679c\u5305\u7684&nbsp;<code>__init__.py<\/code>&nbsp;\u4ee3\u7801\u5b9a\u4e49\u4e86\u5217\u8868&nbsp;<code>__all__<\/code>\uff0c\u8fd0\u884c&nbsp;<code>from&nbsp;package&nbsp;import&nbsp;*<\/code>&nbsp;\u65f6\uff0c\u5b83\u5c31\u662f\u7528\u4e8e\u5bfc\u5165\u7684\u6a21\u5757\u540d\u5217\u8868\u3002\u53d1\u5e03\u5305\u7684\u65b0\u7248\u672c\u65f6\uff0c\u5305\u7684\u4f5c\u8005\u5e94\u66f4\u65b0\u6b64\u5217\u8868\u3002\u5982\u679c\u5305\u7684\u4f5c\u8005\u8ba4\u4e3a\u6ca1\u6709\u5fc5\u8981\u5728\u5305\u4e2d\u6267\u884c\u5bfc\u5165 * \u64cd\u4f5c\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u63d0\u4f9b\u6b64\u5217\u8868\u3002\u4f8b\u5982\uff0c<code>sound\/effects\/__init__.py<\/code>&nbsp;\u6587\u4ef6\u5305\u542b\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">__all__ = [\"echo\", \"surround\", \"reverse\"]\n<\/pre>\n\n\n\n<p>\u8fd9\u5c06\u610f\u5473\u7740\u5c06&nbsp;<code>from&nbsp;sound.effects&nbsp;import&nbsp;*<\/code>&nbsp;\u5bfc\u5165&nbsp;<code>sound.effects<\/code>&nbsp;\u5305\u7684\u4e09\u4e2a\u547d\u540d\u7684\u5b50\u6a21\u5757\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6ca1\u6709\u5b9a\u4e49&nbsp;<code>__all__<\/code>\uff0c<code>from&nbsp;sound.effects&nbsp;import&nbsp;*<\/code>&nbsp;\u8bed\u53e5&nbsp;<em>\u4e0d\u4f1a<\/em>&nbsp;\u628a\u5305&nbsp;<code>sound.effects<\/code>&nbsp;\u4e2d\u6240\u6709\u5b50\u6a21\u5757\u90fd\u5bfc\u5165\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\uff1b\u8be5\u8bed\u53e5\u53ea\u786e\u4fdd\u5bfc\u5165\u5305&nbsp;<code>sound.effects<\/code>&nbsp;\uff08\u53ef\u80fd\u8fd8\u4f1a\u8fd0\u884c&nbsp;<code>__init__.py<\/code>&nbsp;\u4e2d\u7684\u521d\u59cb\u5316\u4ee3\u7801\uff09\uff0c\u7136\u540e\uff0c\u518d\u5bfc\u5165\u5305\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\u3002\u8fd9\u4e9b\u540d\u79f0\u5305\u62ec&nbsp;<code>__init__.py<\/code>&nbsp;\u4e2d\u5b9a\u4e49\u7684\u4efb\u4f55\u540d\u79f0\uff08\u4ee5\u53ca\u663e\u5f0f\u52a0\u8f7d\u7684\u5b50\u6a21\u5757\uff09\uff0c\u8fd8\u5305\u62ec\u4e4b\u524d&nbsp;<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/reference\/simple_stmts.html#import\"><code>import<\/code><\/a>&nbsp;\u8bed\u53e5\u663e\u5f0f\u52a0\u8f7d\u7684\u5305\u91cc\u7684\u5b50\u6a21\u5757\u3002<\/p>\n\n\n\n<h3 class=\"has-light-pink-background-color has-background\">\u53d8\u91cf__all__\u7684\u597d\u5904\uff1a\u53ea\u4f1a\u5bfc\u51faall\u4e2d\u7684\u5b50\u6a21\u5757\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u907f\u514d\u547d\u540d\u7a7a\u95f4\u7684\u6c61\u67d3\uff0c\u5e76\u52a0\u901f\u6a21\u5757\u7684\u5bfc\u5165<\/h3>\n\n\n\n<p>\u4e00\u3001<strong>\u6a21\u5757\u516c\u5f00\u63a5\u53e3\u7684\u4e00\u79cd\u7ea6\u5b9a<\/strong><br>__all__\u53ef\u4ee5\u5728\u6a21\u5757\u7ea7\u522b\u66b4\u9732\u63a5\u53e3\uff0c\u5f62\u5f0f\u5982\u4e0b\uff1a<br>__all__ = [&#8220;foo&#8221;, &#8220;bar&#8221;]<br>Python \u6ca1\u6709\u539f\u751f\u7684\u53ef\u89c1\u6027\u63a7\u5236\uff0c\u5176\u53ef\u89c1\u6027\u7684\u7ef4\u62a4\u662f\u9760\u4e00\u5957\u9700\u8981\u5927\u5bb6\u81ea\u89c9\u9075\u5b88\u7684\u201d\u7ea6\u5b9a\u201c\uff0c\u6bd4\u5982\uff0c\u4e0b\u5212\u7ebf\u5f00\u5934\u7684\u53d8\u91cf\u5bf9\u5916\u90e8\u4e0d\u53ef\u89c1\u3002<br>__all__ \u662f\u9488\u5bf9\u6a21\u5757\u516c\u5f00\u63a5\u53e3\u7684\u4e00\u79cd\u7ea6\u5b9a\uff0c\u4ee5\u63d0\u4f9b\u4e86\u201d\u767d\u540d\u5355\u201c\u7684\u5f62\u5f0f\u66b4\u9732\u63a5\u53e3\u3002\u5982\u679c\u5b9a\u4e49\u4e86__all__\uff0c\u5176\u4ed6\u6587\u4ef6\u4e2d\u4f7f\u7528from xxx import *\u5bfc\u5165\u8be5\u6587\u4ef6\u65f6\uff0c\u53ea\u4f1a\u5bfc\u5165 __all__ \u5217\u51fa\u7684\u6210\u5458\uff0c\u53ef\u4ee5\u5176\u4ed6\u6210\u5458\u90fd\u88ab\u6392\u9664\u5728\u5916\u3002<br>\u5982\uff0ctest1.py,test2.py,test3.py\u4e09\u4e2a\u6587\u4ef6\uff1a<br>test1.py<br>#__all__ = [&#8216;func&#8217;]<br>def func():<br>pass<br><br>test2.py<br>import test1<br><br>__all__ = [&#8216;func2&#8217;, &#8216;test1&#8217;]<br>def func2():<br>pass<br><br>def func22():<br>pass<br><br>test3.py<br>from test2 import *<br><br>func2() #\u80fd\u6b63\u5e38\u5f15\u7528<br>test1.func() #\u80fd\u6b63\u5e38\u5f15\u7528<br>func22() #\u4e0d\u80fd\u6b63\u5e38\u5f15\u7528<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">\u4e8c\u3001<strong>\u63a7\u5236 from xxx import * \u7684\u884c\u4e3a<\/strong><br>python\u4e0d\u63d0\u5021\u7528 from xxx import * \u8fd9\u79cd\u5199\u6cd5\u3002\u5982\u679c\u4e00\u4e2a\u6a21\u5757 xxx \u6ca1\u6709\u5b9a\u4e49 __all__\uff0c\u6267\u884c from spam import * \u65f6\u4f1a\u5c06 xxx \u4e2d\u6240\u6709\u975e\u4e0b\u5212\u7ebf\u5f00\u5934\u7684\u6210\u5458\uff08\u5305\u62ec\u8be5\u6a21\u5757import\u7684\u5176\u4ed6\u6a21\u5757\u6210\u5458\uff09\u90fd\u4f1a\u5bfc\u5165\u5f53\u524d\u547d\u540d\u7a7a\u95f4\uff0c\u8fd9\u6837\u5c31\u53ef\u80fd\u5f04\u810f\u5f53\u524d\u7684\u547d\u540d\u7a7a\u95f4\u3002\u663e\u5f0f\u58f0\u660e\u4e86 __all__\uff0cimport * \u5c31\u53ea\u4f1a\u5bfc\u5165 __all__ \u5217\u51fa\u7684\u6210\u5458\uff0c\u5982\u679c __all__ \u5b9a\u4e49\u6709\u8bef\uff0c\u8fd8\u4f1a\u660e\u786e\u5730\u629b\u51fa\u5f02\u5e38\uff0c\u65b9\u4fbf\u68c0\u67e5\u9519\u8bef\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">\u4e09\u3001<strong>\u4e3a lint \u7b49\u4ee3\u7801\u68c0\u67e5\u5de5\u5177\u63d0\u4f9b\u8f85\u52a9<\/strong><br>\u7f16\u5199\u5e93\u65f6\uff0c\u7ecf\u5e38\u4f1a\u5728 __init__.py \u4e2d\u66b4\u9732\u6574\u4e2a\u5305\u7684 API\uff0c\u800c\u8fd9\u4e9b API \u7684\u5b9e\u73b0\u53ef\u80fd\u662f\u5728\u5305\u7684\u5176\u4ed6\u6a21\u5757\u4e2d\u3002\u5982\u679c\u4ec5\u4ec5\u8fd9\u6837\u5199\uff1afrom xxx import a, b\uff0c\u4e00\u4e9b\u4ee3\u7801\u68c0\u67e5\u5de5\u5177\uff0c\u5982 pyflakes \u4f1a\u62a5\u9519\uff0c\u8ba4\u4e3a\u53d8\u91cf a\u548c b import \u4e86\u4f46\u6ca1\u88ab\u4f7f\u7528\u3002\u4e00\u4e2a\u53ef\u884c\u7684\u65b9\u6cd5\u662f\u628a\u8fd9\u4e2a\u8b66\u544a\u538b\u6389\uff1afrom xxx import a, b # noqa \uff08No Q\/A\uff0c\u5373\u65e0\u8d28\u91cf\u4fdd\u8bc1\uff09\uff0c\u4f46\u66f4\u597d\u7684\u65b9\u6cd5\u662f\u663e\u5f0f\u5b9a\u4e49 __all__\uff0c\u8fd9\u6837\u4ee3\u7801\u68c0\u67e5\u5de5\u5177\u5c31\u4f1a\u7406\u89e3\uff0c\u4ece\u800c\u4e0d\u518d\u62a5 unused variables \u7684\u8b66\u544a\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">\u56db\u3001<strong>\u5b9a\u4e49 all \u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9<\/strong><\/p>\n\n\n\n<ul><li>__all__ \u7684\u5f62\u5f0f\u90fd\u662f list\u7c7b\u578b\u3002\u5982\u679c\u5199\u6210\u5176\u4ed6\u7c7b\u578b\uff0c pyflakes \u7b49 lint \u5de5\u5177\u53ef\u80fd\u65e0\u6cd5\u8bc6\u522b\u3002<\/li><li>\u4e0d\u80fd\u52a8\u6001\u751f\u6210 __all__\uff0c\u5982\u4f7f\u7528\u5217\u8868\u89e3\u6790\u5f0f\u3002__all__ \u7684\u4f5c\u7528\u662f\u5b9a\u4e49\u516c\u5f00\u63a5\u53e3\uff0c\u9700\u8981\u4ee5\u5b57\u9762\u91cf\u7684\u5f62\u5f0f\u663e\u5f0f\u5199\u51fa\u6765\u3002<\/li><li>\u5373\u4f7f\u5b9a\u4e49\u4e86 __all__\uff0c \u4e5f\u4e0d\u5e94\u8be5\u5728\u975e\u4e34\u65f6\u4ee3\u7801\u4e2d\u4f7f\u7528 from xxx import * \u8bed\u6cd5\uff0c\u6216\u7528\u7f16\u7a0b\u5de5\u5177\u6a21\u62df Ruby \u7684\u81ea\u52a8 import\u3002Python \u4e0d\u50cf Ruby\uff0c\u6ca1\u6709 Module \u8fd9\u7c7b\u6210\u5458\uff0c\u6a21\u5757\u5c31\u662f\u547d\u540d\u7a7a\u95f4\u9694\u79bb\u7684\u6267\u884c\u8005\u3002\u5982\u679c\u6253\u7834\u4e86\u8fd9\u4e00\u5c42\uff0c\u5f15\u5165\u8bf8\u591a\u52a8\u6001\u56e0\u7d20\uff0c\u751f\u4ea7\u73af\u5883\u4e2d\u8dd1\u7684\u4ee3\u7801\u5c31\u53ef\u80fd\u5145\u6ee1\u4e0d\u786e\u5b9a\u6027\uff0c\u8c03\u8bd5\u4e5f\u4f1a\u53d8\u5f97\u56f0\u96be\u3002<\/li><li>\u6309\u7167 PEP8 \u5efa\u8bae\u7684\u98ce\u683c\uff0c__all__ \u5e94\u8be5\u5199\u5728\u6240\u6709 import \u8bed\u53e5\u4e0b\u9762\uff0c\u51fd\u6570\u3001\u5e38\u91cf\u7b49\u6210\u5458\u5b9a\u4e49\u7684\u4e0a\u9762\u3002<\/li><li>\u5982\u679c\u4e00\u4e2a\u6a21\u5757\u9700\u8981\u66b4\u9732\u7684\u63a5\u53e3\u6539\u52a8\u9891\u7e41\uff0c__all__ \u53ef\u4ee5\u8fd9\u6837\u5b9a\u4e49\uff1a<\/li><\/ul>\n\n\n\n<p>__all__ = [<br>&#8220;foo&#8221;,<br>&#8220;bar&#8221;,<br>&#8220;egg&#8221;,<br>]<br>\u8fd9\u6837\u4fee\u6539\u4e00\u4e2a\u66b4\u9732\u7684\u63a5\u53e3\u53ea\u4fee\u6539\u4e00\u884c\uff0c\u65b9\u4fbf\u7248\u672c\u63a7\u5236\u7684\u65f6\u5019\u770b diff\u3002\u6700\u540e\u591a\u51fa\u7684\u9017\u53f7\u5728 Python \u4e2d\u662f\u5141\u8bb8\u7684\uff0c\u7b26\u5408 PEP8 \u98ce\u683c\u3002<\/p>\n\n\n\n<p><strong>\u7531\u4e0a\u9762\u7684\u8f93\u51fa\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053import *\u53ea\u4f1a\u5bfc\u5165__all__\u4e2d\u6307\u5b9a\u7684\u53d8\u91cf\uff0c\u65e0\u8bba\u662f\u5426\u4ee5\u4e0b\u5212\u7ebf\u5f00\u5934\u3002\u8fd9\u6837\u9650\u5236\u53ef\u4ee5\u9632\u6b62import *\u547d\u4ee4\u5bfc\u5165\u592a\u591a\u53d8\u91cf\u6c61\u67d3\u547d\u540d\u7a7a\u95f4\uff0c\u8fc7\u6ee4\u6389\u4e00\u4e9b\u4e2d\u95f4\u53d8\u91cf\u5982b<\/strong><\/p>\n\n\n\n<h1>\u4e94\u3001\u6a21\u5757\u5bfc\u5165\u7684\u7edd\u5bf9\u5f15\u7528\u4e0e\u76f8\u5bf9\u5f15\u7528<\/h1>\n\n\n\n<p><strong>python\u4e2d\u7684import\u5206\u4e3a\u7edd\u5bf9\u5f15\u7528\u548c\u76f8\u5bf9\u5f15\u7528\u4e24\u79cd<\/strong>\u3002\u5b83\u4eec\u4e4b\u95f4\u7684\u5dee\u5f02\u5728\u4e8e\uff0c\u5f15\u7528\u6a21\u5757\u65f6\uff0c\u5b9a\u4f4d\u88ab\u5f15\u7528\u6a21\u5757\u4f4d\u7f6e \u7684\u65b9\u5f0f\u4e0d\u540c\u3002<\/p>\n\n\n\n<p>\u7edd\u5bf9\u5f15\u7528\u662f\u901a\u8fc7.\u7684\u8fde\u63a5\uff0c\u6307\u5b9a\u51fa\u6700\u9ad8\u7ea7\u6587\u4ef6\uff08\u5939\uff09\uff0c\u5230\u76ee\u6807\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u6211\u4eec\u4e0a\u9762\u7684\u6240\u6709\u7528\u6cd5\u90fd\u5c5e\u4e8e\u7edd\u5bf9\u5f15\u7528\u3002<\/p>\n\n\n\n<p>\u800c\u76f8\u5bf9\u5f15\u7528\u662f\u6307\u5b9a\u5f85\u5f15\u7528\u6a21\u5757\u4e0e\u5f53\u524d\u6587\u4ef6\u7684\u76f8\u5bf9\u4f4d\u7f6e\uff0c.\u8868\u793a\u4e0a\u4e00\u7ea7\u6587\u4ef6<\/p>\n\n\n\n<ul><li>\u7edd\u5bf9\u5f15\u7528\uff1afrom folder.abcd import myclass<\/li><li>\u76f8\u5bf9\u5f15\u7528\uff1afrom .abcd import myclass<\/li><\/ul>\n\n\n\n<p>\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u65e0\u8bba\u662f\u7edd\u5bf9\u5bfc\u5165\u8fd8\u662f\u76f8\u5bf9\u5bfc\u5165\u90fd\u8981\u6ce8\u610f\uff0c\u5982\u4f55\u5bfc\u5165\u4e0e\u88ab\u8c03\u7528\u4f4d\u7f6e\u6709\u5173\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6a21\u5757 \u76f8\u4fe1\u4f7f\u7528\u8fc7Python\u7f16\u5199\u4ee3\u7801\u7684\u540c\u5b66\uff0c\u4f1a\u7ecf\u5e38\u5728\u6587\u4ef6\u5934\u770b\u5230\u8fd9\u6837\u7684import \u2026\uff0c\u662f\u7684\uff0c\u8fd9\u5c31\u662f\u5bfc\u5165\u6a21\u5757 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/08\/23\/python-all\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">python \u5305\u3001\u6a21\u5757\u7684\u4e66\u5199 \u4ee5\u53ca __all__ \u53d8\u91cf\u7684\u7528\u6cd5<\/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,11],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/6134"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=6134"}],"version-history":[{"count":30,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/6134\/revisions"}],"predecessor-version":[{"id":6203,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/6134\/revisions\/6203"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=6134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=6134"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=6134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}