{"id":8903,"date":"2024-05-22T16:01:01","date_gmt":"2024-05-22T08:01:01","guid":{"rendered":""},"modified":"2024-05-22T16:01:01","modified_gmt":"2024-05-22T08:01:01","slug":"python\u4e2dargs\u7684\u7528\u6cd5_args\u548ckwargs\u7684\u533a\u522b[\u901a\u4fd7\u6613\u61c2]","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8903.html","title":{"rendered":"python\u4e2dargs\u7684\u7528\u6cd5_args\u548ckwargs\u7684\u533a\u522b[\u901a\u4fd7\u6613\u61c2]"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

*args<\/code> \u548c **kwargs<\/code>\u7684\u7528\u6cd5<\/h2>\n
\n

\u731b\u6ef4\u6253\u5f00\u535a\u5ba2\uff0c\u53d1\u73b0\u5b9e\u5728\u6709\u6bb5\u65f6\u95f4\u6ca1\u66f4\u65b0\u4e86\u3002\u53c8\u521a\u597d\u7528\u5230\u4e86**kwargs \u3002
\u9042\u60f3\u8d77\u4e86\u8bb8\u4e45\u4e4b\u524d\u603b\u7ed3\u7684\u8fd9\u7bc7\u535a\u5ba2\uff0c\u5938\u5f20\u70b9\u8bf4\u4e5f\u7b97\u662f\u81ea\u5df1\u7684\u4e00\u4e2a\u5455\u5fc3\u6ca5\u8840\u4e4b\u4f5c\u5427\u3002<\/p>\n<\/blockquote>\n

\u76f8\u4fe1\u4e0d\u5c11\u540c\u5b66\u5728\u770b\u5927\u795e\u7684\u7a0b\u5e8f\u65f6\uff0c\u603b\u4f1a\u770b\u89c1 *args, **kwargs \u8fd9\u6837\u7684\u5199\u6cd5\uff0c\u4f46\u4f60\u77e5\u9053\u8fd9\u79cd\u5199\u6cd5\u7684\u542b\u4e49\u662f\u4ec0\u4e48\u5417\uff1f<\/p>\n

\u5bf9\u4e8e\u5927\u90e8\u5206Python\u521d\u5b66\u8005\u800c\u8a00\uff0c*args<\/code> \u548c ``kwargs` \u8fd9\u4e24\u4e2a\u9b54\u6cd5\u53d8\u91cf\u9700\u8981\u82b1\u8d39\u5927\u91cf\u65f6\u95f4\u6765\u89e3\u91ca\u3002\u522b\u88ab\u8fd9\u4e9b\u8bed\u53e5\u6240\u7eca\u5012\u3002\u5176\u5b9e\u8fd9\u4e9b\u5e76\u4e0d\u662f\u4ec0\u4e48\u8d85\u7ea7\u7279\u6b8a\u7684\u53c2\u6570\uff0c\u4e5f\u5e76\u4e0d\u5947\u7279\uff0c\u53ea\u662f\u7f16\u7a0b\u4eba\u5458\u7ea6\u5b9a\u7684\u53d8\u91cf\u540d\u5b57\uff0cargs \u662f arguments \u7684\u7f29\u5199\uff0c\u8868\u793a\u4f4d\u7f6e\u53c2\u6570\uff1bkwargs \u662f keyword arguments \u7684\u7f29\u5199\uff0c\u8868\u793a\u5173\u952e\u5b57\u53c2\u6570\u3002 \u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u6765\u4e00\u8d77\u4e86\u89e3<\/strong>\u4ed6\u4eec\u7684\u7528\u6cd5**\uff0c\u4ee5\u53ca\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u4f7f\u7528\uff1f\u00b7<\/strong><\/p>\n

\u9996\u5148\u4f60\u8981\u660e\u767d\u7684\u662f:<\/p>\n

    \n
  • \u5176\u5b9e\u5e76\u4e0d\u662f\u5199\u6210 *args<\/code> \u548c **kwargs<\/code> \uff0c\u53ea\u6709\u524d\u9762\u7684 * \uff08\u661f\u53f7\uff09\u624d\u662f\u5fc5\u987b\u7684\u3002<\/li>\n
  • \u5411python\u4f20\u9012\u53c2\u6570\u7684\u65b9\u5f0f\u6709\u4e24\u79cd:
    \u4f4d\u7f6e\u53c2\u6570\uff08positional argument\uff09
    \u5173\u952e\u8bcd\u53c2\u6570\uff08keyword argument\uff09<\/li>\n<\/ul>\n

    \u73b0\u5728\u6211\u4eec\u518d\u6765\u770b *args \u4e0e **kwargs \u7684\u533a\u522b\uff0c\u4e24\u8005\u90fd\u662f python \u4e2d\u53ef\u53d8\u7684\u53c2\u6570
    *args \u8868\u793a\u4efb\u4f55\u591a\u4e2a\u65e0\u540d\u53c2\u6570\uff0c \u4ed6\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a tuple
    ** kwargs \u8868\u793a\u5173\u952e\u5b57\u53c2\u6570\uff0c \u5b83\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a dict<\/p>\n

    \u540c\u65f6\u4f7f\u7528\u65f6\u5fc5\u987b\u8981\u6c42 *args \u53c2\u6570\u5217\u8981\u5728** kwargs \u524d\u9762 \u3010\u56e0\u4e3a\u4f4d\u7f6e\u53c2\u6570\u5728\u5173\u952e\u5b57\u53c2\u6570\u7684\u524d\u9762\u3002\u3011<\/p>\n

    Splat \u8fd0\u7b97\u7b26<\/h3>\n

    *<\/code> \u5e38\u5e38\u4e0e\u4e58\u6cd5\u8fd0\u7b97\uff0cC\u8bed\u8a00\u4e2d\u7684\u6307\u9488 \u6709\u5173\uff0c\u4f46\u5728python\u4e2d\uff0c\u5219\u662f splat<\/code> \u8fd0\u7b97\u7b26\u7684 \u4e24\u500d\u3002\u4e14 splat\u8fd0\u7b97\u7b26\u9700\u8981\u66f4\u5f3a\u5927\u7684\u7c7b\u6bd4<\/p>\n

    a = [1,2,3]\nb = [*a,4,5,6]\nb \n# ----------------- \u8f93\u51fa\u7ed3\u679c -----------------\n# [1, 2, 3, 4, 5, 6]\n# ----------------- \u603b\u7ed3 -----------------\n# \u5c06a\u7684\u5185\u5bb9\u79fb\u5165\uff08\u89e3\u5305\uff09\u5230\u65b0\u5217\u8868b\u4e2d\u3002\n<\/code><\/pre>\n

    *args \u7684\u7528\u6cd5<\/h3>\n

    * args \u548c ** args \u4e3b\u8981\u7528\u4e8e\u51fd\u6570\u5b9a\u4e49\uff0c\u4f60\u53ef\u4ee5\u5c06\u4e0d\u5b9a\u6570\u91cf\u7684\u53c2\u6570\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n

    \u8fd9\u91cc\u4e0d\u5b9a\u7684\u610f\u601d\u662f\uff1a \u9884\u5148\u5e76\u4e0d\u77e5\u9053\uff0c\u51fd\u6570\u4f7f\u7528\u8005\u4f1a\u4f20\u9012\u591a\u5c11\u4e2a\u53c2\u6570\u7ed9\u4f60\uff0c\u6240\u5728\u5728\u8fd9\u4e2a\u573a\u666f\u4e0b\u4f7f\u7528\u8fd9\u4e24\u4e2a\u5173\u952e\u5b57\u3002 * args \u662f\u7528\u6765\u53d1\u9001\u4e00\u4e2a \u975e\u952e\u503c \u7684\u53ef\u53d8\u6570\u91cf\u7684\u53c2\u6570\u5217\u8868\u7ed9\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n

    Case 1<\/h4>\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)\ntest_var_args('yasoob','python','eggs','test')\n<\/code><\/pre>\n

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

    first normal arg: yasoob\nanother arg through *argv: python\nanother arg through *argv: eggs\nanother arg through *argv: test\n<\/code><\/pre>\n

    Case 2 \u5b9a\u4e49\u4e00\u4e2a\u6253\u5370\u7684\u51fd\u6570\uff0c\u4f20\u5165\u4efb\u610f\u53c2\u6570\u5373\u53ef<\/h4>\n
    def print_func(*args):\n    print(type(args))\n    print(args)\nprint_func(1,2,'python\u5e0c\u671b\u793e',[])\n<\/code><\/pre>\n

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

    <class 'tuple'>\n(1, 2, 'python\u5e0c\u671b\u793e', [])\n<\/code><\/pre>\n

    Case 3 \u5728\u6253\u5370\u51fd\u6570\u7684\u53c2\u6570\u5904\uff0c\u65b0\u589e x \u548c y \u53d8\u91cf<\/h4>\n
    def print_func(x,y,*args):\n    print(type(x))\n    print(x)\n    print(y)\n    print(type(args))\n    print(args)\nprint_func(1,2,'python\u5e0c\u671b\u793e',[])\n<\/code><\/pre>\n

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

    <class 'int'>\n1\n2\n<class 'tuple'>\n('python\u5e0c\u671b\u793e', [])\n<\/code><\/pre>\n

    Case4 \u5c06 *args \u653e\u5728\u53c2\u6570\u6700\u524d\u9762<\/h4>\n
    def print_func(*args,x,y):\n    print(type(x))\n    print(x)\n    print(y)\n    print(type(args))\n    print(args)\n<\/code><\/pre>\n

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

    TypeError: print_func() missing 2 required keyword-only arguments: 'x' and 'y'\n<\/code><\/pre>\n

    \u53ef\u4ee5\u770b\u51fa\uff0c\u6253\u5370\u51fd\u6570 print_func<\/code>\u627e\u4e0d\u5230\u5173\u952e\u8bcd\u53c2\u6570 x<\/code> \u548c y<\/code><\/p>\n

    \u6240\u4ee5\u82e5 *args<\/code> \u4e0d\u662f\u5728\u6700\u540e\uff0c\u5219\u9700\u8981\u5728\u53c2\u6570\u4f20\u5165\u65f6\uff0c\u660e\u786e\u5b9a\u4e49 *args<\/code>\u540e\u9762\u7684\u53d8\u91cf\u53c2\u6570\u540d\uff0c\u5982\u4e0b\uff1a<\/p>\n

    # \u6539\u6b63\u7684\u4ee3\u7801<\/span>\ndef<\/span> print_func<\/span>(<\/span>*<\/span>args,<\/span>x,<\/span>y)<\/span>:<\/span>\n    print<\/span>(<\/span>type<\/span>(<\/span>x)<\/span>)<\/span>\n    print<\/span>(<\/span>x)<\/span>\n    print<\/span>(<\/span>y)<\/span>\n    print<\/span>(<\/span>type<\/span>(<\/span>args)<\/span>)<\/span>\n    print<\/span>(<\/span>args)<\/span>\nprint_func(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>'python\u5e0c\u671b\u793e'<\/span>,<\/span>[<\/span>]<\/span>,<\/span>x=<\/span>'x'<\/span>,<\/span>y=<\/span>'y'<\/span>)<\/span>\n<\/code><\/pre>\n

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

    <<\/span>class<\/span> 'str'<\/span>><\/span>\nx\ny\n<<\/span>class<\/span> 'tuple'<\/span>><\/span>\n(<\/span>1<\/span>,<\/span> 2<\/span>,<\/span> 'python\u5e0c\u671b\u793e'<\/span>,<\/span> [<\/span>]<\/span>)<\/span>\n<\/code><\/pre>\n

    Case 5 \u5c06*args<\/code> \u653e\u5728\u4e2d\u95f4<\/h4>\n

    \u8fdb\u4e00\u6b65\u6ef4\u6211\u4eec\u53ef\u4ee5\u8003\u8651 \u5c06*args<\/code> \u653e\u5728\u4e2d\u95f4\uff0c\u7ed3\u679c\u5982\u4f55\u3002\u5c31\u8ba9\u5927\u5bb6\u81ea\u5df1\u6765\u52a8\u624b\u770b\u770b\u5427\uff1f<\/p>\n

    ** kwargs \u7684\u7528\u6cd5<\/h3>\n

    **kwargs<\/code>\u5141\u8bb8\u4f60\u5c06\u4e0d\u5b9a\u957f\u5ea6\u7684 \u3010\u952e\u503c\u5bf9 key-value \u3011\uff0c\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\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528**kwargs<\/code><\/p>\n

    Case 1<\/h4>\n
    def print_func(**kwargs):\n    print(type(kwargs))\n    print(kwargs)\nprint_func(1, 2, 'python\u5e0c\u671b\u793e', [])\n<\/code><\/pre>\n

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

    TypeError: print_func() takes 0 positional arguments but 4 were given\n<\/code><\/pre>\n

    \u76f4\u63a5\u62a5\u9519\u4e86\uff0c\u5927\u81f4\u610f\u601d\u662f\u9700\u8981\u7ed9\u51fa\u56db\u4e2a\u53c2\u6570\uff01 \u6539\u6b63\uff1a<\/p>\n

    def print_func(**kwargs):\n    print(type(kwargs))\n    print(kwargs)\n\nprint_func(a=1, b=2, c='\u5475\u5475\u54d2', d=[])\n<\/code><\/pre>\n

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

    <class 'dict'>\n{'a': 1, 'b': 2, 'c': '\u5475\u5475\u54d2', 'd': []}\n<\/code><\/pre>\n

    \u603b\u7ed3<\/strong>\uff1a \u5165\u53c2\u65f6\uff0c\u591a\u6dfb\u52a0\u4e86\u5173\u952e\u8bcd\u3002\u53ef\u4ee5\u770b\u5230\u6253\u5370\u65f6\uff0ckwargs\u672c\u8eab\u662f\u5b57\u5178\u7c7b\u578b\uff0c\u5c06\u4f20\u5165\u7684\u53c2\u6570\u4ee5\u5b57\u5178\u5f62\u5f0f\u5448\u73b0\u51fa\u6765\u3002<\/p>\n

    Case 2<\/h4>\n
    def<\/span> greet_me<\/span>(<\/span>**<\/span>kwargs)<\/span>:<\/span>\n    for<\/span> key,<\/span> value in<\/span> kwargs.<\/span>items(<\/span>)<\/span>:<\/span>\n        print<\/span>(<\/span>\"{0} == {1}\"<\/span>.<\/span>format<\/span>(<\/span>key,<\/span> value)<\/span>)<\/span>\ngreet_me(<\/span>name=<\/span>\"yasoob\"<\/span>)<\/span>\n<\/code><\/pre>\n

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

    name == yasoob\n<\/code><\/pre>\n

    \u73b0\u5728\u4f60\u53ef\u4ee5\u770b\u51fa\u6211\u4eec\u600e\u6837\u5728\u4e00\u4e2a\u51fd\u6570\u91cc\uff0c\u5904\u7406\u4e86\u4e00\u4e2a\u3010\u952e\u503c\u5bf9 key-value \u3011\u53c2\u6570\u4e86<\/p>\n

    \u7ec4\u5408\u4f7f\u7528 args<\/code>,*args<\/code> \u548c **kwargs<\/code> \u6765\u8c03\u7528\u51fd\u6570<\/h3>\n

    Case 1<\/h4>\n

    arg,*args,**kwargs<\/code> ,\u4e09\u8005\u662f\u53ef\u4ee5\u7ec4\u5408\u4f7f\u7528\u7684\uff0c\u4f46\u662f\u7ec4\u5408\u4f7f\u7528\u9700\u8981\u9075\u5faa\u4e00\u5b9a\u7684\u8bed\u6cd5\u89c4\u5219\uff0c\u5373\u987a\u5e8f\u4e3a\u738b\u3002<\/p>\n

    \u9700\u8981\u6309\u7167\uff1a<\/p>\n

    arg,*args,**kwargs<\/code> \u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u7684\u987a\u5e8f\u3002<\/p>\n

    def print_func(x, *args, **kwargs):\n    print(x)\n    print(args)\n    print(kwargs)\n\nprint_func(1, 2, 3, 4, y=1, a=2, b=3, c=4)\n<\/code><\/pre>\n

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

    1\n(2, 3, 4)\n{'y': 1, 'a': 2, 'b': 3, 'c': 4}\n<\/code><\/pre>\n

    Case 2<\/h4>\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<\/p>\n

    def test_args_kwargs(arg1,arg2,arg3):\n    print(\"arg1:\",arg1)\n    print(\"arg2:\",arg2)\n    print(\"arg3:\",arg3)\n<\/code><\/pre>\n

    \u4f60\u53ef\u4ee5\u4f7f\u7528 *args<\/code> \u548c **kwargs<\/code> \u6765\u7ed9\u8fd9\u4e2a\u5c0f\u51fd\u6570\u4f20\u9012\u53c2\u6570\u3002 \u4e0b\u9762\u662f\u600e\u6837\u505a\u5f97\uff1a<\/p>\n

    # \u9996\u5148\u4f60\u53ef\u4ee5\u4f7f\u7528 *args\nargs = (\"two\",3,5)\ntest_args_kwargs(*args)\n\n#-------- \u5f97\u5230\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a----------------------\n# arg1: two\n# arg2: 3\n# arg3: 5\n# ---------------------------------------------------\n<\/code><\/pre>\n
    #  \u73b0\u5728\u4f7f\u7528 **kwargs\nkwargs = {\"arg3\": 3,\"arg2\":\"two\",\"arg1\":5}\ntest_args_kwargs(**kwargs)\n\n#-------- \u5f97\u5230\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a----------------------\n# arg1: 5\n# arg2: two\n# arg3: 3\n# ---------------------------------------------------\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"python\u4e2dargs\u7684\u7528\u6cd5_args\u548ckwargs\u7684\u533a\u522b[\u901a\u4fd7\u6613\u61c2]*args\u548c**kwargs\u7684\u7528\u6cd5\u731b\u6ef4\u6253\u5f00\u535a\u5ba2\uff0c\u53d1\u73b0\u5b9e\u5728\u6709\u6bb5\u65f6\u95f4\u6ca1\u66f4\u65b0\u4e86","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\/8903"}],"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=8903"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8903\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}