{"id":222005,"date":"2021-07-20T10:17:42","date_gmt":"2021-07-20T02:17:42","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=222005"},"modified":"2021-07-07T11:18:26","modified_gmt":"2021-07-07T03:18:26","slug":"python-function-parameters","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-function-parameters.html","title":{"rendered":"\u5b9e\u6218\u8bb2\u89e3Python\u51fd\u6570\u53c2\u6570"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4eca\u5929\u7ed9\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b\u81ea\u5df1\u6574\u7406\u7684\u4e00\u7bc7 Python \u53c2\u6570\u7684\u5185\u5bb9\uff0c\u5185\u5bb9\u975e\u5e38\u7684\u5e72\uff0c\u5168\u6587\u901a\u8fc7\u6848\u4f8b\u7684\u5f62\u5f0f\u6765\u7406\u89e3\u77e5\u8bc6\u70b9\uff0c\u81ea\u8ba4\u4e3a\u6bd4\u7f51\u4e0a 80% \u7684\u6587\u7ae0\u8bb2\u7684\u90fd\u8981\u660e\u767d\uff0c\u5982\u679c\u4f60\u662f\u5165\u95e8\u4e0d\u4e45\u7684 python \u65b0\u624b\uff0c\u76f8\u4fe1\u672c\u7bc7\u6587\u7ae0\u5e94\u8be5\u5bf9\u4f60\u4f1a\u6709\u4e0d\u5c0f\u7684\u5e2e\u52a9\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1. \u53c2\u6570\u5206\u7c7b<\/strong><\/div>\n

\u51fd\u6570\uff0c\u5728\u5b9a\u4e49\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u6709\u53c2\u6570\u7684\uff0c\u4e5f\u53ef\u4ee5\u6ca1\u6709\u53c2\u6570\u3002<\/p>\n

\u4ece\u51fd\u6570\u5b9a\u4e49\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u53c2\u6570\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\uff1a<\/p>\n

    \u5fc5\u9009\u53c2\u6570\uff1a\u8c03\u7528\u51fd\u6570\u65f6\u5fc5\u987b\u8981\u6307\u5b9a\u7684\u53c2\u6570\uff0c\u5728\u5b9a\u4e49\u65f6\u6ca1\u6709\u7b49\u53f7<\/ol>\n
      \u53ef\u9009\u53c2\u6570\uff1a\u4e5f\u53eb\u9ed8\u8ba4\u53c2\u6570\uff0c\u8c03\u7528\u51fd\u6570\u65f6\u53ef\u4ee5\u6307\u5b9a\u4e5f\u53ef\u4ee5\u4e0d\u6307\u5b9a\uff0c\u4e0d\u6307\u5b9a\u5c31\u9ed8\u8ba4\u7684\u53c2\u6570\u503c\u6765\u3002<\/ol>\n

      \u4f8b\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0ca \u548c b \u5c5e\u4e8e\u5fc5\u9009\u53c2\u6570\uff0c c \u548c d \u5c5e\u4e8e\u53ef\u9009\u53c2\u6570<\/p>\n

      \r\ndef func(a,b,c=0, d=1): \r\n    pass \r\n<\/pre>\n

      \u4ece\u51fd\u6570\u8c03\u7528\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u53c2\u6570\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\uff1a<\/p>\n

      \u5173\u952e\u5b57\u53c2\u6570\uff1a\u8c03\u7528\u65f6\uff0c\u4f7f\u7528 key=value \u5f62\u5f0f\u4f20\u53c2\u7684\uff0c\u8fd9\u6837\u4f20\u9012\u53c2\u6570\u5c31\u53ef\u4ee5\u4e0d\u6309\u5b9a\u4e49\u987a\u5e8f\u6765\u3002<\/p>\n

      \u4f4d\u7f6e\u53c2\u6570\uff1a\u8c03\u7528\u65f6\uff0c\u4e0d\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u7684 key-value \u5f62\u5f0f\u4f20\u53c2\uff0c\u8fd9\u6837\u4f20\u53c2\u8981\u6ce8\u610f\u6309\u7167\u51fd\u6570\u5b9a\u4e49\u65f6\u53c2\u6570\u7684\u987a\u5e8f\u6765\u3002<\/p>\n

      \r\ndef func(a,b,c=0, d=1): \r\n    pass \r\n \r\n  # \u5173\u952e\u5b57\u53c2\u6570\u4f20\u53c2\u65b9\u6cd5 \r\nfunc(a=10, c=30, b=20, d=40) \r\n \r\n  # \u4f4d\u7f6e\u53c2\u6570\u4f20\u53c2\u65b9\u6cd5 \r\nfunc(10, 20, 30, 40) \r\n<\/pre>\n

      \u6700\u540e\u8fd8\u6709\u4e00\u79cd\u975e\u5e38\u7279\u6b8a\u7684\u53c2\u6570\uff0c\u53eb\u505a\u53ef\u53d8\u53c2\u6570\u3002<\/p>\n

      \u610f\u601d\u662f\u53c2\u6570\u4e2a\u6570\u53ef\u53d8\uff0c\u53ef\u4ee5\u662f 0 \u4e2a\u6216\u8005\u4efb\u610f\u4e2a\uff0c\u4f46\u662f\u4f20\u53c2\u65f6\u4e0d\u80fd\u6307\u5b9a\u53c2\u6570\u540d\uff0c\u901a\u5e38\u4f7f\u7528 *args \u548c **kw \u6765\u8868\u793a\uff1a<\/p>\n

        *args\uff1a\u63a5\u6536\u5230\u7684\u6240\u6709\u6309\u7167\u4f4d\u7f6e\u53c2\u6570\u65b9\u5f0f\u4f20\u9012\u8fdb\u6765\u7684\u53c2\u6570\uff0c\u662f\u4e00\u4e2a\u5143\u7ec4\u7c7b\u578b<\/ol>\n
          **kw \uff1a\u63a5\u6536\u5230\u7684\u6240\u6709\u6309\u7167\u5173\u952e\u5b57\u53c2\u6570\u65b9\u5f0f\u4f20\u9012\u8fdb\u6765\u7684\u53c2\u6570\uff0c\u662f\u4e00\u4e2a\u5b57\u5178\u7c7b\u578b<\/ol>\n
          \r\ndef func(*args, **kw): \r\n    print(args) \r\n    print(kw) \r\n \r\nfunc(10, 20, c=20, d=40) \r\n<\/pre>\n

          \u8f93\u51fa\u5982\u4e0b<\/p>\n

          \r\n(10, 20) \r\n{'c': 20, 'd': 40} \r\n<\/pre>\n
          2. \u5341\u4e00\u4e2a\u6848\u4f8b<\/strong><\/div>\n

          \u6848\u4f8b\u4e00\uff1a\u5728\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c a \u662f\u5fc5\u9009\u53c2\u6570\uff0c\u662f\u5fc5\u987b\u8981\u6307\u5b9a\u7684<\/p>\n

          \r\n>>> def demo_func(a): \r\n...     print(a) \r\n...  \r\n>>> demo_func(10)  \r\n10 \r\n>>> demo_func()  # \u4e0d\u6307\u5b9a\u4f1a\u62a5\u9519 \r\nTraceback (most recent call last): \r\n  File \"\", line 1, in  \r\nTypeError: demo_func() missing 1 required positional argument: 'a' \r\n<\/module><\/stdin><\/pre>\n

          \u6848\u4f8b\u4e8c\uff1a\u5728\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0cb \u662f\u53ef\u9009\u53c2\u6570(\u9ed8\u8ba4\u53c2\u6570)\uff0c\u53ef\u4ee5\u6307\u5b9a\u4e5f\u53ef\u4ee5\u4e0d\u6307\u5b9a\uff0c\u4e0d\u6307\u5b9a\u7684\u8bdd\uff0c\u9ed8\u8ba4\u4e3a10<\/p>\n

          \r\n>>> def demo_func(b=10): \r\n...     print(b) \r\n...  \r\n>>> demo_func(20) \r\n20 \r\n>>> demo_func() \r\n10 \r\n<\/pre>\n

          \u6848\u4f8b\u4e09\uff1a\u5728\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c name \u548c age \u90fd\u662f\u5fc5\u9009\u53c2\u6570\uff0c\u5728\u8c03\u7528\u6307\u5b9a\u53c2\u6570\u65f6\uff0c\u5982\u679c\u4e0d\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u65b9\u5f0f\u4f20\u53c2\uff0c\u9700\u8981\u6ce8\u610f\u987a\u5e8f<\/p>\n

          \r\n>>> def print_profile(name, age): \r\n...     return f\"\u6211\u7684\u540d\u5b57\u53eb{name}\uff0c\u4eca\u5e74{age}\u5c81\u4e86\" \r\n... \r\n>>> print_profile(\"iswbm\", 27) \r\n'\u6211\u7684\u540d\u5b57\u53ebiswbm\uff0c\u4eca\u5e7427\u5c81\u4e86' \r\n'\u6211\u7684\u540d\u5b57\u53ebiswbm\uff0c\u4eca\u5e7427\u5c81\u4e86'\r\n\r\n\u5982\u679c\u53c2\u6570\u592a\u591a\uff0c\u4f60\u4e0d\u60f3\u592a\u82b1\u7cbe\u529b\u53bb\u6ce8\u610f\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u65b9\u5f0f\u4f20\u53c2\uff0c\u5728\u6307\u5b9a\u53c2\u6570\u65f6\u9644\u4e0a\u53c2\u6570\u540d\uff0c\u6bd4\u5982\u8fd9\u6837\uff1a\r\n\r\n>>> print_profile(age=27, name=\"iswbm\") \r\n'\u6211\u7684\u540d\u5b57\u53ebiswbm\uff0c\u4eca\u5e7427\u5c81\u4e86' \r\n<\/pre>\n

          \u6848\u4f8b\u56db\uff1a\u5728\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0cargs \u53c2\u6570\u548c\u4e0a\u9762\u7684\u53c2\u6570\u540d\u4e0d\u592a\u4e00\u6837\uff0c\u5728\u5b83\u524d\u9762\u6709\u4e00\u4e2a *\uff0c\u8fd9\u5c31\u8868\u660e\u4e86\u5b83\u662f\u4e00\u4e2a\u53ef\u53d8\u53c2\u6570\uff0c\u53ef\u4ee5\u63a5\u6536\u4efb\u610f\u4e2a\u6570\u7684\u4e0d\u6307\u5b9a\u53c2\u6570\u540d\u7684\u53c2\u6570\u3002<\/p>\n

          \r\n>>> def demo_func(*args): \r\n...     print(args) \r\n...  \r\n>>>  \r\n>>> demo_func(10, 20, 30) \r\n(10, 20, 30) \r\n<\/pre>\n

          \u6848\u4f8b\u4e94\uff1a\u5728\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0ckw \u53c2\u6570\u548c\u4e0a\u9762\u7684 *args \u8fd8\u591a\u4e86\u4e00\u4e2a * \uff0c\u603b\u5171\u4e24\u4e2a ** \uff0c\u8fd9\u4e2a\u610f\u601d\u662f kw \u662f\u4e00\u4e2a\u53ef\u53d8\u5173\u952e\u5b57\u53c2\u6570\uff0c\u53ef\u4ee5\u63a5\u6536\u4efb\u610f\u4e2a\u6570\u7684\u5e26\u53c2\u6570\u540d\u7684\u53c2\u6570\u3002<\/p>\n

          \r\n>>> def demo_func(**kw): \r\n...     print(kw) \r\n...  \r\n>>> demo_func(a=10, b=20, c=30) \r\n{'a': 10, 'b': 20, 'c': 30} \r\n<\/pre>\n

          \u6848\u4f8b\u516d\uff1a\u5728\u5b9a\u4e49\u65f6\uff0c\u5fc5\u9009\u53c2\u6570\u4e00\u5b9a\u8981\u5728\u53ef\u9009\u53c2\u6570\u7684\u524d\u9762\uff0c\u4e0d\u7136\u8fd0\u884c\u65f6\u4f1a\u62a5\u9519<\/p>\n

          \r\n>>> def demo_func(a=1, b): \r\n...     print(a, b) \r\n...  \r\n  File \"\", line 1 \r\nSyntaxError: non-default argument follows default argument \r\n>>> \r\n>>> def demo_func(a, b=1): \r\n...     print(a, b) \r\n...  \r\n>>> \r\n<\/stdin><\/pre>\n

          \u6848\u4f8b\u4e03\uff1a\u5728\u5b9a\u4e49\u65f6\uff0c\u53ef\u53d8\u4f4d\u7f6e\u53c2\u6570\u4e00\u5b9a\u8981\u5728\u53ef\u53d8\u5173\u952e\u5b57\u53c2\u6570\u524d\u9762\uff0c\u4e0d\u7136\u8fd0\u884c\u65f6\u4e5f\u4f1a\u62a5\u9519<\/p>\n

          \r\n>>> def demo_func(**kw, *args): \r\n  File \"\", line 1 \r\n    def demo_func(**kw, *args): \r\n                        ^ \r\nSyntaxError: invalid syntax \r\n>>>  \r\n>>> def demo_func(*args, **kw): \r\n...     print(args, kw) \r\n...  \r\n>>>  \r\n<\/stdin><\/pre>\n

          \u6848\u4f8b\u516b\uff1a\u53ef\u53d8\u4f4d\u7f6e\u53c2\u6570\u53ef\u4ee5\u653e\u5728\u5fc5\u9009\u53c2\u6570\u524d\u9762\uff0c\u4f46\u662f\u5728\u8c03\u7528\u65f6\uff0c\u5fc5\u9009\u53c2\u6570\u5fc5\u987b\u8981\u6307\u5b9a\u53c2\u6570\u540d\u6765\u4f20\u5165\uff0c\u5426\u5219\u4f1a\u62a5\u9519<\/p>\n

          \r\n>>> def demo_func(*args, b): \r\n...     print(args) \r\n...     print(b) \r\n...  \r\n>>> demo_func(1, 2, 100) \r\nTraceback (most recent call last): \r\n  File \"\", line 1, in  \r\nTypeError: demo_func() missing 1 required keyword-only argument: 'b' \r\n>>>  \r\n>>> demo_func(1, 2, b=100) \r\n(1, 2) \r\n100 \r\n<\/module><\/stdin><\/pre>\n

          \u6848\u4f8b\u4e5d\uff1a\u53ef\u53d8\u5173\u952e\u5b57\u53c2\u6570\u5219\u4e0d\u4e00\u6837\uff0c\u53ef\u53d8\u5173\u952e\u5b57\u53c2\u6570\u4e00\u5b9a\u5f97\u653e\u5728\u6700\u540e\uff0c\u4e0b\u9762\u4e09\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4e0d\u7ba1\u5173\u952e\u5b57\u53c2\u6570\u540e\u9762\u63a5\u4f4d\u7f6e\u53c2\u6570\uff0c\u8fd8\u662f\u9ed8\u8ba4\u53c2\u6570\uff0c\u8fd8\u662f\u53ef\u53d8\u53c2\u6570\uff0c\u90fd\u4f1a\u62a5\u9519\u3002<\/p>\n

          \r\n>>> def demo_func(**kw, a): \r\n  File \"\", line 1 \r\n    def demo_func(**kw, a): \r\n                        ^ \r\nSyntaxError: invalid syntax \r\n>>>  \r\n>>> def demo_func(**kw, a=1): \r\n  File \"<\/stdin>\", line 1 \r\n    def demo_func(**kw, a=1): \r\n                        ^ \r\nSyntaxError: invalid syntax \r\n>>>  \r\n>>> def demo_func(**kw, *args): \r\n  File \"<\/stdin>\", line 1 \r\n    def demo_func(**kw, *args): \r\n                        ^ \r\nSyntaxError: invalid syntax \r\n<\/stdin><\/pre>\n

          \u6848\u4f8b\u5341\uff1a\u5c06\u4e0a\u9762\u7684\u77e5\u8bc6\u70b9\u4e32\u8d77\u6765\uff0c\u56db\u79cd\u53c2\u6570\u7c7b\u578b\u53ef\u4ee5\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\u51fa\u73b0\uff0c\u4f46\u4e00\u5b9a\u8981\u6ce8\u610f\u987a\u5e8f<\/p>\n

          \r\ndef demo_func(arg1, arg2=10, *args, **kw): \r\n    print(\"arg1: \", arg1) \r\n    print(\"arg2: \", arg2) \r\n    print(\"args: \", args) \r\n    print(\"kw: \", kw) \r\n\u8bd5\u7740\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a\r\n\r\n>>> demo_func(1,12, 100, 200, d=1000, e=2000) \r\narg1:  1 \r\narg2:  12 \r\nargs:  (100, 200) \r\nkw:  {'d': 1000, 'e': 2000} \r\n<\/pre>\n

          \u6848\u4f8b\u5341\u4e00\uff1a\u4f7f\u7528\u5355\u72ec\u7684 *\uff0c\u5f53\u4f60\u5728\u7ed9\u540e\u9762\u7684\u4f4d\u7f6e\u53c2\u6570\u4f20\u9012\u65f6\uff0c\u5bf9\u4f60\u4f20\u53c2\u7684\u65b9\u5f0f\u6709\u4e25\u683c\u8981\u6c42\uff0c\u4f60\u5728\u4f20\u53c2\u65f6\u5fc5\u987b\u8981\u4ee5\u5173\u952e\u5b57\u53c2\u6570\u7684\u65b9\u5f0f\u4f20\u53c2\u6570\uff0c\u8981\u5199\u53c2\u6570\u540d\uff0c\u4e0d\u7136\u4f1a\u62a5\u9519\u3002<\/p>\n

          \r\n>>> def demo_func(a, b, *, c): \r\n...     print(a) \r\n...     print(b) \r\n...     print(c) \r\n...  \r\n>>>  \r\n>>> demo_func(1, 2, 3) \r\nTraceback (most recent call last): \r\n  File \"\", line 1, in  \r\nTypeError: demo_func() takes 2 positional arguments but 3 were given \r\n>>>  \r\n>>> demo_func(1, 2, c=3) \r\n1 \r\n2 \r\n3 \r\n<\/module><\/stdin><\/pre>\n
          3. \u4f20\u53c2\u7684\u5751<\/strong><\/div>\n

          \u51fd\u6570\u53c2\u6570\u4f20\u9012\u7684\u662f\u5b9e\u9645\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u3002\u5982\u679c\u53c2\u6570\u662f\u5f15\u7528\u7c7b\u578b\u7684\u6570\u636e\u7c7b\u578b(\u5217\u8868\u3001\u5b57\u5178\u7b49)\uff0c\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u540e\uff0c\u5c31\u7b97\u6ca1\u6709\u628a\u4fee\u6539\u540e\u7684\u503c\u8fd4\u56de\u56de\u53bb\uff0c\u5916\u9762\u7684\u503c\u5176\u5b9e\u4e5f\u5df2\u7ecf\u53d1\u751f\u4e86\u53d8\u5316\u3002<\/p>\n

          \r\n>>> def add_item(item, source_list): \r\n...     source_list.append(item) \r\n... \r\n>>> alist = [0,1] \r\n>>> add_item(2, alist) \r\n>>> alist \r\n[0, 1, 2] \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

          \u51fd\u6570\uff0c\u5728\u5b9a\u4e49\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u6709\u53c2\u6570\u7684\uff0c\u4e5f\u53ef\u4ee5\u6ca1\u6709\u53c2\u6570\u3002 \u4ece\u51fd\u6570\u5b9a\u4e49\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u53c2\u6570\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\uff1a \u5fc5\u9009\u53c2\u6570\uff1a\u8c03\u7528\u51fd […]<\/p>\n","protected":false},"author":1329,"featured_media":222038,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-222005","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/222005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=222005"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/222005\/revisions"}],"predecessor-version":[{"id":222041,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/222005\/revisions\/222041"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/222038"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=222005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=222005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=222005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}