{"id":6196,"date":"2024-08-16T08:01:01","date_gmt":"2024-08-16T00:01:01","guid":{"rendered":""},"modified":"2024-08-16T08:01:01","modified_gmt":"2024-08-16T00:01:01","slug":"python\u7f16\u8f91\u5668_python\u548cc++\u54ea\u4e2a\u597d","status":"publish","type":"post","link":"https:\/\/mushiming.com\/6196.html","title":{"rendered":"python\u7f16\u8f91\u5668_python\u548cc++\u54ea\u4e2a\u597d"},"content":{"rendered":"
\n

\"python\u7f16\u8f91\u5668_python\u548cc++\u54ea\u4e2a\u597d<\/p>\n

\u6bcf\u5468\uff0c\u4f1a\u7528\u5927\u767d\u8bdd\uff0c\u8bb2\u89e3\u4e00\u4e2a Python \u4e2d\u90a3\u4e9b\u96be\u4ee5\u7406\u89e3\u7684\u77e5\u8bc6\u70b9\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u6837\u7684\u767d\u8bdd\u8bb2\u89e3\uff0c\u53ef\u4ee5\u8ba9\u5927\u5bb6\u66f4\u597d\u7684\u5b66\u4f1a Python\u3002\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u7684\u4e3b\u9898\u662f\uff1a\u5927\u767d\u8bdd\u8bb2\u89e3 Python \u88c5\u9970\u5668\uff1b Python \u88c5\u9970\u5668\u662f\u5728\u9762\u8bd5\u8fc7\u7a0b\u9ad8\u9891\u88ab\u95ee\u5230\u7684\u95ee\u9898\uff0c\u88c5\u9970\u5668\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7528\u7684\u7279\u6027\uff0c\u719f\u7ec3\u638c\u63e1\u88c5\u9970\u5668\u4f1a\u8ba9\u4f60\u5f97\u5230\u4ee5\u4e0b\u51e0\u4e2a\u4f18\u52bf\uff1a<\/p>\n

\u7a0b\u5e8f\u53ef\u8bfb\u6027\u9ad8<\/p>\n

\u4ee3\u7801\u590d\u7528\u6027\u9ad8<\/p>\n

\u4f60\u7684\u7f16\u7a0b\u601d\u8def\u66f4\u52a0\u5bbd\u5e7f<\/p>\n

\u7a0b\u5e8f\u66f4\u52a0 pythonic\uff0c\u66f4\u50cf\u4e13\u4e1a\u5199 Python \u4ee3\u7801<\/p>\n

\u4f46\u5bf9\u4e8e\u65b0\u624b\u6765\u8bf4 Python \u88c5\u9970\u5668\u975e\u5e38\u96be\u4ee5\u7406\u89e3\uff1a<\/p>\n

\u4e0d\u6e05\u695a\u4ec0\u4e48\u662f\u88c5\u9970\u5668<\/p>\n

\u4e0d\u6e05\u695a\u88c5\u9970\u5668\u6709\u4ec0\u4e48\u7528<\/p>\n

\u4e0d\u6e05\u695a\u5982\u4f55\u5199\u88c5\u9970\u5668\u4ee3\u7801<\/p>\n

\u6240\u4ee5\u75f4\u6d77\u4eca\u5929\u7684 P \u767e\u79d1\uff0c\u4f1a\u5e26\u5927\u5bb6\u5f7b\u5e95\u7684\u5f04\u660e\u767d Python \u88c5\u9970\u5668\u3002 \u6587\u7ae0\u4e3b\u8981\u4ece\u4e0b\u9762 4 \u4e2a\u65b9\u9762\u8fdb\u884c\u8bb2\u89e3\uff1a 1 \u88c5\u9970\u5668\u662f\u4ec0\u4e482 \u88c5\u9970\u5668\u7406\u89e3\u57fa\u78403 \u5b9e\u73b0\u4e00\u4e2a\u88c5\u9970\u56684 \u88c5\u9970\u5668\u603b\u7ed3<\/p>\n

1 \u88c5\u9970\u5668\u662f\u4ec0\u4e48<\/p>\n

\u4fd7\u8bdd\u8bf4\uff1a\u827a\u672f\u6765\u6e90\u4e8e\u751f\u6d3b\u3002 \u75f4\u6d77\u8bf4\uff1a\u7f16\u7a0b\u4e5f\u6765\u81ea\u751f\u6d3b\u3002 \u7f16\u7a0b\u7684\u4e16\u754c\u4e5f\u662f\u5982\u6b64\uff0c\u5f88\u591a\u7f16\u7a0b\u4e16\u754c\u91cc\u7684\u6982\u5ff5\uff0c\u90fd\u662f\u6e90\u81ea\u751f\u6d3b\u4e2d\u7684\u65b9\u65b9\u9762\u9762\u3002 \u6bcf\u4e2a\u7f16\u7a0b\u8bed\u8a00\u88ab\u521b\u9020\u51fa\u6765\uff0c\u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u73b0\u5b9e\u4e16\u754c\u67d0\u4e2a\u5177\u4f53\u7684\u95ee\u9898\u3002 \u800c\u6211\u4eec\u7684\u4eca\u5929\u7684\u4e3b\u89d2\u300c\u88c5\u9970\u5668\u300d\u4e5f\u662f\u5982\u6b64\u3002 1.1 \u90a3\u4ec0\u4e48\u662f\u88c5\u9970\u5668\uff1f \u6211\u4eec\u6765\u770b\u4e00\u4e2a\u751f\u6d3b\u4e2d\u5177\u4f53\u7684\u6848\u4f8b\uff1a\u624b\u673a\u94a2\u5316\u819c\u3002 \u73b0\u5728\u57fa\u672c\u4eba\u624b\u4e00\u53f0\u624b\u673a\uff0c\u5927\u5bb6\u4e5f\u77e5\u9053\u624b\u673a\u5c4f\u5e55\u6454\u5012\u5730\u677f\uff0c\u662f\u975e\u5e38\u5bb9\u6613\u574f\u3002 \u624b\u673a\u5c4f\u5e55\u4e00\u574f\uff0c\u5927\u5bb6\u90fd\u975e\u5e38\u5fc3\u75db\uff0c\u56e0\u4e3a\u5982\u679c\u62ff\u51fa\u7ef4\u4fee\uff0c\u8d39\u7528\u90fd\u591f\u91cd\u65b0\u8d2d\u4e70\u4e00\u53f0\u65b0\u7684\u624b\u673a\u4e86\u3002 \u90a3\u6211\u4eec\u8981\u600e\u6837\u5728\u4e0d\u7834\u574f\u539f\u672c\u624b\u673a\u5c4f\u5e55\u7ed3\u6784\u7684\u540c\u65f6\uff0c\u8ba9\u6211\u4eec\u7684\u624b\u673a\u66f4\u8010\u574f\u5462\uff1f \u8fd9\u65f6\u5019\u4f60\u53ea\u9700\u8981\u82b1\u51e0\u5757\u94b1\u8d2d\u4e70\u4e00\u4e2a\u94a2\u5316\u819c\uff0c\u6765\u88c5\u9970\u6211\u4eec\u7684\u624b\u673a\u3002 \u6709\u4e86\u8fd9\u5757\u94a2\u5316\u819c\uff0c\u4ece\u6b64\u4f60\u5c31\u4e0d\u6015\u624b\u673a\u6454\u5012\u5730\u4e0a\u3002 \u624b\u673a\u94a2\u5316\u819c\u5c31\u662f\u73b0\u5b9e\u4e2d\u7684\u88c5\u9970\u5668\uff1a\u94a2\u5316\u819c\u5728\u4e0d\u6539\u53d8\u539f\u6709\u624b\u673a\u7684\u7ed3\u6784\u4e0b\uff0c\u8ba9\u624b\u673a\u5c4f\u5e55\u53d8\u7684\u66f4\u8010\u6454\u3002 \u800c\u6211\u4eec\u7f16\u7a0b\u4e2d\u7684\u88c5\u9970\u5668\uff0c\u540c\u6837\u7684\u9053\u7406\uff1a\u88c5\u9970\u5668\u53ef\u4ee5\u5728\u4e0d\u6539\u53d8\u539f\u6709\u51fd\u6570\u7684\u7ed3\u6784\u4e0b\uff0c\u8ba9\u7a0b\u5e8f\u66f4\u52a0\u5f3a\u5927\u3002 \u88c5\u9970\u5668\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a Python \u51fd\u6570\u6216\u7c7b\uff0c\u5b83\u53ef\u4ee5\u8ba9\u5176\u4ed6\u51fd\u6570\u6216\u7c7b\u5728\u4e0d\u9700\u8981\u505a\u4efb\u4f55\u4ee3\u7801\u4fee\u6539\u7684\u524d\u63d0\u4e0b\u589e\u52a0\u989d\u5916\u529f\u80fd\uff0c\u88c5\u9970\u5668\u7684\u8fd4\u56de\u503c\u4e5f\u662f\u4e00\u4e2a\u51fd\u6570\/\u7c7b\u5bf9\u8c61\u3002 1.2 \u88c5\u9970\u5668\u6709\u4ec0\u4e48\u4f5c\u7528 \u901a\u8fc7\u4e0a\u9762\u7684\u4f8b\u5b50\u4f60\u73b0\u5728\u5e94\u8be5\u662f\u80fd\u660e\u767d\u4ec0\u4e48\u662f\u88c5\u9970\u5668\uff1a\u7528\u6765\u88c5\u9970\u51fd\u6570\u7528\u7684\uff0c\u8ba9\u51fd\u6570\u66f4\u52a0\u5f3a\u5927\u3002 \u4f46\u4f60\u73b0\u5728\u8fd8\u662f\u4e0d\u77e5\u9053 Python \u91cc\u7684\u88c5\u9970\u5668\u957f\u4ec0\u4e48\u6837\uff0c\u5148\u4e0d\u7740\u6025\uff0c\u6211\u4eec\u5148\u6765\u770b\u4e0b\u88c5\u9970\u5668\u6709\u4ec0\u4e48\u4f5c\u7528\u3002 \u90a3\u88c5\u9970\u5668\u90fd\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f \u7b80\u5355\u8bf4\uff1a\u589e\u5f3a\u51fd\u6570\u7684\u529f\u80fd\u3002 \u5982\u679c\u4f60\u60f3\u5728\u4e0d\u6539\u53d8\u539f\u6709\u51fd\u6570\u7684\u57fa\u7840\u4e0a\uff0c\u589e\u52a0\u65b0\u7684\u51fd\u6570\u529f\u80fd\uff0c\u90a3\u88c5\u9970\u5668\u5c31\u975e\u5e38\u9002\u5408\u4f60\u3002 \u88c5\u9970\u5668\u7684\u5b58\u5728\u662f\u4e3a\u4e86\u9002\u7528\u4e24\u4e2a\u573a\u666f\uff1a 1 \u4e00\u4e2a\u662f\u589e\u5f3a\u88ab\u88c5\u9970\u51fd\u6570\u7684\u884c\u4e3a\uff1b2 \u53e6\u4e00\u4e2a\u662f\u4ee3\u7801\u590d\u7528\uff1b \u5177\u4f53\u4e3e\u51e0\u4e2a\u4f8b\u5b50\uff1a<\/p>\n

\u63d2\u5165\u65e5\u5fd7<\/p>\n

\u6027\u80fd\u6d4b\u8bd5<\/p>\n

\u4e8b\u52a1\u5904\u7406<\/p>\n

\u7f13\u5b58<\/p>\n

\u6743\u9650\u6821\u9a8c<\/p>\n

\u4e0a\u9762\u90fd\u662f\u5b9e\u9645\u5de5\u4f5c\u4e2d\u4f1a\u7ecf\u5e38\u9047\u5230\u7684\u573a\u666f\u3002 1.3 \u5c0f\u7ed3 \u770b\u5230\u8fd9\u91cc\u4f60\u5e94\u8be5\u662f\u53ef\u4ee5\u660e\u767d\u4ee5\u4e0b\u51e0\u4e2a\u77e5\u8bc6\u70b9\uff1a a \u4ec0\u4e48\u662f\u88c5\u9970\u5668 \u88c5\u9970\u5668\u53ef\u4ee5\u5728\u4e0d\u6539\u53d8\u539f\u6709\u51fd\u6570\u7684\u7ed3\u6784\u4e0b\uff0c\u8ba9\u7a0b\u5e8f\u66f4\u52a0\u5f3a\u5927\u3002 b \u88c5\u9970\u5668\u6709\u4ec0\u4e48\u4f5c\u7528 \u589e\u5f3a\u51fd\u6570\u7684\u529f\u80fd c \u88c5\u9970\u5668\u9002\u7528\u7684\u4e24\u4e2a\u573a\u666f a1 \u589e\u5f3a\u88ab\u88c5\u9970\u51fd\u6570\u7684\u884c\u4e3a\uff1bb1 \u4ee3\u7801\u590d\u7528\uff1b \u6709\u4e86\u4ee5\u4e0a 3 \u4e2a\u5b66\u524d\u77e5\u8bc6\uff0c\u4f60\u73b0\u5728\u5e94\u8be5\u5bf9\u88c5\u9970\u5668\uff0c\u6709\u4e86\u57fa\u672c\u7684\u8ba4\u77e5\u3002 \u4f46\u4f60\u4ecd\u7136\u4e0d\u77e5\u9053\u88c5\u9970\u5668\u7684\u4ee3\u7801\u5982\u4f55\u7f16\u5199\uff0c\u6240\u4ee5\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u624b\u628a\u624b\u6559\u4f1a\u5927\u5bb6\u5982\u4f55\u7f16\u5199\u4e00\u4e2a Python \u88c5\u9970\u5668\u4ee3\u7801\u3002 \u540c\u6837\u5728\u7f16\u5199\u88c5\u9970\u5668\u4ee3\u7801\u4e4b\u524d\uff0c\u6709 2 \u4e2a\u57fa\u7840\u4f60\u4e00\u5b9a\u8981\u5148\u77e5\u9053\u3002 a Python \u4e2d\u7684\u51fd\u6570\u4e5f\u662f\u53d8\u91cfb Python \u4e2d\u7684\u9ad8\u9636\u51fd\u6570 \u6211\u4eec\u6765\u5177\u4f53\u8bb2\u89e3\u8fd9\u4e2a 2 \u4e2a\u57fa\u7840\u3002 2 \u88c5\u9970\u5668\u7406\u89e3\u7684\u57fa\u7840<\/p>\n

2.1 Python \u4e2d\u7684\u51fd\u6570\u4e5f\u662f\u53d8\u91cf<\/p>\n

Python \u4e2d\u7684\u51fd\u6570\u53ef\u4ee5\u50cf\u666e\u901a\u53d8\u91cf\u4e00\u6837\uff0c\u5f53\u505a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u3002 \u6211\u4eec\u6765\u770b\u4e0b\u8fd9\u4e2a\u6848\u4f8b1\uff1a\u8f93\u51fa\u201c\u75f4\u6d77666\u201d<\/p>\n

1def chihai():<\/p>\n

2    print('\u75f4\u6d77666')<\/p>\n

3<\/p>\n

4chihai()<\/p>\n

5y = chihai<\/p>\n

6y()<\/p>\n

\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n

1\u75f4\u6d77666<\/p>\n

2\u75f4\u6d77666 \u5728\u4ee3\u7801\u4e2d\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u51fd\u6570 chihai\uff0c\u5e76\u8c03\u7528\u4e86 chihai \u51fd\u6570\uff0c\u5e76\u4e14\u628a chihai \u8d4b\u503c\u7ed9 y\u3002 y = chihai \u8868\u660e\u4e86\uff1a\u51fd\u6570\u540d\u53ef\u4ee5\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u5e76\u4e14\u4e0d\u5f71\u54cd\u8c03\u7528\u3002<\/p>\n

\u8fd9\u6837\u8bb2\uff0c\u53ef\u80fd\u8fd8\u6709\u4e9b\u4eba\u4e0d\u592a\u660e\u767d\u3002 \u6211\u4eec\u5728\u6765\u5bf9\u6bd4\u4e0b\u6211\u4eec\u5e38\u7528\u7684\u64cd\u4f5c\u3002 \u8fd9\u5176\u5b9e\u548c\u6574\u6570\u3001\u6570\u5b57\u662f\u4e00\u6837\u7684\uff0c<\/p>\n

\u4e0b\u9762\u7684\u4ee3\u7801\u4f60\u80af\u5b9a\u719f\u6089\uff1a<\/p>\n

1a = 1<\/p>\n

2b = a<\/p>\n

3print(a, b)<\/p>\n

2.2 Python \u4e2d\u7684\u9ad8\u9636\u51fd<\/p>\n

\u4ec0\u4e48\u662f\u9ad8\u9636\u51fd\u6570\uff1f<\/p>\n

\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u8981\u6ee1\u8db3\u5982\u4e0b\u7684\u4e24\u4e2a\u6761\u4ef6\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\uff1a a. \u53ef\u4ee5\u63a5\u6536\u51fd\u6570\u540d\u4f5c\u4e3a\u5b9e\u53c2\uff1bb. \u8fd4\u56de\u503c\u4e2d\u53ef\u4ee5\u5305\u542b\u51fd\u6570\u540d\u3002 \u4e00\u4e2a\u51fd\u6570\u63a5\u6536\u53e6\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd9\u79cd\u51fd\u6570\u79f0\u4e4b\u4e3a\u9ad8\u9636\u51fd\u6570\u3002 \u8fd9\u6837\u7684\u4e00\u4e2a\u51fd\u6570\u5c31\u662f\u9ad8\u9636\u51fd\u6570\uff0c\u540c\u6837\u6211\u4eec\u6539\u9020\u4e0b\u4e4b\u524d\u7684\u4ee3\u7801\uff0c\u8ba9\u5b83\u53d8\u6210\u9ad8\u9636\u51fd\u6570\uff1a \u6848\u4f8b2\uff1a\u9ad8\u9636\u51fd\u6570\u8f93\u51fa\u201c\u75f4\u6d77666\u201d<\/p>\n

1def chihai(name):<\/p>\n

2    print('{}666'.format(name))<\/p>\n

3<\/p>\n

4def name(func):<\/p>\n

5    func()<\/p>\n

6<\/p>\n

7name(chihai('\u75f4\u6d77'))<\/p>\n

\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n

1\u75f4\u6d77666<\/p>\n

\u5728\u6848\u4f8b2\u4e2d\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u51fd\u6570\uff1achihai \u548c name\uff0c\u5176\u4e2d pname\u51fd\u6570\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a func \u7684\u53c2\u6570\u3002 \u8fd9\u4e2a\u53c2\u6570\u6211\u4eec\u662f\u4f5c\u4e3a\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\uff0c\u4e5f\u5c31\u662f chihai \u8fd9\u4e2a\u51fd\u6570\uff0c\u4ece\u800c\u7f16\u5199\u51fa\u4e86\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\u3002 \u5728 Python \u6807\u51c6\u5e93\u4e2d\u6709\u8bb8\u591a\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u6bd4\u5982 map\u3001filter\u3001reduce\u3001sorted\u3002<\/p>\n

3 \u5b9e\u73b0\u4e00\u4e2a\u88c5\u9970\u5668<\/p>\n

\u73b0\u5728\u4f60\u5df2\u7ecf\u77e5\u9053\u4e86\u300c\u51fd\u6570\u540d\u8d4b\u503c\u300d\u548c\u300c\u9ad8\u9636\u51fd\u6570\u300d\uff0c\u6709\u4e86\u8fd9\u4e24\u4e2a\u57fa\u7840\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c1d\u8bd5\u5b9e\u73b0\u4e00\u4e2a\u7c7b\u4f3c\u7684\u88c5\u9970\u5668\u3002 \u8fd8\u662f\u521a\u624d\u4f8b\u5b50\uff0c\u6211\u4eec\u7ee7\u7eed\u6539\u9020\uff1a\u5b9e\u73b0\u4e0d\u4fee\u6539 chihai \u51fd\u6570\u7684\u7ed3\u6784\uff0c\u591a\u8f93\u51fa\u4e00\u53e5\u201c\u6211\u662f\u75f4\u6d77\u201d\u3002 \u6848\u4f8b3\uff1a\u7c7b\u4f3c\u88c5\u9970\u5668\u5b9e\u73b0\u8f93\u51fa\u201c\u75f4\u6d77666\u201d\u7684\u540c\u65f6\uff0c\u8f93\u51fa\u201c\u6211\u662f\u75f4\u6d77\u201d\u3002<\/p>\n

1def name(func):<\/p>\n

2    print('\u6211\u662f\u75f4\u6d77')<\/p>\n

3    return func<\/p>\n

4<\/p>\n

5def chihai():<\/p>\n

6    print('\u75f4\u6d77666')<\/p>\n

7<\/p>\n

8temp = name(chihai)<\/p>\n

9temp()<\/p>\n

\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n

1\u6211\u662f\u75f4\u6d77<\/p>\n

2\u75f4\u6d77666<\/p>\n

\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a name \u51fd\u6570\uff0cname \u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u540d\uff0c\u7136\u540e\u76f4\u63a5\u8fd4\u56de\u8be5\u51fd\u6570\u540d\u3002 \u8fd9\u6837\u6211\u4eec\u5b9e\u73b0\u4e86\u4e0d\u4fee\u6539\u539f\u51fd\u6570 chihai\uff0c\u5e76\u4e14\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u529f\u80fd\u7684\u9700\u6c42\uff1a\u591a\u8f93\u51fa\u4e86\u201c\u6211\u662f\u75f4\u6d77\u201d \u4f46\u662f\u8fd9\u91cc\u6709\u4e2a\u7f3a\u9677\u5c31\u662f\u51fd\u6570\u7684\u8c03\u7528\u65b9\u5f0f\u6539\u53d8\u4e86\u3002 \u5373\u4e0d\u662f\u539f\u672c\u7684 name\uff0c\u800c\u662f temp\u3002 \u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5f88\u7b80\u5355\uff0c\u76f8\u4fe1 a = a*3 \u8fd9\u6837\u7684\u8868\u8fbe\u5f0f\u5927\u5bb6\u90fd\u89c1\u8fc7\u3002 \u90a3\u4e48\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7684 temp = name(chihai)\uff0c\u540c\u6837\u53ef\u4ee5\u4fee\u6539\u4e3a:<\/p>\n

name = name(chihai) \u8fd9\u6837\u6211\u4eec\u5c31\u5b8c\u7f8e\u7684\u89e3\u51b3\u4e86\u95ee\u9898\uff1a\u65e2\u6dfb\u52a0\u65b0\u529f\u80fd\u53c8\u6ca1\u6709\u4fee\u6539\u539f\u51fd\u6570\u548c\u5176\u8c03\u7528\u65b9\u5f0f\u3002 \u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

1def name(func):<\/p>\n

2    print('\u6211\u662f\u75f4\u6d77')<\/p>\n

3    return func<\/p>\n

4<\/p>\n

5def chihai():<\/p>\n

6    print('\u75f4\u6d77666')<\/p>\n

7<\/p>\n

8name = name(chihai)<\/p>\n

9name()<\/p>\n

\u4f46\u8fd9\u6837\u7684\u4ee3\u7801\u5374\u6709\u4e2a\u4e0d\u4fbf\u4e4b\u5904\uff0c\u5373\u6bcf\u6b21\u4f7f\u7528\u8fd9\u6837\u7684\u88c5\u9970\u5668\uff0c\u6211\u4eec\u90fd\u8981\u5199\u7c7b\u4f3c name = status(name) \u7684\u4ee3\u7801\u3002 \u7a0b\u5e8f\u5458\u90fd\u662f\u61d2\u7684\uff0c\u6240\u4ee5\u624d\u6709\u90a3\u4e48\u591a\u9ad8\u7ea7\u7684\u8bed\u6cd5\u3002 \u5728 python \u4e2d\u4e3a\u4e86\u7b80\u5316\u8fd9\u79cd\u60c5\u51b5\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u8bed\u6cd5\u7cd6 @\u3002 @\u7b26\u53f7\u662f\u88c5\u9970\u5668\u7684\u8bed\u6cd5\u7cd6\uff0c\u5728\u5b9a\u4e49\u51fd\u6570\u7684\u65f6\u5019\u4f7f\u7528\uff0c\u907f\u514d\u518d\u4e00\u6b21\u8d4b\u503c\u64cd\u4f5c\u3002 \u5728\u6bcf\u4e2a\u88ab\u88c5\u9970\u7684\u51fd\u6570\u4e0a\u65b9\uff0c\u4f7f\u7528\u8fd9\u4e2a\u8bed\u6cd5\u7cd6\uff0c\u5c31\u53ef\u4ee5\u7701\u6389\u8fd9\u4e00\u53e5\u4ee3\u7801 name = chihai(name)\u3002 \u6700\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

1def name(func):<\/p>\n

2    print('\u6211\u662f\u75f4\u6d77')<\/p>\n

3    return func<\/p>\n

4<\/p>\n

5@name<\/p>\n

6def chihai():<\/p>\n

7    print('\u75f4\u6d77666')<\/p>\n

8<\/p>\n

9chihai()<\/p>\n

\u8fd9\u6837\u6211\u4eec\u5c31\u5f04\u6e05\u695a\u4e86\u88c5\u9970\u5668\u7684\u5de5\u4f5c\u539f\u7406\uff1a 1 \u5199\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\uff0c\u5373\u53c2\u6570\u662f\u51fd\u6570\uff0c\u8fd4\u56de\u7684\u4e5f\u662f\u51fd\u6570;2 \u5728\u5229\u7528\u8bed\u6cd5\u7cd6@\uff0c\u7b80\u5316\u8d4b\u503c\u64cd\u4f5c; \u6848\u4f8b3 \u6700\u540e\u7684\u4ee3\u7801\uff0c\u5c31\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u88c5\u9970\u5668\u5b9e\u73b0\u4ee3\u7801\u3002 \u4f46\u8fd9\u6837\u7684\u4ee3\u7801\u662f\u6709\u7f3a\u9677\uff1aname \u51fd\u6570\u76f4\u63a5\u8fd4\u56de\u4e86\u51fd\u6570\u540d\uff0c\u8fd9\u6837\u51fd\u6570\u8c03\u7528\u540e\u6211\u4eec\u5c31\u6ca1\u529e\u6cd5\u505a\u4efb\u4f55\u4e8b\u60c5\u3002 \u80fd\u770b\u8fd9\u7bc7\u6587\u7ae0\u7684\u540c\u5b66\uff0c\u80af\u5b9a\u662f\u559c\u6b22 Python \u8fd9\u95e8\u8bed\u8a00\u7684\uff0c\u4f46\u4e0d\u4e00\u5b9a\u6709\u5173\u6ce8\u75f4\u6d77\u7684\u516c\u4f17\u53f7\u3002 \u6240\u4ee5\u6211\u8981\u5728\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u6765\u6a21\u62df\u5224\u65ad\u5927\u5bb6\u662f\u5426\u5173\u6ce8\u4e86\u75f4\u6d77\u7684\u516c\u4f17\u53f7\u3002 \u4e3a\u4e86\u80fd\u5224\u65ad\u5927\u5bb6\u662f\u5426\u5173\u6ce8\u6211\uff0c\u6b64\u65f6\u6211\u5c31\u9700\u8981\u5728\u5d4c\u5957\u4e00\u5c42\u51fd\u6570\u3002 \u5c06\u5b9e\u73b0\u989d\u5916\u529f\u80fd\u7684\u90e8\u5206\u5199\u5728\u5185\u5c42\u51fd\u6570\u4e2d\uff0c\u7136\u540e\u5c06\u8fd9\u4e2a\u5185\u5c42\u51fd\u6570\u8fd4\u56de\u5373\u53ef\u3002 \u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u88c5\u9970\u5668\u90fd\u662f\u5d4c\u5957\u51fd\u6570\u7684\u539f\u56e0\u3002 \u4e0b\u9762\u662f\u5b9e\u73b0\u4e86\u80fd\u591f\u5904\u7406\u8fd4\u56de\u503c\u7684\u88c5\u9970\u5668\uff1a<\/p>\n

1def name(func):<\/p>\n

2    def follow_state():<\/p>\n

3        result = func()<\/p>\n

4        print(result)<\/p>\n

5        return result<\/p>\n

6    print('\u6211\u662f\u75f4\u6d77')<\/p>\n

7    return follow_state<\/p>\n

8<\/p>\n

9@name<\/p>\n

10def chihai():<\/p>\n

11    print('\u75f4\u6d77666')<\/p>\n

12    return '\u5b66 Python \u627e\u75f4\u6d77\uff01\u6211\u5df2\u5173\u6ce8\uff01'<\/p>\n

13<\/p>\n

14chihai()<\/p>\n

\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n

1\u6211\u662f\u75f4\u6d77<\/p>\n

2\u75f4\u6d77666<\/p>\n

3\u5b66 Python \u627e\u75f4\u6d77\uff01\u6211\u5df2\u5173\u6ce8\uff01<\/p>\n

\u73b0\u5728\u6211\u4eec\u5b9e\u73b0\u4e86\u80fd\u591f\u5904\u7406\u8fd4\u56de\u503c\u7684\u88c5\u9970\u5668\uff0c\u4f46\u53ef\u80fd\u4f60\u7684\u540d\u79f0\u4e0d\u662f\u53eb\u75f4\u6d77\uff0c\u53ef\u80fd\u53eb\u5c0f\u6d77\u3001\u5c0f\u75f4\uff0c\u5bf9\u4e0d\u5bf9\u3002 \u6240\u4ee5\u6211\u4eec\u8fd8\u9700\u8981\u5b9e\u73b0\u4e00\u7248\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668\uff0c\u8fd9\u6837\u5c31\u80fd\u4fee\u6539\u6211\u662f\u8c01\uff1a<\/p>\n

1def name(name):<\/p>\n

2    def judgen_follow(func):<\/p>\n

3        def follow_state():<\/p>\n

4            result = func()<\/p>\n

5            print(result)<\/p>\n

6            return func<\/p>\n

7        return follow_state<\/p>\n

8    print(\"\u6211\u662f{}\".format(name))<\/p>\n

9    return judgen_follow<\/p>\n

10<\/p>\n

11@name(name='\u5c0f\u75f4')<\/p>\n

12def chihai():<\/p>\n

13    print('\u75f4\u6d77666')<\/p>\n

14    return '\u5b66 Python \u627e\u75f4\u6d77\uff01\u6211\u5df2\u5173\u6ce8\uff01'<\/p>\n

15<\/p>\n

16chihai()<\/p>\n

\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n

1\u6211\u662f\u5c0f\u75f4<\/p>\n

2\u75f4\u6d77666<\/p>\n

3\u5b66 Python \u627e\u75f4\u6d77\uff01\u6211\u5df2\u5173\u6ce8\uff01<\/p>\n

\u4e0a\u9762\u7684 name \u662f\u5141\u8bb8\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668\u3002 \u5b83\u5b9e\u9645\u4e0a\u662f\u5bf9\u539f\u6709\u88c5\u9970\u5668\u7684\u4e00\u4e2a\u51fd\u6570\u5c01\u88c5\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u88c5\u9970\u5668\u3002 \u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u7406\u89e3\u4e3a\u4e00\u4e2a\u542b\u6709\u53c2\u6570\u7684\u95ed\u5305\u3002 \u5f53\u6211\u4eec\u4f7f\u7528@name(name=\"\u5c0f\u75f4\")\u8c03\u7528\u7684\u65f6\u5019\uff0cPython \u80fd\u591f\u53d1\u73b0\u8fd9\u4e00\u5c42\u7684\u5c01\u88c5\uff0c\u5e76\u628a\u53c2\u6570\u4f20\u9012\u5230\u88c5\u9970\u5668\u7684\u73af\u5883\u4e2d\u3002 @name(name=\"warn\")\u7b49\u4ef7\u4e8e@judgen_follow\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u5b9e\u73b0\u4e86\u4e00\u4e2a\u53ef\u4ee5\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668\u3002<\/p>\n

4 \u88c5\u9970\u5668\u603b\u7ed3 \u6700\u540e\u6211\u628a\u4eca\u5929\u91cd\u8981\u7684\u77e5\u8bc6\u70b9\uff0c\u5168\u90e8\u7ed9\u5927\u5bb6\u4e00\u8d77\u603b\u7ed3\u51fa\u6765\uff1a<\/p>\n

1 \u4ec0\u4e48\u662f\u88c5\u9970\u5668 a. \u5927\u767d\u8bdd\u603b\u7ed3\u88c5\u9970\u5668\uff1a\u4fdd\u62a4\u624b\u673a\u7684\u624b\u673a\u58f3b. \u4e13\u4e1a\u8bdd\u603b\u7ed3\u88c5\u9970\u5668\uff1a\u5904\u7406\u51fd\u6570\u7684\u51fd\u6570 2 \u88c5\u9970\u5668\u6709\u4ec0\u4e48\u7528 a. \u589e\u5f3a\u51fd\u6570\u7684\u529f\u80fd 3 \u88c5\u9970\u5668\u4f7f\u7528\u573a\u666f a. \u589e\u5f3a\u88ab\u88c5\u9970\u51fd\u6570\u7684\u884c\u4e3ab. \u4ee3\u7801\u590d\u7528 4 \u88c5\u9970\u5668\u7406\u89e3\u7684\u57fa\u7840 a. Python \u4e2d\u7684\u51fd\u6570\u4e5f\u662f\u53d8\u91cfb. \u4e00\u4e2a\u51fd\u6570\u63a5\u6536\u53e6\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd9\u79cd\u51fd\u6570\u79f0\u4e4b\u4e3a\u9ad8\u9636\u51fd\u6570\u3002c. @\u7b26\u53f7\u662f\u88c5\u9970\u5668\u7684\u8bed\u6cd5\u7cd6\uff0c\u5728\u5b9a\u4e49\u51fd\u6570\u7684\u65f6\u5019\u4f7f\u7528\uff0c\u907f\u514d\u518d\u4e00\u6b21\u8d4b\u503c\u64cd\u4f5c\u3002<\/p>\n

\u88c5\u9970\u5668\u7684\u672c\u8d28\u662f\u51fd\u6570\uff0c\u5176\u53c2\u6570\u662f\u53e6\u4e00\u4e2a\u51fd\u6570(\u88ab\u88c5\u9970\u7684\u51fd\u6570)\u3002 \u88c5\u9970\u5668\u901a\u5e38\u4f1a\u989d\u5916\u5904\u7406\u88ab\u88c5\u9970\u7684\u51fd\u6570\uff0c\u7136\u540e\u628a\u5b83\u8fd4\u56de\uff0c\u6216\u8005\u5c06\u5176\u66ff\u6362\u6210\u53e6\u4e00\u4e2a\u51fd\u6570\u6216\u53ef\u8c03\u7528\u5bf9\u8c61\u3002 \u884c\u4e3a\u826f\u597d\u7684\u88c5\u9970\u5668\u53ef\u4ee5\u91cd\u7528\uff0c\u4ee5\u51cf\u5c11\u4ee3\u7801\u91cf\u3002 \u5bf9\u4e8e\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u4e8c\u5c42\u88c5\u9970\u5668\uff0c\u5176\u5b9e\u5c31\u662f\u5916\u5c42\u51fd\u6570\u4f20\u5165\u88ab\u88c5\u9970\u7684\u51fd\u6570\u7684\u51fd\u6570\u540d\u3002 \u5185\u5c42\u51fd\u6570\u8981\u4e48\u8c03\u7528\u8fd9\u4e2a\u88ab\u88c5\u9970\u7684\u51fd\u6570\uff0c\u8981\u4e48\u8fd4\u56de\u8fd9\u4e2a\u88ab\u88c5\u9970\u7684\u51fd\u6570\u7684\u51fd\u6570\u540d\u3002 \u5916\u5c42\u51fd\u6570\u8fd4\u56de\u5185\u5c42\u51fd\u6570\u7684\u51fd\u6570\u540d\u3002 \u88c5\u9970\u7684\u8fc7\u7a0b\u5728\u5185\u5c42\u51fd\u6570\u4e2d\u5b8c\u6210\u3002 \u5bf9\u4e8e\u4eca\u5929\u7684\u6587\u7ae0\uff0c\u6211\u603b\u7ed3\u4e86\u4e0b\uff1a<\/p>\n

1\u6211\u662f\u75f4\u6d77\uff0c\u4e00\u4f4d 95 \u540e\u7684 coder\u3002<\/p>\n

2\u75f4\u6d77666<\/p>\n

3\u5b66 Python \u627e\u75f4\u6d77\uff01\u6211\u5df2\u5173\u6ce8\uff01<\/p>\n

\"python\u7f16\u8f91\u5668_python\u548cc++\u54ea\u4e2a\u597d<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"python\u7f16\u8f91\u5668_python\u548cc++\u54ea\u4e2a\u597d\u6bcf\u5468\uff0c\u4f1a\u7528\u5927\u767d\u8bdd\uff0c\u8bb2\u89e3\u4e00\u4e2aPython\u4e2d\u90a3\u4e9b\u96be\u4ee5\u7406\u89e3\u7684\u77e5\u8bc6\u70b9","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6196"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=6196"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6196\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=6196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=6196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=6196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}