{"id":664,"date":"2026-03-11T14:45:00","date_gmt":"2026-03-11T06:45:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=664"},"modified":"2026-07-14T15:29:43","modified_gmt":"2026-07-14T07:29:43","slug":"python%e4%b8%ad%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/03\/11\/python%e4%b8%ad%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f\/","title":{"rendered":"python\u4e2d\u5355\u4f8b\u6a21\u5f0f"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1. \u5355\u4f8b\u6a21\u5f0f<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class DataBase:\n    _instance = None\n\n    def __new__(cls, *args, **keywords):\n        if cls._instance is None:\n            cls._instance = super().__new__(cls)\n        return cls._instance\n\n# \u6d4b\u8bd5\nconn1 = Database()\nconn2 = Database()\nprint(conn1 is conn2)  # True\uff0c\u8bf4\u660e\u662f\u540c\u4e00\u4e2a\u5b9e\u4f8b<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u65e0\u53c2 <code>super()<\/code> \u662f\u8bed\u6cd5\u7cd6\uff0c\u5728 <code>__new__<\/code> \u91cc\u7b49\u4ef7\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>super().__new__(cls)\n# \u7b49\u4ef7\u5b8c\u6574\u5199\u6cd5\nsuper(Database, cls).__new__(cls)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">super \u4f1a\u987a\u7740 MRO \u627e\u4e0b\u4e00\u4e2a\u7236\u7c7b\uff08\u8fd9\u91cc\u5c31\u662f object\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>__new__<\/code> \u662f<strong>\u7279\u6b8a\u7684\u9759\u6001\u6784\u9020\u65b9\u6cd5<\/strong>\uff08\u5185\u7f6e\u7279\u6b8a\u9b54\u672f\u65b9\u6cd5\uff0c\u548c\u666e\u901a\u5b9e\u4f8b\u65b9\u6cd5 \/ \u7c7b\u65b9\u6cd5\u89c4\u5219\u4e0d\u4e00\u6837\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">python \u4e2d\u6240\u6709\u7c7b<strong>\u9ed8\u8ba4\u9690\u5f0f\u7ee7\u627f object<\/strong>\uff0c\u7b49\u4ef7\u4e8e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Database(object):\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u4e0d\u5199\u7236\u7c7b\uff0c\u5e95\u5c42\u81ea\u52a8\u7ee7\u627f <code>object<\/code>\uff0c\u6240\u4ee5\u4e00\u5b9a\u5b58\u5728\u7236\u7c7b\uff0c<code>super()<\/code> \u5b8c\u5168\u5408\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">super () \u5728 <strong>new<\/strong> \u91cc\u7684\u4f5c\u7528<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>super().__new__(cls)<\/code> \u7b49\u4ef7\u4e8e\u62ff\u5230\u7236\u7c7b <code>object<\/code> \u7684 <code>__new__<\/code> \u65b9\u6cd5\uff0c\u8c03\u7528\u5e95\u5c42\u5185\u5b58\u5206\u914d\u903b\u8f91\uff0c\u521b\u5efa\u7a7a\u5b9e\u4f8b\u5bf9\u8c61\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u4e0d\u5199 <code>super().__new__(cls)<\/code>\uff0c\u5c31\u6ca1\u6709\u5bf9\u8c61\uff0c\u6ca1\u6cd5\u8d4b\u503c\u7ed9 <code>_instance<\/code>\uff1b<\/li>\n\n\n\n<li><code>object.__new__(Database)<\/code> \u624d\u662f\u771f\u6b63\u5206\u914d\u5185\u5b58\u3001\u751f\u6210\u5b9e\u4f8b\u7684\u5e95\u5c42\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u5bf9\u8c61\u7684\u521b\u5efa\u8fc7\u7a0b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f60\u5199 <code>obj = MyClass()<\/code> \u65f6\uff0cPython \u5185\u90e8\u6267\u884c\u6d41\u7a0b\u5206\u4e24\u6b65\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u8c03\u7528 <code>MyClass.__new__(cls)<\/code>\n<ul class=\"wp-block-list\">\n<li><code>__new__<\/code> \u8d1f\u8d23\u5206\u914d\u5185\u5b58\u3001\u521b\u5efa\u5e76\u8fd4\u56de<strong>\u7a7a\u5b9e\u4f8b\u5bf9\u8c61<\/strong><\/li>\n\n\n\n<li>\u5982\u679c\u7c7b\u6ca1\u6709\u81ea\u5b9a\u4e49 <code>__new__<\/code>\uff0c\u4f1a\u5411\u4e0a\u8c03\u7528\u7236\u7c7b <code>object.__new__<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u62ff\u5230 <code>__new__<\/code> \u8fd4\u56de\u7684\u5b9e\u4f8b\u540e\uff0c\u8c03\u7528 <code>__init__(self)<\/code> \u505a\u521d\u59cb\u5316\u8d4b\u503c<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u7c7b\u4f3c\u4e8e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def create_object(cls, *args, **kwargs):\n    # 1. \u8c03\u7528 __new__ \u521b\u5efa\u5b9e\u4f8b\n    obj = cls.__new__(cls, *args, **kwargs)\n\n    # 2. \u7c7b\u578b\u68c0\u67e5\uff1a\u53ea\u6709 obj \u662f cls \u7684\u5b9e\u4f8b\uff08\u6216\u5176\u5b50\u7c7b\u7684\u5b9e\u4f8b\uff09\u65f6\u624d\u8c03\u7528 __init__\n    if isinstance(obj, cls):\n        obj.__init__(*args, **kwargs)\n\n    # 3. \u8fd4\u56de\u5bf9\u8c61\n    return obj<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u5bf9\u8c61\u6c60\/\u7f13\u5b58<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class ConnectionPool:\n    _pool = {}\n\n    def __new__(cls, conn_id):\n        if conn_id not in cls._pool:\n            obj = super().__new__(cls)\n            cls._pool&#91;conn_id] = obj\n        return cls._pool&#91;conn_id]\n\n# \u6d4b\u8bd5\npool1 = ConnectionPool(\"conn_1\")\npool2 = ConnectionPool(\"conn_1\")\npool3 = ConnectionPool(\"conn_2\")\nprint(pool1 is pool2)  # True\uff0c\u76f8\u540c conn_id \u8fd4\u56de\u540c\u4e00\u4e2a\u5bf9\u8c61\nprint(pool1 is pool3)  # False\uff0c\u4e0d\u540c conn_id \u8fd4\u56de\u4e0d\u540c\u5bf9\u8c61<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u6b63\u6574\u6570\uff08\u5e26\u9ed8\u8ba4\u503c\u56de\u9000\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>__new__<\/code> \u53ef\u4ee5\u8fd4\u56de\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u5f53\u8fd4\u56de\u7684\u5bf9\u8c61\u4e0d\u662f\u5f53\u524d\u7c7b\u7684\u5b9e\u4f8b\u65f6\uff0c<code>__init__<\/code> \u4e0d\u4f1a\u88ab\u6267\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class PositiveInt:<br> &nbsp; &nbsp;def __new__(cls, value):<br> &nbsp; &nbsp; &nbsp; &nbsp;if value &lt; 0:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return 0 &nbsp;# \u8fd4\u56de int \u7c7b\u578b\u7684 0\uff0c\u4e0d\u662f PositiveInt \u7684\u5b9e\u4f8b<br> &nbsp; &nbsp; &nbsp; &nbsp;return super().__new__(cls)<br>\u200b<br> &nbsp; &nbsp;def __init__(self, value):<br> &nbsp; &nbsp; &nbsp; &nbsp;print(\"PositiveInt.__init__ \u88ab\u8c03\u7528\")<br> &nbsp; &nbsp; &nbsp; &nbsp;self.value = value<br>\u200b<br># \u6d4b\u8bd5<br>p = PositiveInt(5)<br>print(type(p)) &nbsp; &nbsp;# &lt;class '__main__.PositiveInt'&gt;<br>print(p.value) &nbsp; &nbsp;# 5<br>\u200b<br>n = PositiveInt(-3)<br>print(type(n)) &nbsp; &nbsp;# &lt;class 'int'&gt;<br>print(n) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# 0<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. \u5355\u4f8b\u6a21\u5f0f \u65e0\u53c2 super() \u662f\u8bed\u6cd5\u7cd6\uff0c\u5728..<\/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-664","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\/664","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=664"}],"version-history":[{"count":3,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/664\/revisions"}],"predecessor-version":[{"id":667,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/664\/revisions\/667"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}