{"id":15302,"date":"2023-10-27T14:51:16","date_gmt":"2023-10-27T06:51:16","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=15302"},"modified":"2023-10-30T10:25:19","modified_gmt":"2023-10-30T02:25:19","slug":"python-9","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2023\/10\/27\/python-9\/","title":{"rendered":"Python\u4fee\u9970\u7b26"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Python\u4fee\u9970\u7b26\u4e5f\u79f0\u4e3a\u88c5\u9970\u5668\uff0c\u5b83\u5141\u8bb8\u4f60\u7528\u4e00\u4e2a\u989d\u5916\u7684\u51fd\u6570\u6765\u589e\u5f3a\u5df2\u6709\u7684\u51fd\u6570\u800c\u4e0d\u9700\u8981\u4fee\u6539\u5df2\u6709\u51fd\u6570\u7684\u4ee3\u7801<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4fee\u9970\u7b26\u4e0d\u662f\u4e00\u5b9a\u8981\u4f7f\u7528\u7684\uff0c\u4e0d\u4f7f\u7528\u4e5f\u5b8c\u5168\u53ef\u4ee5\uff0c\u53ea\u662f\u4f7f\u7528\u4e86\u4fee\u9970\u7b26\u53ef\u4ee5\u6709\u5982\u4e0b\u597d\u5904\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4ee3\u7801\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\uff0c\u66f4\u52a0\u4f18\u96c5<\/li>\n\n\n\n<li>\u5c06\u5b9e\u73b0\u7279\u5b9a\u7684\u529f\u80fd\u4ee3\u7801\u5c01\u88c5\u6210\u88c5\u9970\u5668\uff0c\u63d0\u9ad8\u4ee3\u7801\u590d\u7528\u7387\uff0c\u589e\u5f3a\u4ee3\u7801\u53ef\u8bfb\u6027<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4fee\u9970\u7b26\u6709\u4ee5\u4e0b\u51e0\u4e2a\u7279\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4fee\u9970\u7b26\u662f\u4e00\u4e2a\u51fd\u6570\uff1a\u4fee\u9970\u7b26\u5b9e\u9645\u5c31\u662f\u4e00\u4e2a\u7ba1\u7406\u5df2\u6709\u51fd\u6570\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e2a\u88ab\u7ba1\u7406\u7684\u5df2\u6709\u51fd\u6570\u79f0\u4e3a\u88ab\u4fee\u9970\u51fd\u6570<\/li>\n\n\n\n<li>\u4fee\u9970\u7b26\u53d6\u88ab\u4fee\u9970\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff1a\u4fee\u9970\u7b26\u9700\u8981\u63a5\u6536\u88ab\u4fee\u9970\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u56e0\u6b64\uff0c\u53ea\u9700\u8981\u5c06\u88ab\u4fee\u9970\u51fd\u6570\u4f5c\u4e3a\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\u4f20\u5165\u4fee\u9970\u7b26\u5373\u53ef<\/li>\n\n\n\n<li>\u4fee\u9970\u7b26\u8fd4\u56de\u4e00\u4e2a\u65b0\u51fd\u6570\uff1a\u4fee\u9970\u7b26\u8fd4\u56de\u4e00\u4e2a\u65b0\u51fd\u6570\u4f5c\u4e3a\u5176\u8fd4\u56de\u503c\uff0c\u4e0d\u8fc7\u8fd4\u56de\u7684\u51fd\u6570\u9700\u8981\u8c03\u7528\u88ab\u4fee\u9970\u7684\u51fd\u6570<\/li>\n\n\n\n<li>\u4fee\u9970\u7b26\u7ef4\u62a4\u88ab\u4fee\u9970\u51fd\u6570\u7684\u7b7e\u540d\uff1a\u4fee\u9970\u7b26\u9700\u8981\u786e\u4fdd\u8fd4\u56de\u7684\u51fd\u6570\u4e0e\u88ab\u4fee\u9970\u7684\u51fd\u6570\u5177\u6709\u540c\u6837\u7684\u53c2\u6570(\u53c2\u6570\u4e2a\u6570\u548c\u7c7b\u578b)\uff0c\u51fd\u6570\u53c2\u6570\u7684\u4e2a\u6570\u548c\u7c7b\u578b\u4e00\u822c\u79f0\u4e3a\u7b7e\u540d<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4fee\u9970\u7b26\u4ee5\u7b26\u53f7&#8217;@&#8217;\u5f00\u59cb\uff0c\u540e\u9762\u7684\u51fd\u6570\u5c31\u662f\u88c5\u9970\u5668\uff0c\u88c5\u9970\u5668\u653e\u5728\u51fd\u6570\u7684\u4e0a\u9762\uff0c\u4e0e\u8fd9\u4e2a\u51fd\u6570\u7ed1\u5b9a\uff0c\u5728\u6211\u4eec\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4e0d\u662f\u5148\u6267\u884c\u8fd9\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u88c5\u9970\u5668\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@decorator\ndef func():\n    pass<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u4e0b\u9762\u4f8b\u5b50\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u4e0d\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668\uff0c\u7136\u540e\u6253\u5370\u53c2\u6570\u548c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"326\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/1-12.png\" alt=\"\" class=\"wp-image-15311\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/1-12.png 696w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/1-12-300x141.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/1-12-600x281.png 600w\" sizes=\"auto, (max-width: 696px) 100vw, 696px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u56fe\u4e2d\uff0c\u4fee\u9970\u7b26hello\u5c31\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u88ab\u4fee\u9970\u51fd\u6570\u4e3aworld\uff0c\u56e0\u6b64\u8c03\u7528\u51fd\u6570world\u7684\u65f6\u5019\uff0c\u5b9e\u9645\u662f\u5c06world\u5bf9\u8c61\u4f20\u9012\u5230hello\u51fd\u6570\u4e2d\uff0c\u4f5c\u4e3ahello\u51fd\u6570\u7684\u53c2\u6570\uff0chello\u51fd\u6570\u4e2d\u8fd4\u56dewrapper\u51fd\u6570\uff0cwrapper\u6700\u540e\u8fd4\u56de\u51fd\u6570func\uff0c\u8fd9\u4e2afunc\u5c31\u662f\u4f20\u9012\u8fc7\u6765\u7684world\u51fd\u6570\u5bf9\u8c61\uff0c\u56e0\u6b64\uff0c<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u4e00\u4e2a\u4fee\u9970\u7b26\u5e94\u7528\u5230\u4e00\u4e2a\u73b0\u6709\u51fd\u6570\u4e0a\u7684\u65f6\u5019\uff0c\u5bf9\u8fd9\u4e2a\u73b0\u6709\u51fd\u6570\u7684\u8c03\u7528\u90fd\u4f1a\u66ff\u6362\u4e3a\u8c03\u7528\u4fee\u9970\u7b26\u8fd4\u56de\u7684\u51fd\u6570<\/mark>\uff0c\u56e0\u6b64\uff0c\u4e0a\u9762\u7b2c14\u884c\u8c03\u7528world\u51fd\u6570\u540e\uff0c\u5b9e\u9645\u662f\u7531hello\u4fee\u9970\u7b26\u8fd4\u56de\u7684\u51fd\u6570wrapper\u6765\u8c03\u7528\u7684<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4fee\u9970\u7b26\u8981\u6c42\u8fd4\u56de\u7684\u51fd\u6570wrapper\u548c\u88ab\u4fee\u9970\u7684\u51fd\u6570func\u4e2d\u51fd\u6570\u7b7e\u540d\u4e00\u6837\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u4e0a\u9762\u4f7f\u7528*args\u548c**kwargs\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4e00\u4e2a\u4fee\u9970\u7b26\u4fee\u9970\u591a\u4e2a\u51fd\u6570\uff0c\u4f46\u662f\u6bcf\u4e2a\u51fd\u6570\u7684\u4f20\u53c2\u4e2a\u6570\u4e0d\u4e00\u6837\uff0c\u6709\u7684\u51fd\u6570\u6ca1\u6709\u53c2\u6570\uff0c\u6709\u7684\u67092\u4e2a\u53c2\u6570\uff0c\u6709\u7684\u67093\u4e2a\u53c2\u6570\uff0c\u5982\u679c\u4f7f\u7528\u540c\u4e00\u4e2a\u4fee\u9970\u7b26\uff0c\u90a3\u4e48\u6700\u597d\u7684\u529e\u6cd5\u5c31\u662f\u8ba9\u4fee\u9970\u7b26\u7684\u53c2\u6570\u4fdd\u6301&#8221;\u901a\u7528&#8221;\uff0c\u4f7f\u7528*args\u548c**kwargs\u5373\u53ef\u63a5\u6536\u4efb\u610f\u7c7b\u578b\u548c\u6570\u91cf\u7684\u53c2\u6570\uff0c\u6700\u540e\u8fd0\u884c\u6b64\u4ee3\u7801\uff0c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"693\" height=\"69\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/2-10.png\" alt=\"\" class=\"wp-image-15314\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/2-10.png 693w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/2-10-300x30.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/2-10-600x60.png 600w\" sizes=\"auto, (max-width: 693px) 100vw, 693px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u4e0b\u9762\u662f\u4e00\u4e2a\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668\uff0c\u9700\u8981\u5728\u539f\u6709\u57fa\u7840\u4e0a\u5d4c\u5957\u4e00\u5c42\u51fd\u6570\u7528\u6765\u63a5\u6536\u53c2\u6570\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"695\" height=\"519\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/4-6.png\" alt=\"\" class=\"wp-image-15317\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/4-6.png 695w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/4-6-300x224.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/4-6-600x448.png 600w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c6\u884c\uff1a\u5b9a\u4e49\u5916\u5c42\u51fd\u6570\u7528\u6765\u63a5\u6536\u4fee\u9970\u7b26\u53c2\u6570\uff0c\u6b64\u51fd\u6570\u4e2d\u8c03\u7528return\u6765\u8fd4\u56dehello\u51fd\u6570<\/li>\n\n\n\n<li>\u7b2c7\u884c\uff1ahello\u51fd\u6570\u4e2d\u7684\u53c2\u6570\u5c31\u662f\u4f20\u9012\u8fc7\u6765\u7684world\u51fd\u6570\u5bf9\u8c61\uff0c\u5728hello\u4e2d\u8c03\u7528return\u8fd4\u56dewrapper<\/li>\n\n\n\n<li>\u7b2c8\u884c\uff1awrapper\u4e2d\u5bf9\u53c2\u6570params\u8fdb\u884c\u5224\u65ad\u7136\u540e\u6267\u884c\u76f8\u5173\u5185\u5bb9\uff0c\u7136\u540e\u8c03\u7528func\uff0c\u5b9e\u9645\u6267\u884c\u7684world\u51fd\u6570<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"695\" height=\"76\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/5-7.png\" alt=\"\" class=\"wp-image-15319\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/5-7.png 695w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/5-7-300x33.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/5-7-600x66.png 600w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u770b\u51fa\uff0c\u4e0d\u7528\u4fee\u6539\u73b0\u5728world()\u51fd\u6570\u4ee3\u7801\uff0c\u5373\u53ef\u5b9e\u73b0\u5728\u51fd\u6570\u524d\u540e\u589e\u52a0\u5185\u5bb9\u6216\u8005\u5176\u4f59\u7684\u4e00\u4e9b\u64cd\u4f5c\uff0c\u589e\u5f3a\u4e86\u51fd\u6570\u7684\u529f\u80fd<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u4e0a\u9762\u7684\u662f\u57fa\u4e8e\u51fd\u6570\u7684\u88c5\u9970\u5668\uff0c\u9664\u6b64\u4e4b\u5916\u8fd8\u6709\u57fa\u4e8e\u7c7b\u7684\u88c5\u9970\u5668\uff0c\u5b9e\u73b0\u7c7b\u88c5\u9970\u5668\uff0c\u7c7b\u5fc5\u987b\u5b9e\u73b0__call__\u548c__init__\u4e24\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c__init__\u7528\u4e8e\u63a5\u6536\u88ab\u4fee\u9970\u51fd\u6570\uff0c__call__\u7528\u4e8e\u5b9e\u73b0\u88c5\u9970\u903b\u8f91<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662f\u4e00\u4e2a\u4e0d\u5e26\u53c2\u6570\u7684\u7c7b\u88c5\u9970\u5668\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"694\" height=\"385\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/6-8.png\" alt=\"\" class=\"wp-image-15322\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/6-8.png 694w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/6-8-300x166.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/6-8-600x333.png 600w\" sizes=\"auto, (max-width: 694px) 100vw, 694px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"695\" height=\"89\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/7-8.png\" alt=\"\" class=\"wp-image-15323\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/7-8.png 695w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/7-8-300x38.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/7-8-600x77.png 600w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">4\u3001\u5e26\u53c2\u6570\u7684\u7c7b\u88c5\u9970\u5668<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e26\u53c2\u6570\u548c\u4e0d\u5e26\u53c2\u6570\u7684\u7c7b\u88c5\u9970\u5668\u6709\u5f88\u5927\u7684\u4e0d\u540c\uff0c__init__\u4e0d\u518d\u63a5\u6536\u88ab\u88c5\u9970\u51fd\u6570\uff0c\u800c\u662f\u63a5\u6536\u4f20\u5165\u53c2\u6570\uff0c__call__\u63a5\u6536\u88ab\u88c5\u9970\u51fd\u6570\uff0c\u5b9e\u73b0\u88c5\u9970\u903b\u8f91<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662f\u4e00\u4e2a\u5e26\u53c2\u6570\u7684\u7c7b\u88c5\u9970\u5668\u7684\u4f8b\u5b50\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"444\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/8-7.png\" alt=\"\" class=\"wp-image-15325\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/8-7.png 696w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/8-7-300x191.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/8-7-600x383.png 600w\" sizes=\"auto, (max-width: 696px) 100vw, 696px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"693\" height=\"86\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/9-5.png\" alt=\"\" class=\"wp-image-15327\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/9-5.png 693w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/9-5-300x37.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/10\/9-5-600x74.png 600w\" sizes=\"auto, (max-width: 693px) 100vw, 693px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4fee\u9970\u7b26\u4e5f\u79f0\u4e3a\u88c5\u9970\u5668\uff0c\u5b83\u5141\u8bb8\u4f60\u7528\u4e00\u4e2a\u989d\u5916\u7684\u51fd\u6570\u6765\u589e\u5f3a\u5df2\u6709\u7684\u51fd\u6570\u800c\u4e0d\u9700\u8981\u4fee\u6539\u5df2\u6709\u51fd\u6570\u7684\u4ee3\u7801 \u4fee\u9970\u7b26\u4e0d\u662f [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":15303,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,29],"tags":[],"class_list":["post-15302","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-yunwei"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/15302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=15302"}],"version-history":[{"count":16,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/15302\/revisions"}],"predecessor-version":[{"id":15328,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/15302\/revisions\/15328"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/15303"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=15302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=15302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=15302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}