{"id":3276,"date":"2025-06-02T15:25:20","date_gmt":"2025-06-04T08:23:19","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3276"},"modified":"2025-06-04T15:25:20","modified_gmt":"2025-06-04T08:23:19","slug":"python%e6%96%b0%e7%89%b9%e6%80%a7%ef%bc%9a%e4%bd%bf%e7%94%a8new%e4%be%86%e5%89%b5%e5%bb%ba%e7%89%a9%e4%bb%b6-3","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e6%96%b0%e7%89%b9%e6%80%a7%ef%bc%9a%e4%bd%bf%e7%94%a8new%e4%be%86%e5%89%b5%e5%bb%ba%e7%89%a9%e4%bb%b6-3\/","title":{"rendered":"2025 \u6700\u65b0 Python \u8a9e\u6cd5\uff1a\u4f7f\u7528 `__new__()` \u65b9\u6cd5\u5275\u5efa\u7269\u4ef6\u7684\u5b8c\u6574\u6307\u5357"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, __new__()\"><\/p>\n<h1>Python __new__() \u65b9\u6cd5\u7684\u4f7f\u7528\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`__new__()` \u65b9\u6cd5\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u51fd\u5f0f\uff0c\u7528\u65bc\u5275\u5efa\u4e00\u500b\u65b0\u7684\u7269\u4ef6\u5be6\u4f8b\u3002\u9019\u7a2e\u65b9\u6cd5\u901a\u5e38\u5728\u81ea\u5b9a\u7fa9\u985e\u5225\u4e2d\u4f7f\u7528\uff0c\u7279\u5225\u662f\u7576\u4f60\u9700\u8981\u63a7\u5236\u7269\u4ef6\u7684\u5275\u5efa\u904e\u7a0b\u6642\u3002\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e `__new__()` \u7684\u4f7f\u7528\u65b9\u6cd5\u3001\u5be6\u4f5c\u7bc4\u4f8b\u3001\u932f\u8aa4\u6392\u9664\u4ee5\u53ca\u5ef6\u4f38\u61c9\u7528\u3002<\/p>\n<p>## \u4f7f\u7528 `__new__()` \u5275\u5efa\u7269\u4ef6<\/p>\n<p>`__new__()` \u65b9\u6cd5\u662f\u4e00\u500b\u975c\u614b\u65b9\u6cd5\uff0c\u8ca0\u8cac\u5206\u914d\u7269\u4ef6\u7684\u8a18\u61b6\u9ad4\u4e26\u8fd4\u56de\u65b0\u7684\u7269\u4ef6\u5be6\u4f8b\u3002\u5176\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __new__(cls, *args, **kwargs):<br \/>\n        # \u5275\u5efa\u4e00\u500b\u65b0\u7684\u7269\u4ef6<br \/>\n        instance = super(MyClass, cls).__new__(cls)<br \/>\n        return instance<br \/>\n&#8220;`<\/p>\n<p>## \u5b8c\u6574\u5be6\u4f5c\u7bc4\u4f8b<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5b8c\u6574\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 `__new__()` \u5275\u5efa\u7269\u4ef6\u4e26\u521d\u59cb\u5316\u5c6c\u6027\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __new__(cls, arg1, arg2):<br \/>\n        # \u5275\u5efa\u4e00\u500b\u65b0\u7684\u7269\u4ef6<br \/>\n        instance = super(MyClass, cls).__new__(cls)<br \/>\n        return instance<\/p>\n<p>    def __init__(self, arg1, arg2):<br \/>\n        self.arg1 = arg1<br \/>\n        self.arg2 = arg2<\/p>\n<p># \u5275\u5efa\u4e00\u500b\u65b0\u7684\u7269\u4ef6<br \/>\nobj = MyClass(&#8220;foo&#8221;, &#8220;bar&#8221;)<\/p>\n<p># \u8a2a\u554f\u7269\u4ef6\u7684\u5c6c\u6027<br \/>\nprint(obj.arg1)  # \u8f38\u51fa\uff1afoo<br \/>\nprint(obj.arg2)  # \u8f38\u51fa\uff1abar<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba `MyClass` \u7684\u81ea\u5b9a\u7fa9\u985e\u5225\uff0c\u4e26\u4f7f\u7528 `__new__()` \u65b9\u6cd5\u4f86\u5275\u5efa\u7269\u4ef6\u3002\u5728 `__init__()` \u65b9\u6cd5\u4e2d\uff0c\u6211\u5011\u521d\u59cb\u5316\u4e86\u7269\u4ef6\u7684\u5c6c\u6027\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5982\u679c\u5728\u4f7f\u7528 `__new__()` \u65b9\u6cd5\u6642\u9047\u5230\u932f\u8aa4\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<p>1. \u78ba\u4fdd\u4f60\u6b63\u78ba\u5730\u8abf\u7528 `super()` \u65b9\u6cd5\u3002<br \/>\n2. \u78ba\u4fdd\u5728 `__new__()` \u4e2d\u8fd4\u56de\u7684\u7269\u4ef6\u5be6\u4f8b\u662f\u6b63\u78ba\u7684\u985e\u5225\u3002<br \/>\n3. \u5982\u679c\u4f7f\u7528\u591a\u91cd\u7e7c\u627f\uff0c\u6aa2\u67e5\u985e\u5225\u7684\u89e3\u6790\u9806\u5e8f\uff08MRO\uff09\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`__new__()` \u65b9\u6cd5\u4e0d\u50c5\u9650\u65bc\u5275\u5efa\u7269\u4ef6\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u55ae\u4f8b\u6a21\u5f0f\u7b49\u8a2d\u8a08\u6a21\u5f0f\u7684\u5be6\u73fe\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u55ae\u4f8b\u6a21\u5f0f\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Singleton:<br \/>\n    _instance = None<\/p>\n<p>    def __new__(cls, *args, **kwargs):<br \/>\n        if not cls._instance:<br \/>\n            cls._instance = super(Singleton, cls).__new__(cls)<br \/>\n        return cls._instance<\/p>\n<p># \u4f7f\u7528 Singleton \u985e\u5225<br \/>\ns1 = Singleton()<br \/>\ns2 = Singleton()<br \/>\nprint(s1 is s2)  # \u8f38\u51fa\uff1aTrue\uff0c\u5169\u8005\u70ba\u540c\u4e00\u500b\u5be6\u4f8b<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c`Singleton` \u985e\u5225\u78ba\u4fdd\u53ea\u6709\u4e00\u500b\u5be6\u4f8b\u88ab\u5275\u5efa\u3002<\/p>\n<p>\u5728 Python \u4e2d\uff0c`__new__()` \u65b9\u6cd5\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u7269\u4ef6\u5275\u5efa\u529f\u80fd\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u9032\u884c\u7279\u6b8a\u63a7\u5236\u6642\u3002\u5c0d\u65bc\u60f3\u6df1\u5165\u4e86\u89e3\u7269\u4ef6\u5c0e\u5411\u7de8\u7a0b\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u719f\u6089\u9019\u500b\u65b9\u6cd5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p>\u60f3\u9032\u4e00\u6b65\u5b78\u7fd2 Python \u7684\u7269\u4ef6\u5c0e\u5411\u7de8\u7a0b\uff0c\u8acb\u53c3\u8003\u9019\u7bc7 [Python \u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08\u5165\u9580](https:\/\/vocus.cc\/article\/6015f4d8fd89780001a9e8f1)\u3002<\/p>\n<p>## \u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/p>\n<p>### Q1: `__new__()` \u548c `__init__()` \u6709\u4ec0\u9ebc\u5340\u5225\uff1f<br \/>\nA1: `__new__()` \u662f\u4e00\u500b\u975c\u614b\u65b9\u6cd5\uff0c\u8ca0\u8cac\u5275\u5efa\u7269\u4ef6\uff0c\u800c `__init__()` \u662f\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u7528\u65bc\u8a2d\u7f6e\u7269\u4ef6\u7684\u5c6c\u6027\u3002`__new__()` \u5148\u65bc `__init__()` \u88ab\u8abf\u7528\u3002<\/p>\n<p>### Q2: \u7576\u9700\u8981\u5275\u5efa\u55ae\u4f8b\u985e\u5225\u6642\u61c9\u4f7f\u7528 `__new__()` \u55ce\uff1f<br \/>\nA2: \u662f\u7684\uff0c\u4f7f\u7528 `__new__()` \u53ef\u4ee5\u78ba\u4fdd\u5728\u985e\u5225\u4e2d\u53ea\u5275\u5efa\u4e00\u500b\u5be6\u4f8b\uff0c\u9019\u662f\u5be6\u73fe\u55ae\u4f8b\u6a21\u5f0f\u7684\u5e38\u898b\u65b9\u6cd5\u3002<\/p>\n<p>### Q3: `__new__()` \u53ef\u4ee5\u7528\u65bc\u54ea\u4e9b\u5834\u666f\uff1f<br \/>\nA3: `__new__()` \u53ef\u4ee5\u7528\u65bc\u9700\u8981\u63a7\u5236\u7269\u4ef6\u5275\u5efa\u7684\u5834\u666f\uff0c\u4f8b\u5982\u55ae\u4f8b\u6a21\u5f0f\u3001\u5143\u985e\u3001\u4ee5\u53ca\u5be6\u73fe\u4e0d\u53ef\u8b8a\u985e\u5225\u7b49\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u65b0\u7279\u6027new()\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u5275\u5efa\u7269\u4ef6\uff0c\u66f4\u52a0\u65b9\u4fbf\u5feb\u6377\u3002\u672c\u6587\u5c07\u4ecb\u7d39new()\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u4f86\u63d0\u9ad8\u958b\u767c\u6548\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3276","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-QQ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3276"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3276\/revisions"}],"predecessor-version":[{"id":3277,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3276\/revisions\/3277"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}