{"id":3765,"date":"2025-05-30T00:01:31","date_gmt":"2025-05-29T16:01:31","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3765"},"modified":"2025-05-30T00:01:31","modified_gmt":"2025-05-29T16:01:31","slug":"python-%e4%b8%ad%e6%80%8e%e6%a0%b7%e5%ae%9e%e7%8e%b0%e5%85%83%e7%bc%96%e7%a8%8b%ef%bc%88metaprogramming%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/05\/30\/python-%e4%b8%ad%e6%80%8e%e6%a0%b7%e5%ae%9e%e7%8e%b0%e5%85%83%e7%bc%96%e7%a8%8b%ef%bc%88metaprogramming%ef%bc%89\/","title":{"rendered":"Python \u4e2d\u600e\u6837\u5b9e\u73b0\u5143\u7f16\u7a0b\uff08Metaprogramming\uff09"},"content":{"rendered":"\n<p>\u5728 Python \u4e2d\uff0c\u201c<strong>\u5143\u7f16\u7a0b\uff08Metaprogramming\uff09<\/strong>\u201d \u662f\u6307\u7f16\u5199\u80fd\u64cd\u4f5c\u7a0b\u5e8f\u81ea\u8eab\u7ed3\u6784\uff08\u5982\u7c7b\u3001\u51fd\u6570\u3001\u4ee3\u7801\u5bf9\u8c61\u7b49\uff09\u7684\u4ee3\u7801\u3002\u7b80\u800c\u8a00\u4e4b\uff1a<strong>\u7a0b\u5e8f\u64cd\u4f5c\u7a0b\u5e8f\u672c\u8eab<\/strong>\u3002<\/p>\n\n\n\n<p>Python \u652f\u6301\u5143\u7f16\u7a0b\u7684\u6838\u5fc3\u80fd\u529b\u4e3b\u8981\u6765\u81ea\u5176\u52a8\u6001\u7279\u6027\uff0c\u5305\u62ec\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u5143\u7f16\u7a0b\u7684\u5e38\u89c1\u624b\u6bb5<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6280\u672f<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u88c5\u9970\u5668\uff08Decorator\uff09<\/td><td>\u5728\u4e0d\u4fee\u6539\u539f\u59cb\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u589e\u5f3a\u51fd\u6570\u6216\u7c7b\u884c\u4e3a<\/td><\/tr><tr><td>\u5143\u7c7b\uff08Metaclass\uff09<\/td><td>\u63a7\u5236\u7c7b\u7684\u521b\u5efa\u65b9\u5f0f\uff08\u5373\u7c7b\u7684\u201c\u7c7b\u201d\uff09<\/td><\/tr><tr><td><code>type()<\/code><\/td><td>\u52a8\u6001\u521b\u5efa\u7c7b<\/td><\/tr><tr><td><code>getattr()<\/code>\/<code>setattr()<\/code><\/td><td>\u52a8\u6001\u8bbf\u95ee\/\u4fee\u6539\u5bf9\u8c61\u5c5e\u6027<\/td><\/tr><tr><td>\u53cd\u5c04\uff08<code>hasattr()<\/code> \u7b49\uff09<\/td><td>\u52a8\u6001\u68c0\u67e5\u548c\u64cd\u4f5c\u5bf9\u8c61\u7ed3\u6784<\/td><\/tr><tr><td><code>exec()<\/code> \/ <code>eval()<\/code><\/td><td>\u52a8\u6001\u6267\u884c\u5b57\u7b26\u4e32\u4ee3\u7801\uff08\u9700\u8c28\u614e\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u793a\u4f8b\uff1a\u88c5\u9970\u5668\u662f\u6700\u5e38\u89c1\u7684\u5143\u7f16\u7a0b\u6280\u672f<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>def log(func):\n    def wrapper(*args, **kwargs):\n        print(f\"\u8c03\u7528 {func.__name__}() \u51fd\u6570\")\n        return func(*args, **kwargs)\n    return wrapper\n\n@log\ndef say_hello():\n    print(\"Hello!\")\n\nsay_hello()<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8c03\u7528 say_hello() \u51fd\u6570\nHello!<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2705 \u88c5\u9970\u5668\u53ef\u4ee5\u7406\u89e3\u4e3a\uff1a\u8fd0\u884c\u65f6\u4fee\u6539\u51fd\u6570\u6216\u7c7b\u884c\u4e3a\u7684\u201c\u94a9\u5b50\u201d\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u793a\u4f8b\uff1a\u4f7f\u7528 <code>type()<\/code> \u52a8\u6001\u521b\u5efa\u7c7b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># \u52a8\u6001\u521b\u5efa\u4e00\u4e2a\u7c7b\nMyClass = type(\"MyClass\", (object,), {\"x\": 5, \"show\": lambda self: print(self.x)})\n\nobj = MyClass()\nobj.show()  # \u8f93\u51fa\uff1a5<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"MyClass\"<\/code>\uff1a\u7c7b\u540d<\/li>\n\n\n\n<li><code>(object,)<\/code>\uff1a\u7ee7\u627f\u7684\u7236\u7c7b<\/li>\n\n\n\n<li>\u5b57\u5178\uff1a\u5c5e\u6027\u548c\u65b9\u6cd5\u5b9a\u4e49<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u793a\u4f8b\uff1a\u5143\u7c7b\uff08Metaclass\uff09\u4fee\u6539\u7c7b\u521b\u5efa\u884c\u4e3a<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Meta(type):\n    def __new__(cls, name, bases, dct):\n        dct&#91;'class_id'] = 123  # \u81ea\u52a8\u52a0\u5165\u5c5e\u6027\n        return super().__new__(cls, name, bases, dct)\n\nclass MyClass(metaclass=Meta):\n    pass\n\nprint(MyClass.class_id)  # \u8f93\u51fa\uff1a123<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5143\u7c7b\u662f\u5728<strong>\u7c7b\u521b\u5efa\u65f6\u81ea\u52a8\u89e6\u53d1\u7684\u201c\u94a9\u5b50\u673a\u5236\u201d<\/strong>\uff0c\u80fd\u63a7\u5236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u7b49\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u53cd\u5c04\u76f8\u5173\u51fd\u6570<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570<\/th><th>\u529f\u80fd<\/th><\/tr><\/thead><tbody><tr><td><code>hasattr(obj, attr)<\/code><\/td><td>\u662f\u5426\u6709\u67d0\u4e2a\u5c5e\u6027<\/td><\/tr><tr><td><code>getattr(obj, attr)<\/code><\/td><td>\u83b7\u53d6\u5c5e\u6027\u503c<\/td><\/tr><tr><td><code>setattr(obj, attr, val)<\/code><\/td><td>\u8bbe\u7f6e\u5c5e\u6027\u503c<\/td><\/tr><tr><td><code>delattr(obj, attr)<\/code><\/td><td>\u5220\u9664\u5c5e\u6027<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>class A:\n    x = 10\n\na = A()\nprint(getattr(a, 'x'))  # 10\nsetattr(a, 'y', 20)\nprint(a.y)              # 20<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u614e\u7528\uff1a<code>exec()<\/code> \u548c <code>eval()<\/code><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>expr = \"1 + 2 * 3\"\nresult = eval(expr)\nprint(result)  # \u8f93\u51fa\uff1a7\n\ncode = \"\"\"\ndef greet():\n    print(\"Hello from exec!\")\n\"\"\"\nexec(code)\ngreet()<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>eval()<\/code>\uff1a\u6267\u884c\u8868\u8fbe\u5f0f\uff0c\u8fd4\u56de\u503c\u3002<\/li>\n\n\n\n<li><code>exec()<\/code>\uff1a\u6267\u884c\u4e00\u6bb5\u8bed\u53e5\uff08\u5982\u51fd\u6570\u5b9a\u4e49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u26a0\ufe0f <strong>\u6ce8\u610f\u5b89\u5168\u6027\u95ee\u9898\uff0c\u6c38\u8fdc\u4e0d\u8981\u76f4\u63a5\u6267\u884c\u7528\u6237\u8f93\u5165\uff01<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5e94\u7528<\/th><th>\u5143\u7f16\u7a0b\u5f62\u5f0f<\/th><\/tr><\/thead><tbody><tr><td>ORM \u6846\u67b6\uff08\u5982 SQLAlchemy\uff09<\/td><td>\u5143\u7c7b\u52a8\u6001\u751f\u6210\u5c5e\u6027\u548c\u65b9\u6cd5<\/td><\/tr><tr><td>\u81ea\u52a8\u6ce8\u518c\u63d2\u4ef6<\/td><td>\u88c5\u9970\u5668 + \u7c7b\u6ce8\u518c\u8868<\/td><\/tr><tr><td>\u53c2\u6570\u6821\u9a8c \/ \u7f13\u5b58\u88c5\u9970\u5668<\/td><td>\u51fd\u6570\u88c5\u9970\u5668<\/td><\/tr><tr><td>\u52a8\u6001 API \u8def\u7531<\/td><td>\u52a8\u6001\u751f\u6210\u51fd\u6570\u548c\u8def\u5f84<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u51fa\u7ad9\u94fe\u63a5<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udcd8 Python \u5b98\u65b9\u6587\u6863\uff08\u6570\u636e\u6a21\u578b\uff09<br>\ud83d\udd17 <a href=\"https:\/\/docs.python.org\/3\/reference\/datamodel.html\">https:\/\/docs.python.org\/3\/reference\/datamodel.html<\/a><\/li>\n\n\n\n<li>\ud83d\udcd8 Python Decorators Guide<br>\ud83d\udd17 <a href=\"https:\/\/realpython.com\/primer-on-python-decorators\/\">https:\/\/realpython.com\/primer-on-python-decorators\/<\/a><\/li>\n\n\n\n<li>\ud83d\udcd8 Metaclass Explained<br>\ud83d\udd17 <a href=\"https:\/\/realpython.com\/python-metaclasses\/\">https:\/\/realpython.com\/python-metaclasses\/<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e5d\u3001\u603b\u7ed3\uff1a\u5143\u7f16\u7a0b\u5173\u952e\u5bf9\u7167\u8868<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6280\u672f<\/th><th>\u573a\u666f<\/th><th>\u4f18\u52bf<\/th><\/tr><\/thead><tbody><tr><td>\u88c5\u9970\u5668<\/td><td>\u589e\u5f3a\u51fd\u6570\/\u7c7b\uff08\u65e0\u4fb5\u5165\uff09<\/td><td>\u9ad8\u5ea6\u53ef\u590d\u7528\u3001\u4f18\u96c5<\/td><\/tr><tr><td><code>type()<\/code><\/td><td>\u52a8\u6001\u751f\u6210\u7c7b<\/td><td>\u975e\u5e38\u52a8\u6001\uff0c\u9002\u5408\u5143\u5de5\u5382<\/td><\/tr><tr><td>\u5143\u7c7b<\/td><td>\u63a7\u5236\u7c7b\u5b9a\u4e49<\/td><td>\u6846\u67b6\u8bbe\u8ba1\u5229\u5668\uff0c\u5f3a\u5927\u4f46\u590d\u6742<\/td><\/tr><tr><td>\u53cd\u5c04\u51fd\u6570<\/td><td>\u52a8\u6001\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027<\/td><td>\u7f16\u5199\u901a\u7528\u5de5\u5177\u6216\u8c03\u8bd5\u8f85\u52a9<\/td><\/tr><tr><td><code>exec<\/code>\/<code>eval<\/code><\/td><td>\u52a8\u6001\u6267\u884c\u4ee3\u7801\uff08\u4e0d\u63a8\u8350\u76f4\u63a5\u7528\uff09<\/td><td>\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u5371\u9669\u7cfb\u6570\u9ad8<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\uff0c\u201c\u5143\u7f16\u7a0b\uff08Metaprogramming\uff09\u201d \u662f\u6307\u7f16\u5199\u80fd\u64cd\u4f5c\u7a0b\u5e8f\u81ea\u8eab\u7ed3\u6784\uff08\u5982\u7c7b\u3001\u51fd\u6570\u3001\u4ee3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3766,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-3765","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3765"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3765\/revisions"}],"predecessor-version":[{"id":3767,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3765\/revisions\/3767"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3766"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}