{"id":1874,"date":"2021-10-24T10:11:25","date_gmt":"2021-10-24T02:11:25","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=1874"},"modified":"2024-05-05T14:19:36","modified_gmt":"2024-05-05T06:19:36","slug":"python-%e5%87%bd%e6%95%b0%e8%a3%85%e9%a5%b0%e5%99%a8","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=1874","title":{"rendered":"Python @\u51fd\u6570\u88c5\u9970\u5668"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor:pointer\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.iyatt.com\/?p=1874\/#%E8%87%AA%E5%AE%9A%E4%B9%89%E8%A3%85%E9%A5%B0%E5%99%A8%E5%87%BD%E6%95%B0\" >\u81ea\u5b9a\u4e49\u88c5\u9970\u5668\u51fd\u6570<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.iyatt.com\/?p=1874\/#%E5%86%85%E7%BD%AE%E8%A3%85%E9%A5%B0%E5%99%A8%E5%87%BD%E6%95%B0\" >\u5185\u7f6e\u88c5\u9970\u5668\u51fd\u6570<\/a><\/li><\/ul><\/nav><\/div>\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%87%AA%E5%AE%9A%E4%B9%89%E8%A3%85%E9%A5%B0%E5%99%A8%E5%87%BD%E6%95%B0\"><\/span>\u81ea\u5b9a\u4e49\u88c5\u9970\u5668\u51fd\u6570<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def decorator(func):  # \u88c5\u9970\u5668\u51fd\u6570\n    def inner(num1, num2):  # \u5185\u90e8\u51fd\u6570\n        return func(num1, num2) ** 2  # \u5c06\u51fd\u6570\u7684\u6c42\u548c\u7ed3\u679c\u8ba1\u7b97\u5e73\u65b9\n    return inner  # \u8fd4\u56de\u5e73\u65b9\u540e\u7684\u7ed3\u679c\u4f5c\u4e3a\u51fd\u6570\u7684\u5b9e\u9645\u8fd4\u56de\u503c\n\n\n@decorator  # \u4f7f\u7528\u88c5\u9970\u5668\ndef fun(num1, num2):\n    sum = num1 + num2  # \u6c42\u548c\n    return sum  # \u6c42\u548c\u7ed3\u679c\u4f1a\u4f20\u7ed9\u88c5\u9970\u5668\n\n\nprint(fun(2, 4))  # \u8c03\u7528\u6d4b\u8bd5\u51fd\u6570<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"174\" height=\"70\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2021\/10\/2021-10-24-10-04-03-\u7684\u5c4f\u5e55\u622a\u56fe.png\" alt=\"\" class=\"wp-image-1875 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 174px; --smush-placeholder-aspect-ratio: 174\/70;\" \/><\/figure>\n\n\n\n<p>\u51fd\u6570 fun \u672c\u8eab\u53ea\u662f\u8ba1\u7b97\u4f20\u5165\u7684\u4e24\u4e2a\u53c2\u6570\u7684\u6c42\u548c\uff0c\u7136\u540e\u5229\u7528\u88c5\u9970\u5668\u6269\u5c55\u529f\u80fd\uff0c\u5bf9\u6c42\u548c\u540e\u7684\u6570\u503c\u518d\u5e73\u65b9\u3002\u6211\u5bf9\u88c5\u9970\u5668\u7684\u7406\u89e3\u5c31\u662f\u7528\u6765\u7ed9\u51fd\u6570\u6269\u5c55\u529f\u80fd\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%86%85%E7%BD%AE%E8%A3%85%E9%A5%B0%E5%99%A8%E5%87%BD%E6%95%B0\"><\/span>\u5185\u7f6e\u88c5\u9970\u5668\u51fd\u6570<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>1.\u9759\u6001\u65b9\u6cd5\uff1a@staticmethod<\/p>\n\n\n\n<p>\u5bf9\u7c7b\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u8fdb\u884c\u4fee\u9970\uff0c\u53ef\u5b9e\u73b0\u975e\u5b9e\u4f8b\u5316\u7684\u60c5\u51b5\u4e0b\u76f4\u63a5\u8c03\u7528\u7c7b\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class CC(object):\n    @staticmethod\n    def aa():\n        print('\u9759\u6001\u65b9\u6cd5')\n\n    def bb(self):\n        print('\u5b9e\u4f8b\u5316\u8c03\u7528')\n\n\nCC.aa()  # \u9759\u6001\u65b9\u6cd5\n\ncc = CC()  # \u8c03\u7528\u9700\u8981\u5148\u5b9e\u4f8b\u5316\ncc.bb()<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"121\" height=\"88\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2021\/10\/2021-10-24-10-40-58-\u7684\u5c4f\u5e55\u622a\u56fe.png\" alt=\"\" class=\"wp-image-1880 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 121px; --smush-placeholder-aspect-ratio: 121\/88;\" \/><\/figure>\n\n\n\n<p>2.\u4fee\u9970\u51fd\u6570\u65e0\u9700\u5b9e\u4f8b\u5316\uff0c\u4e0d\u7528 self \u53c2\u6570\uff1a@classmethod<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class C(object):\n    def aa(self):\n        print('\u7c7b\u65b9\u6cd5')\n\n    @classmethod\n    def bb(d):  # \u9700\u8981\u4e00\u4e2a\u8868\u793a\u81ea\u8eab\u7c7b\u7684\u53c2\u6570\n        print('\u65e0\u9700\u5b9e\u4f8b\u5316\u7684\u65b9\u6cd5')\n        d().aa()  # \u8c03\u7528\u81ea\u8eab\u7c7b\u4e2d\u7684\u5176\u4ed6\u65b9\u6cd5\n\n\nC.bb()<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"159\" height=\"89\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2021\/10\/2021-10-24-10-56-23-\u7684\u5c4f\u5e55\u622a\u56fe.png\" alt=\"\" class=\"wp-image-1881 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 159px; --smush-placeholder-aspect-ratio: 159\/89;\" \/><\/figure>\n\n\n\n<p>3.\u53ef\u8ba9\u65b9\u6cd5\u50cf\u5c5e\u6027\u4e00\u6837\u8bbf\u95ee\uff08\u53ea\u8bfb\uff09\uff1a@property<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class C(object):\n    def __init__(self):\n        self.__a = 9\n        self.__b = 10\n\n    @property\n    def a(self):\n        return self.__a\n\n    @property\n    def b(self):\n        return self.__b\n\n\nc = C()\nprint(c.a)  # \u65e0\u9700\u901a\u8fc7 c.a() \u52a0\u62ec\u53f7\u7684\u65b9\u5f0f\u8bbf\u95ee\nprint(c.b)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"131\" height=\"87\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2021\/10\/2021-10-24-11-07-49-\u7684\u5c4f\u5e55\u622a\u56fe.png\" alt=\"\" class=\"wp-image-1882 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 131px; --smush-placeholder-aspect-ratio: 131\/87;\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u81ea\u5b9a\u4e49\u88c5\u9970\u5668\u51fd\u6570 \u793a\u4f8b\uff1a \u51fd\u6570 fun \u672c\u8eab\u53ea\u662f\u8ba1\u7b97\u4f20\u5165\u7684\u4e24\u4e2a\u53c2\u6570\u7684\u6c42\u548c\uff0c\u7136\u540e\u5229\u7528\u88c5\u9970\u5668\u6269\u5c55\u529f\u80fd\uff0c\u5bf9\u6c42\u548c\u540e\u7684\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[1,592],"tags":[],"class_list":["post-1874","post","type-post","status-publish","format-standard","hentry","category-all","category-python"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/1874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1874"}],"version-history":[{"count":0,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/1874\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}