**kwargs<\/code><\/h1>\n\n\n\n\u6211\u89c2\u5bdf\u5230\uff0c\u5927\u90e8\u5206\u65b0\u7684Python\u7a0b\u5e8f\u5458\u90fd\u9700\u8981\u82b1\u4e0a\u5927\u91cf\u65f6\u95f4\u7406\u89e3\u6e05\u695a *args<\/code> \u548c**kwargs<\/code>\u8fd9\u4e24\u4e2a\u9b54\u6cd5\u53d8\u91cf\u3002\u90a3\u4e48\u5b83\u4eec\u5230\u5e95\u662f\u4ec0\u4e48?<\/p>\n\n\n\n\u9996\u5148\u8ba9\u6211\u544a\u8bc9\u4f60, \u5176\u5b9e\u5e76\u4e0d\u662f\u5fc5\u987b\u5199\u6210*args<\/code>\u00a0\u548c**kwargs<\/code>\u3002 \u53ea\u6709\u53d8\u91cf\u524d\u9762\u7684\u00a0*<\/code>(\u661f\u53f7)\u624d\u662f\u5fc5\u987b\u7684. \u4f60\u4e5f\u53ef\u4ee5\u5199\u6210*var<\/code>\u00a0\u548c**vars<\/code>. \u800c\u5199\u6210*args<\/code>\u00a0\u548c**kwargs<\/code>\u53ea\u662f\u4e00\u4e2a\u901a\u4fd7\u7684\u547d\u540d\u7ea6\u5b9a\u3002 \u90a3\u5c31\u8ba9\u6211\u4eec\u5148\u770b\u4e00\u4e0b*args<\/code>\u5427\u3002<\/p>\n\n\n\n*args \u7684\u7528\u6cd5<\/h1>\n\n\n\n
*args<\/code> \u548c **kwargs<\/code> \u4e3b\u8981\u7528\u4e8e\u51fd\u6570\u5b9a\u4e49\u3002 \u4f60\u53ef\u4ee5\u5c06\u4e0d\u5b9a\u6570\u91cf\u7684\u53c2\u6570\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n\n\n\n\u8fd9\u91cc\u7684\u4e0d\u5b9a\u7684\u610f\u601d\u662f\uff1a\u9884\u5148\u5e76\u4e0d\u77e5\u9053, \u51fd\u6570\u4f7f\u7528\u8005\u4f1a\u4f20\u9012\u591a\u5c11\u4e2a\u53c2\u6570\u7ed9\u4f60, \u6240\u4ee5\u5728\u8fd9\u4e2a\u573a\u666f\u4e0b\u4f7f\u7528\u8fd9\u4e24\u4e2a\u5173\u952e\u5b57\u3002 *args<\/code> \u662f\u7528\u6765\u53d1\u9001\u4e00\u4e2a\u975e\u952e\u503c\u5bf9\u7684\u53ef\u53d8\u6570\u91cf\u7684\u53c2\u6570\u5217\u8868\u7ed9\u4e00\u4e2a\u51fd\u6570.<\/p>\n\n\n\n\u8fd9\u91cc\u6709\u4e2a\u4f8b\u5b50\u5e2e\u4f60\u7406\u89e3\u8fd9\u4e2a\u6982\u5ff5:<\/p>\n\n\n\n
def test_var_args(f_arg, *argv):\n print(\"first normal arg:\", f_arg)\n for arg in argv:\n print(\"another arg through *argv:\", arg)\n\ntest_var_args('yasoob', 'python', 'eggs', 'test')\n<\/code><\/pre>\n\n\n\n\u8fd9\u4f1a\u4ea7\u751f\u5982\u4e0b\u8f93\u51fa:<\/p>\n\n\n\n
first normal arg: yasoob\nanother arg through *argv: python\nanother arg through *argv: eggs\nanother arg through *argv: test\n<\/code><\/pre>\n\n\n\n**kwargs \u7684\u7528\u6cd5<\/h1>\n\n\n\n
**kwargs<\/code> \u5141\u8bb8\u4f60\u5c06\u4e0d\u5b9a\u957f\u5ea6\u7684\u952e\u503c\u5bf9<\/strong>, \u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\u3002 \u5982\u679c\u4f60\u60f3\u8981\u5728\u4e00\u4e2a\u51fd\u6570\u91cc\u5904\u7406\u5e26\u540d\u5b57\u7684\u53c2\u6570<\/strong>, \u4f60\u5e94\u8be5\u4f7f\u7528**kwargs<\/code>\u3002<\/p>\n\n\n\n\u8fd9\u91cc\u6709\u4e2a\u8ba9\u4f60\u4e0a\u624b\u7684\u4f8b\u5b50:<\/p>\n\n\n\n
def greet_me(**kwargs):\n for key, value in kwargs.items():\n print(\"{0} == {1}\".format(key, value))\n\n\n>>> greet_me(name=\"yasoob\")\nname == yasoob\n<\/code><\/pre>\n\n\n\n\u73b0\u5728\u4f60\u53ef\u4ee5\u770b\u51fa\u6211\u4eec\u600e\u6837\u5728\u4e00\u4e2a\u51fd\u6570\u91cc, \u5904\u7406\u4e86\u4e00\u4e2a\u952e\u503c\u5bf9<\/strong>\u53c2\u6570\u4e86\u3002<\/p>\n\n\n\n\u8fd9\u5c31\u662f**kwargs<\/code>\u7684\u57fa\u7840, \u800c\u4e14\u4f60\u53ef\u4ee5\u770b\u51fa\u5b83\u6709\u591a\u4e48\u7ba1\u7528\u3002 \u63a5\u4e0b\u6765\u8ba9\u6211\u4eec\u8c08\u8c08\uff0c\u4f60\u600e\u6837\u4f7f\u7528*args<\/code>\u00a0\u548c\u00a0**kwargs<\/code>\u6765\u8c03\u7528\u4e00\u4e2a\u53c2\u6570\u4e3a\u5217\u8868\u6216\u8005\u5b57\u5178\u7684\u51fd\u6570\u3002<\/p>\n\n\n\n\u4f7f\u7528 *args<\/code> \u548c **kwargs<\/code> \u6765\u8c03\u7528\u51fd\u6570<\/h1>\n\n\n\n\u90a3\u73b0\u5728\u6211\u4eec\u5c06\u770b\u5230\u600e\u6837\u4f7f\u7528*args<\/code>\u548c**kwargs<\/code> \u6765\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u3002 \u5047\u8bbe\uff0c\u4f60\u6709\u8fd9\u6837\u4e00\u4e2a\u5c0f\u51fd\u6570\uff1a<\/p>\n\n\n\ndef test_args_kwargs(arg1, arg2, arg3):\n print(\"arg1:\", arg1)\n print(\"arg2:\", arg2)\n print(\"arg3:\", arg3)\n<\/code><\/pre>\n\n\n\n\u4f60\u53ef\u4ee5\u4f7f\u7528*args<\/code>\u6216**kwargs<\/code>\u6765\u7ed9\u8fd9\u4e2a\u5c0f\u51fd\u6570\u4f20\u9012\u53c2\u6570\u3002 \u4e0b\u9762\u662f\u600e\u6837\u505a\uff1a<\/p>\n\n\n\n# \u9996\u5148\u4f7f\u7528 *args\n>>> args = (\"two\", 3, 5)\n>>> test_args_kwargs(*args)\narg1: two\narg2: 3\narg3: 5\n\n# \u73b0\u5728\u4f7f\u7528 **kwargs:\n>>> kwargs = {\"arg3\": 3, \"arg2\": \"two\", \"arg1\": 5}\n>>> test_args_kwargs(**kwargs)\narg1: 5\narg2: two\narg3: 3\n<\/code><\/pre>\n\n\n\n\u6807\u51c6\u53c2\u6570\u4e0e*args\u3001**kwargs<\/code>\u5728\u4f7f\u7528\u65f6\u7684\u987a\u5e8f<\/h3>\n\n\n\n\u90a3\u4e48\u5982\u679c\u4f60\u60f3\u5728\u51fd\u6570\u91cc\u540c\u65f6\u4f7f\u7528\u6240\u6709\u8fd9\u4e09\u79cd\u53c2\u6570\uff0c \u987a\u5e8f\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n\n\n\n
some_func(fargs, *args, **kwargs)\n\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528\u5b83\u4eec\uff1f\n\u8fd9\u8fd8\u771f\u7684\u8981\u770b\u4f60\u7684\u9700\u6c42\u800c\u5b9a\u3002\n\n\u6700\u5e38\u89c1\u7684\u7528\u4f8b\u662f\u5728\u5199\u51fd\u6570\u88c5\u9970\u5668\u7684\u65f6\u5019\uff08\u4f1a\u5728\u53e6\u4e00\u7ae0\u91cc\u8ba8\u8bba\uff09\u3002\n\n\u6b64\u5916\u5b83\u4e5f\u53ef\u4ee5\u7528\u6765\u505a\u7334\u5b50\u8865\u4e01(monkey patching)\u3002\u7334\u5b50\u8865\u4e01\u7684\u610f\u601d\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6(runtime)\u4fee\u6539\u67d0\u4e9b\u4ee3\u7801\u3002 \u6253\u4e2a\u6bd4\u65b9\uff0c\u4f60\u6709\u4e00\u4e2a\u7c7b\uff0c\u91cc\u9762\u6709\u4e2a\u53ebget_info\u7684\u51fd\u6570\u4f1a\u8c03\u7528\u4e00\u4e2aAPI\u5e76\u8fd4\u56de\u76f8\u5e94\u7684\u6570\u636e\u3002\u5982\u679c\u6211\u4eec\u60f3\u6d4b\u8bd5\u5b83\uff0c\u53ef\u4ee5\u628aAPI\u8c03\u7528\u66ff\u6362\u6210\u4e00\u4e9b\u6d4b\u8bd5\u6570\u636e\u3002\u4f8b\u5982\uff1a\n\nimport someclass\n\ndef get_info(self, *args):\n return \"Test data\"\n\nsomeclass.get_info = get_info\n\u6211\u6562\u80af\u5b9a\u4f60\u4e5f\u53ef\u4ee5\u60f3\u8c61\u5230\u4e00\u4e9b\u5176\u4ed6\u7684\u7528\u4f8b\u3002\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u539f\u6587\uff1a https:\/\/docs.pythontab.com\/interpy\/ *args \u548c&nb<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,13],"tags":[],"yoast_head":"\n
1.0 *args \u548c **kwargs \uff08\u5b66\u4e60\u8bb0\u5f55\uff09 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n