{"id":674,"date":"2026-03-15T20:37:00","date_gmt":"2026-03-15T12:37:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=674"},"modified":"2026-07-14T20:41:36","modified_gmt":"2026-07-14T12:41:36","slug":"python%e8%a3%85%e9%a5%b0%e5%99%a8","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/03\/15\/python%e8%a3%85%e9%a5%b0%e5%99%a8\/","title":{"rendered":"python\u88c5\u9970\u5668"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u88c5\u9970\u5668\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u63a5\u53d7\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u65b0\u51fd\u6570\u7684\u9ad8\u9636\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def my_decorator(func):\n    def wrapper():\n        print(\"\u51fd\u6570\u6267\u884c\u524d\")\n        func()\n        print(\"\u51fd\u6570\u6267\u884c\u540e\")\n    return wrapper\n\n# \u4e0b\u9762\u7684\u4ee3\u7801\ndef say_hello():\n    print(\"Hello!\")\nsay_hello = my_decorator(say_hello)\n\n# \u7b49\u6548\u4e8e\n@my_decorator\ndef say_hello():\n    print(\"Hello!\")\n\nsay_hello()\n# \u8f93\u51fa\uff1a\n# \u51fd\u6570\u6267\u884c\u524d\n# Hello!\n# \u51fd\u6570\u6267\u884c\u540e<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u591a\u4e2a\u88c5\u9970\u5668\u53e0\u52a0<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528\u591a\u4e2a\u88c5\u9970\u5668\uff0c\u6267\u884c\u987a\u5e8f\u4e3a\u4ece\u4e0b\u5230\u4e0a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@decorator_a<br>@decorator_b<br>def func():<br> &nbsp; &nbsp;pass<br>\u200b<br># \u7b49\u6548\u4e8e\uff1a<br># func = decorator_a(decorator_b(func))<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u73b0timer\u88c5\u9970\u5668<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>def timer(func):\n    import time\n    def wrapper():\n        start = time.time()\n        func()\n        end = time.time()\n        cost = end - start\n        print(f\"{func.__name__}  \u6267\u884c\u65f6\u95f4: {cost:.4f} \u79d2\")\n    return wrapper\n@timer\ndef slow_function():\n    time.sleep(1)\n    return \"Done\"\n\nslow_function()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u73b0wraps\u88c5\u9970\u5668<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u73b0wraps\u88c5\u9970\u5668\uff0c\u7528\u4e8e\u4e0d\u6539\u53d8\u51fd\u6570\u7684\u540d\u79f0\u548c\u6ce8\u91ca<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def wraps(func):\n    def decorator(wrapper):\n        wrapper.__name__ = func.__name__\n        wrapper.__doc__ = func.__doc__\n        return wrapper\n    return decorator\n\n\ndef my_decorator(func):\n    @wraps(func)\n    def wrapper():\n        print(\"\u51fd\u6570\u6267\u884c\u524d\")\n        func()\n        print(\"\u51fd\u6570\u6267\u884c\u540e\")\n\n    return wrapper\n\n\n@my_decorator\ndef say_hello():\n    \"\"\"\u6253\u62db\u547c\"\"\"\n    print(\"Hello!\")\n\n\nsay_hello()\n# \u8f93\u51fa\uff1a\n# \u51fd\u6570\u6267\u884c\u524d\n# Hello!\n# \u51fd\u6570\u6267\u884c\u540e\nprint(\"name\", say_hello.__name__)\nprint(\"doc\", say_hello.__doc__)\n# \u8f93\u51fa\uff1a\n# name say_hello\n# doc \u6253\u62db\u547c\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u73b0repeat\u88c5\u9970\u5668<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def repeat(n):\n    def decorator(func):\n        def wrapper(*args, **kwargs):\n            t = n\n            while t > 0:\n                func(*args, **kwargs)\n                t -= 1\n\n        return wrapper\n\n    return decorator\n\n\n@repeat(3)\ndef say_hello(s):\n    print(s)\n\n\nsay_hello(1)  # \u8f93\u51fa: 1 1 1<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u73b0cache\u88c5\u9970\u5668<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7f16\u5199\u4e00\u4e2a\u88c5\u9970\u5668 <code>cache<\/code>\uff0c\u7f13\u5b58\u51fd\u6570\u7684\u8ba1\u7b97\u7ed3\u679c\u3002\u5f53\u4f7f\u7528\u76f8\u540c\u7684\u53c2\u6570\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u76f4\u63a5\u8fd4\u56de\u7f13\u5b58\u7684\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def cache(func):\n    _cache = {}\n\n    def wrapper(*args, **kwargs):\n        key = (args, tuple(kwargs.items()))\n        if key not in _cache:\n            _cache&#91;key] = func(*args, **kwargs)\n        return _cache&#91;key]\n\n    return wrapper\n\n\n@cache\ndef fibonacci(n):\n    if n &lt; 2:\n        return n\n    return fibonacci(n - 1) + fibonacci(n - 2)\n\n\nprint(fibonacci(60))  # \u5e94\u8be5\u5feb\u901f\u8fd4\u56de\u7ed3\u679c\uff0c\u8f93\u51fa: 9227465<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u63d0\u793a\uff1a<\/strong> \u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u53c2\u6570\u5230\u7ed3\u679c\u7684\u6620\u5c04\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u73b0to_dict\u88c5\u9970\u5668<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7f16\u5199\u4e00\u4e2a\u7c7b\u88c5\u9970\u5668 <code>to_dict<\/code>\uff0c\u81ea\u52a8\u4e3a\u7c7b\u751f\u6210 <code>to_dict<\/code> \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u5178<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def to_dict(cls):\n    def to_dict_method(self):\n        return self.__dict__\n\n    cls.to_dict = to_dict_method\n    return cls\n\n\n@to_dict\nclass Point:\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y\n\n\np = Point(3, 4)\nprint(p.to_dict())  # \u8f93\u51fa: {'x': 3, 'y': 4}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u88c5\u9970\u5668\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u63a5\u53d7\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u65b0\u51fd\u6570\u7684\u9ad8..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,14],"tags":[54],"class_list":["post-674","post","type-post","status-publish","format-standard","hentry","category-python","category-back","tag-python"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/python\/\" rel=\"category tag\">Python<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/\" rel=\"category tag\">\u540e\u7aef<\/a>","tag_info":"\u540e\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=674"}],"version-history":[{"count":3,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/674\/revisions"}],"predecessor-version":[{"id":677,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/674\/revisions\/677"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}