{"id":50054,"date":"2024-12-03T16:41:58","date_gmt":"2024-12-03T08:41:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50054\/"},"modified":"2024-12-03T16:41:58","modified_gmt":"2024-12-03T08:41:58","slug":"python-%e7%b1%bb%e6%96%b9%e6%b3%95%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e5%8a%a8%e6%80%81%e5%8f%82%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50054\/","title":{"rendered":"Python \u7c7b\u65b9\u6cd5\u5982\u4f55\u5904\u7406\u52a8\u6001\u53c2\u6570\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u7c7b\u65b9\u6cd5\u5982\u4f55\u5904\u7406\u52a8\u6001\u53c2\u6570\uff1f<\/h1>\n<p>\u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aPython \u7c7b\u65b9\u6cd5\u5982\u4f55\u5904\u7406\u52a8\u6001\u53c2\u6570\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241110\/173123966867309ef4c64c6.jpg\" class=\"aligncenter\" title=\"Python \u7c7b\u65b9\u6cd5\u5982\u4f55\u5904\u7406\u52a8\u6001\u53c2\u6570\uff1f\u63d2\u56fe\" alt=\"Python \u7c7b\u65b9\u6cd5\u5982\u4f55\u5904\u7406\u52a8\u6001\u53c2\u6570\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u52a8\u6001\u53c2\u6570\u7684\u5e94\u7528\u4e0e\u7406\u89e3<\/strong><\/p>\n<p>\u5728 python \u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u65f6\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u8981\u4f20\u5165\u52a8\u6001\u53c2\u6570\u7684\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u6709\u6388\u6743\u9650\u5236\u7684\u7c7b\u65b9\u6cd5\uff0c\u5b83\u7684\u53c2\u6570\u6570\u91cf\u53ef\u80fd\u56e0\u4e0d\u540c\u7684\u6388\u6743\u7b49\u7ea7\u800c\u5f02\u3002<\/p>\n<p>\u539f\u672c\uff0c\u6211\u4eec\u53ef\u80fd\u5b9a\u4e49\u4e00\u4e2a\u5e26\u6709\u53ef\u53d8\u53c2\u6570\u7684\u7c7b\u65b9\u6cd5\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>class basecontroller:\n    def authorized(*args):\n        # ...<\/pre>\n<p>\u7136\u800c\uff0c\u8c03\u7528\u65f6\u4f1a\u9047\u5230\u9519\u8bef\uff1a<\/p>\n<pre>typeerror: aothorized() takes 1 positional argument but 2 were given<\/pre>\n<p>\u539f\u56e0\u5728\u4e8e\uff0c\u53ef\u53d8\u53c2\u6570 *args \u63a5\u6536\u7684\u662f\u4e00\u4e2a\u5143\u7ec4\uff0c\u800c\u5173\u952e\u5b57\u53c2\u6570 <strong>kwargs \u63a5\u6536\u7684\u662f\u4e00\u4e2a\u5b57\u5178\u3002\u5bf9\u4e8e\u52a8\u6001\u53c2\u6570\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528 <\/strong>kwargs \u6765\u63a5\u6536\u3002<\/p>\n<p>\u6b63\u786e\u5b9a\u4e49\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>class basecontroller:\n    def authorized(**kwargs):\n        # ...<\/pre>\n<p>\u8fd9\u6837\uff0c\u8c03\u7528\u65f6\u53ef\u4ee5\u4f20\u5165\u4efb\u610f\u6570\u91cf\u7684\u5173\u952e\u5b57\u53c2\u6570\uff1a<\/p>\n<pre>isok = _baseController.authorized(user_id=123, role='admin')<\/pre>\n<p>\u53e6\u5916\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u7c7b\u578b\u7684\u53c2\u6570\u6709\u4e0d\u540c\u7684\u89e3\u6790\u987a\u5e8f\u548c\u9650\u5236\uff1a<\/p>\n<ul>\n<li>\u4f4d\u7f6e\u53c2\u6570\uff1a\u5fc5\u987b\u5728\u52a8\u6001\u53c2\u6570\u4e4b\u524d\u89e3\u6790\uff0c\u4e14\u6570\u91cf\u5fc5\u987b\u56fa\u5b9a<\/li>\n<li>\u5173\u952e\u5b57\u53c2\u6570\uff1a\u53ef\u4ee5\u4e0e\u52a8\u6001\u53c2\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u4f46\u5fc5\u987b\u5728\u52a8\u6001\u53c2\u6570\u4e4b\u540e<\/li>\n<li>\u9ed8\u8ba4\u503c\u53c2\u6570\uff1a\u53ef\u4ee5\u4e0e\u52a8\u6001\u53c2\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u4f46\u5fc5\u987b\u5728\u52a8\u6001\u53c2\u6570\u4e4b\u524d<\/li>\n<\/ul>\n<p>\u4e07\u80fd\u5f62\u53c2 <strong>args \u548c <\/strong>kwargs \u53ef\u4ee5\u63a5\u6536\u4efb\u610f\u6570\u91cf\u548c\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5143\u7ec4\u548c\u5b57\u5178\u4e2d\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPython \u7c7b\u65b9\u6cd5\u5982\u4f55\u5904\u7406\u52a8\u6001\u53c2\u6570\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u7c7b\u65b9\u6cd5\u5982\u4f55\u5904\u7406\u52a8\u6001\u53c2\u6570\uff1f \u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aPython \u7c7b\u65b9\u6cd5\u5982\u4f55\u5904\u7406\u52a8\u6001\u53c2\u6570\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u52a8\u6001\u53c2\u6570\u7684\u5e94\u7528\u4e0e\u7406\u89e3 \u5728 python \u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u65f6\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u8981\u4f20\u5165\u52a8\u6001\u53c2\u6570\u7684\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u6709\u6388\u6743\u9650\u5236\u7684\u7c7b\u65b9\u6cd5\uff0c\u5b83\u7684\u53c2\u6570\u6570\u91cf\u53ef\u80fd\u56e0\u4e0d\u540c\u7684\u6388\u6743\u7b49\u7ea7\u800c\u5f02\u3002 \u539f\u672c\uff0c\u6211\u4eec\u53ef\u80fd\u5b9a\u4e49\u4e00\u4e2a\u5e26\u6709\u53ef\u53d8\u53c2\u6570\u7684\u7c7b\u65b9\u6cd5\uff0c\u5982\u4e0b\u6240\u793a\uff1a class basecontroller: def authorized(*args): # &#8230; \u7136\u800c\uff0c\u8c03\u7528\u65f6\u4f1a\u9047\u5230\u9519\u8bef\uff1a typeerror: aothorized() takes 1 positional argument but 2 were given \u539f\u56e0\u5728\u4e8e\uff0c\u53ef\u53d8\u53c2\u6570 *args \u63a5\u6536\u7684\u662f\u4e00\u4e2a\u5143\u7ec4\uff0c\u800c\u5173\u952e\u5b57\u53c2\u6570 kwargs \u63a5\u6536\u7684\u662f\u4e00\u4e2a\u5b57\u5178\u3002\u5bf9\u4e8e\u52a8\u6001\u53c2\u6570\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528 kwargs \u6765\u63a5\u6536\u3002 \u6b63\u786e\u5b9a\u4e49\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a class basecontroller: def authorized(**kwargs): # &#8230; \u8fd9\u6837\uff0c\u8c03\u7528\u65f6\u53ef\u4ee5\u4f20\u5165\u4efb\u610f\u6570\u91cf\u7684\u5173\u952e\u5b57\u53c2\u6570\uff1a isok = _baseController.authorized(user_id=123, role=&#8217;admin&#8217;) \u53e6\u5916\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u7c7b\u578b\u7684\u53c2\u6570\u6709\u4e0d\u540c\u7684\u89e3\u6790\u987a\u5e8f\u548c\u9650\u5236\uff1a \u4f4d\u7f6e\u53c2\u6570\uff1a\u5fc5\u987b\u5728\u52a8\u6001\u53c2\u6570\u4e4b\u524d\u89e3\u6790\uff0c\u4e14\u6570\u91cf\u5fc5\u987b\u56fa\u5b9a \u5173\u952e\u5b57\u53c2\u6570\uff1a\u53ef\u4ee5\u4e0e\u52a8\u6001\u53c2\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u4f46\u5fc5\u987b\u5728\u52a8\u6001\u53c2\u6570\u4e4b\u540e \u9ed8\u8ba4\u503c\u53c2\u6570\uff1a\u53ef\u4ee5\u4e0e\u52a8\u6001\u53c2\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u4f46\u5fc5\u987b\u5728\u52a8\u6001\u53c2\u6570\u4e4b\u524d \u4e07\u80fd\u5f62\u53c2 args \u548c kwargs \u53ef\u4ee5\u63a5\u6536\u4efb\u610f\u6570\u91cf\u548c\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5143\u7ec4\u548c\u5b57\u5178\u4e2d\u3002 \u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPython [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-50054","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=50054"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50054\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}