{"id":672,"date":"2026-03-14T16:53:00","date_gmt":"2026-03-14T08:53:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=672"},"modified":"2026-07-14T17:12:34","modified_gmt":"2026-07-14T09:12:34","slug":"python-%e5%85%83%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/03\/14\/python-%e5%85%83%e7%b1%bb\/","title":{"rendered":"python \u5143\u7c7b"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u7c7b\u7684\u521b\u5efa\u8fc7\u7a0b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <code>class<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\u7c7b\u65f6\uff0cPython \u5e95\u5c42\u4f1a\u8c03\u7528\u5143\u7c7b\u6765\u521b\u5efa\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Dog:<br> &nbsp; &nbsp;pass<br>\u200b<br>\u200b<br># \u7b49\u6548\u4e8e<br>class Dog(metaclass=type):<br> &nbsp; &nbsp;pass<br>\u200b<br>\u200b<br># \u7b49\u6548\u4e8e<br>Dog = type(\"Dog\", (), {})<br>\u200b<br># \u7b49\u6548\u4e8e<br>Dog = type.__call__(type, \"Dog\", (), {})<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e5f\u5c31\u662f\u8bf4\uff0c<code>class<\/code> \u5173\u952e\u5b57\u53ea\u662f\u8bed\u6cd5\u7cd6\uff0c\u5e95\u5c42\u4ecd\u7136\u662f\u901a\u8fc7 <code>type()<\/code> \u521b\u5efa\u7c7b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u81ea\u5b9a\u4e49\u5143\u7c7b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u7ee7\u627f <code>type<\/code>\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5143\u7c7b\uff0c\u63a7\u5236\u7c7b\u7684\u521b\u5efa\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyMeta(type):<br> &nbsp; &nbsp;def __new__(mcs, name, bases, namespace):<br> &nbsp; &nbsp; &nbsp; &nbsp;print(f\"\u6b63\u5728\u521b\u5efa\u7c7b: {name}\")<br> &nbsp; &nbsp; &nbsp; &nbsp;print(f\"\u7236\u7c7b: {bases}\")<br> &nbsp; &nbsp; &nbsp; &nbsp;print(f\"\u5c5e\u6027: {list(namespace.keys())}\")<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u5fc5\u987b\u8c03\u7528 type.__new__ \u6765\u771f\u6b63\u521b\u5efa\u7c7b<br> &nbsp; &nbsp; &nbsp; &nbsp;cls = super().__new__(mcs, name, bases, namespace)<br> &nbsp; &nbsp; &nbsp; &nbsp;return cls<br>\u200b<br>\u200b<br># \u4f7f\u7528 metaclass \u53c2\u6570\u6307\u5b9a\u5143\u7c7b<br>class Dog(metaclass=MyMeta):<br> &nbsp; &nbsp;species = \"Canis familiaris\"<br>\u200b<br> &nbsp; &nbsp;def bark(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;print(\"Woof!\")<br>\u200b<br>\u200b<br># \u7b49\u6548\u4e8e<br># def bark(self):<br># &nbsp; &nbsp; print(\"Woof!\")<br>\u200b<br>\u200b<br># Dog = type.__call__(MyMeta, \"Dog\", (), {\"species\": \"Canis familiaris\", \"bark\": bark})<br>\u200b<br># \u8f93\u51fa\uff1a<br># \u6b63\u5728\u521b\u5efa\u7c7b: Dog<br># \u7236\u7c7b: ()<br># \u5c5e\u6027: &#91;'__module__', '__qualname__', 'species', 'bark']<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u53c2\u6570\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>mcs<\/code>\uff1a\u5143\u7c7b\u81ea\u8eab\uff08\u7c7b\u4f3c\u7c7b\u65b9\u6cd5\u4e2d\u7684 <code>cls<\/code>\uff09<\/li>\n\n\n\n<li><code>name<\/code>\uff1a\u7c7b\u540d\u5b57\u7b26\u4e32<\/li>\n\n\n\n<li><code>bases<\/code>\uff1a\u7236\u7c7b\u5143\u7ec4<\/li>\n\n\n\n<li><code>namespace<\/code>\uff1a\u7c7b\u5c5e\u6027\u7684\u5b57\u5178<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u6df1\u5165\uff1a\u5143\u7c7b\u7684\u67e5\u627e\u987a\u5e8f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b50\u7c7b\u4f1a\u7ee7\u627f\u7236\u7c7b\u7684\u5143\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyMeta(type):<br> &nbsp; &nbsp;pass<br>\u200b<br>class Base(metaclass=MyMeta):<br> &nbsp; &nbsp;pass<br>\u200b<br>class Child(Base): &nbsp;# \u81ea\u52a8\u7ee7\u627f MyMeta<br> &nbsp; &nbsp;pass<br>\u200b<br>print(type(Child)) &nbsp;# &lt;class '__main__.MyMeta'&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u7236\u7c7b\u5143\u7c7b\u4e0d\u517c\u5bb9\uff0c\u9700\u8981\u4f7f\u7528\u66f4\u901a\u7528\u7684\u5143\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MetaA(type):<br> &nbsp; &nbsp;pass<br>\u200b<br>class MetaB(type):<br> &nbsp; &nbsp;pass<br>\u200b<br>class A(metaclass=MetaA):<br> &nbsp; &nbsp;pass<br>\u200b<br>class B(metaclass=MetaB):<br> &nbsp; &nbsp;pass<br>\u200b<br># class C(A, B): pass  # TypeError! \u5143\u7c7b\u51b2\u7a81<br>\u200b<br># \u89e3\u51b3\u65b9\u6cd5\uff1a\u521b\u5efa\u517c\u5bb9\u7684\u5143\u7c7b<br>class CommonMeta(MetaA, MetaB):<br> &nbsp; &nbsp;pass<br>\u200b<br>class C(A, B, metaclass=CommonMeta): &nbsp;# \u6b63\u5e38<br> &nbsp; &nbsp;pass<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">\u6982\u5ff5<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\">\u5143\u7c7b<\/td><td class=\"has-text-align-left\" data-align=\"left\">\u521b\u5efa\u7c7b\u7684\u7c7b\uff0c\u9ed8\u8ba4\u662f <code>type<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>__new__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u521b\u5efa\u7c7b\uff0c\u8fd4\u56de\u7c7b\u5bf9\u8c61<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>__init__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u521d\u59cb\u5316\u7c7b\uff0c\u65e0\u8fd4\u56de\u503c<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>__call__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u63a7\u5236\u7c7b\u7684\u5b9e\u4f8b\u5316\u8fc7\u7a0b<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u5e94\u7528\u573a\u666f<\/td><td class=\"has-text-align-left\" data-align=\"left\">\u547d\u540d\u68c0\u67e5\u3001\u81ea\u52a8\u6ce8\u518c\u3001\u65b9\u6cd5\u589e\u5f3a\u3001ORM \u7b49<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u3001\u5b9e\u73b0\u5355\u4f8b\u5143\u7c7b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7f16\u5199\u4e00\u4e2a\u5143\u7c7b <code>SingletonMeta<\/code>\uff0c\u4f7f\u5f97\u4efb\u4f55\u4f7f\u7528\u8be5\u5143\u7c7b\u7684\u7c7b\u90fd\u81ea\u52a8\u6210\u4e3a\u5355\u4f8b\u6a21\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class SingletonMeta(type):\n    _instance = {}\n\n    def __call__(cls, *args, **kwds):\n        if cls not in cls._instance:\n            ins = super().__call__(*args, **kwds)\n            cls._instance&#91;cls] = ins\n        return cls._instance&#91;cls]\n\n    pass\n\n\nclass Database(metaclass=SingletonMeta):\n    def __init__(self, host):\n        self.host = host\n\n\ndb1 = Database(\"localhost\")\ndb2 = Database(\"remote\")\nprint(db1 is db2)  # \u5e94\u8be5\u8f93\u51fa True\nprint(db1.host)  # \u5e94\u8be5\u8f93\u51fa localhost\n\n\nclass Player(metaclass=SingletonMeta):\n    pass\n\n\np1 = Player()\np2 = Player()\n\nprint(p1 is p2)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001\u81ea\u52a8\u6ce8\u518c\u5b50\u7c7b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7f16\u5199\u4e00\u4e2a\u5143\u7c7b <code>PluginMeta<\/code>\uff0c\u4f7f\u5f97\u4efb\u4f55\u7ee7\u627f\u81ea <code>Plugin<\/code> \u7684\u5b50\u7c7b\u90fd\u4f1a\u88ab\u81ea\u52a8\u6ce8\u518c\u5230 <code>PluginMeta.registry<\/code> \u5b57\u5178\u4e2d\uff08\u952e\u4e3a\u7c7b\u540d\uff0c\u503c\u4e3a\u7c7b\u672c\u8eab\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class PluginMeta(type):\n    registry = {}\n\n    def __new__(mcs, name, bases, namespace):\n        cls = super().__new__(mcs, name, bases, namespace)\n        if name != \"Plugin\":\n            mcs.registry&#91;name] = cls\n        return cls\n\n    pass\n\n\nclass Plugin(metaclass=PluginMeta):\n    pass\n\n\nclass ImagePlugin(Plugin):\n    pass\n\n\nclass TextPlugin(Plugin):\n    pass\n\n\nclass VideoPlugin(Plugin):\n    pass\n\n\nprint(PluginMeta.registry)\n# \u5e94\u8be5\u8f93\u51fa\u7c7b\u4f3c\uff1a{'ImagePlugin': &lt;class '__main__.ImagePlugin'>, 'TextPlugin': &lt;class '__main__.TextPlugin'>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e09\u3001\u4e3a\u6240\u6709\u65b9\u6cd5\u6dfb\u52a0\u65e5\u5fd7<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7f16\u5199\u4e00\u4e2a\u5143\u7c7b <code>LogMeta<\/code>\uff0c\u81ea\u52a8\u4e3a\u7c7b\u4e2d\u6bcf\u4e2a\u975e\u79c1\u6709\u65b9\u6cd5\uff08\u5373\u4e0d\u4ee5 <code>_<\/code> \u5f00\u5934\u7684\u65b9\u6cd5\uff09\u6dfb\u52a0\u6267\u884c\u65e5\u5fd7\u3002\u8c03\u7528\u65b9\u6cd5\u65f6\uff0c\u5148\u6253\u5370 <code>[LOG] \u8c03\u7528 {\u65b9\u6cd5\u540d}<\/code>\uff0c\u518d\u6267\u884c\u539f\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class LogMeta(type):\n    def __new__(mcs, name, bases, namespace):\n        for key, value in namespace.items():\n            if not key.startswith(\"_\") and callable(value):\n                namespace&#91;key] = mcs.log_wrapper(value)\n        return super().__new__(mcs, name, bases, namespace)\n\n    @staticmethod\n    def log_wrapper(func):\n        def wrapper(*args, **kwargs):\n            print(f\"&#91;LOG] \u8c03\u7528 {func.__name__}\")\n            r = func(*args, **kwargs)\n            return r\n\n        return wrapper\n\n\nclass Calculator(metaclass=LogMeta):\n    a = 1\n\n    def add(self, a, b):\n        return a + b\n\n    def sub(self, a, b):\n        return a - b\n\n\ncalc = Calculator()\nprint(calc.add(3, 5))\nprint(calc.sub(10, 4))\n\n# \u5e94\u8be5\u8f93\u51fa\uff1a\n# &#91;LOG] \u8c03\u7528 add\n# 8\n# &#91;LOG] \u8c03\u7528 sub\n# 6<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7c7b\u7684\u521b\u5efa\u8fc7\u7a0b \u4f7f\u7528 class \u5173\u952e\u5b57\u5b9a\u4e49\u7c7b\u65f6\uff0cP..<\/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":[],"class_list":["post-672","post","type-post","status-publish","format-standard","hentry","category-python","category-back"],"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\/672","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=672"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/672\/revisions"}],"predecessor-version":[{"id":673,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/672\/revisions\/673"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}