{"id":3088,"date":"2025-05-31T16:30:22","date_gmt":"2025-06-04T08:57:21","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3088"},"modified":"2025-06-04T16:30:22","modified_gmt":"2025-06-04T08:57:21","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","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\/","title":{"rendered":"2025 \u6700\u65b0 Python \u7a0b\u5f0f\u6559\u5b78\uff1a\u4f7f\u7528 `__new__()` \u65b9\u6cd5\u5275\u5efa\u7269\u4ef6\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, __new__\"><\/p>\n<h1>Python __new__() \u65b9\u6cd5\u7684\u4f7f\u7528\u65b9\u6cd5<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`__new__()` \u65b9\u6cd5\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u7279\u6b8a\u65b9\u6cd5\uff0c\u5b83\u7528\u65bc\u5275\u5efa\u4e00\u500b\u65b0\u7684\u7269\u4ef6\u4e26\u6307\u5b9a\u5176\u985e\u5225\u3002\u9019\u8207\u4f7f\u7528\u5167\u5efa\u7684 `new()` \u51fd\u5f0f\u6709\u6240\u4e0d\u540c\uff0c\u56e0\u70ba Python \u4e26\u4e0d\u76f4\u63a5\u63d0\u4f9b `new()` \u51fd\u5f0f\u3002\u76f8\u53cd\uff0c`__new__()` \u662f\u4e00\u500b\u975c\u614b\u65b9\u6cd5\uff0c\u901a\u5e38\u8207 `__init__()` \u65b9\u6cd5\u4e00\u8d77\u4f7f\u7528\u4f86\u521d\u59cb\u5316\u7269\u4ef6\u3002\u9019\u88e1\uff0c\u6211\u5011\u5c07\u4ecb\u7d39 `__new__()` \u7684\u4f7f\u7528\u65b9\u6cd5\u4ee5\u53ca\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<p>## \u4ec0\u9ebc\u662f `__new__()` \u65b9\u6cd5\uff1f<\/p>\n<p>`__new__()` \u662f Python \u4e2d\u7684\u4e00\u500b\u975c\u614b\u65b9\u6cd5\uff0c\u5b83\u8ca0\u8cac\u5275\u5efa\u4e26\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u7269\u4ef6\u5be6\u4f8b\u3002\u5b83\u7684\u5b9a\u7fa9\u901a\u5e38\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __new__(cls, *args, **kwargs):<br \/>\n        instance = super(MyClass, cls).__new__(cls)<br \/>\n        return instance<br \/>\n&#8220;`<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c`cls` \u53c3\u6578\u4ee3\u8868\u985e\u5225\u672c\u8eab\uff0c\u800c `args` \u548c `kwargs` \u662f\u50b3\u905e\u7d66\u985e\u5225\u7684\u53c3\u6578\u3002<\/p>\n<p>## \u5982\u4f55\u4f7f\u7528 `__new__()` \u65b9\u6cd5\uff1f<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5be6\u969b\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 `__new__()` \u65b9\u6cd5\u4f86\u5275\u5efa\u7269\u4ef6\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __new__(cls, name):<br \/>\n        instance = super(MyClass, cls).__new__(cls)<br \/>\n        instance.name = name<br \/>\n        return instance<\/p>\n<p>    def __init__(self, name):<br \/>\n        print(f&#8221;\u7269\u4ef6 {self.name} \u5df2\u5275\u5efa\uff01&#8221;)<\/p>\n<p># \u5275\u5efa\u4e00\u500b\u65b0\u7684\u7269\u4ef6<br \/>\nobj = MyClass(name=&#8221;John&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u540d\u70ba `MyClass` \u7684\u985e\u5225\uff0c\u4e26\u4f7f\u7528 `__new__()` \u65b9\u6cd5\u4f86\u521d\u59cb\u5316\u7269\u4ef6\u7684 `name` \u5c6c\u6027\u3002\u9019\u6a23\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u7269\u4ef6\u5275\u5efa\u6642\u7acb\u5373\u8a2d\u7f6e\u5176\u5c6c\u6027\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5982\u679c\u4f60\u5728\u4f7f\u7528 `__new__()` \u65b9\u6cd5\u6642\u9047\u5230\u554f\u984c\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\u53ca\u89e3\u6c7a\u65b9\u6848\uff1a<\/p>\n<p>1. **TypeError: Can&#8217;t instantiate abstract class**\uff1a\u78ba\u4fdd\u4f60\u7684\u985e\u5225\u4e0d\u662f\u62bd\u8c61\u985e\u5225\uff0c\u6216\u5df2\u5be6\u73fe\u6240\u6709\u62bd\u8c61\u65b9\u6cd5\u3002<br \/>\n2. **AttributeError**\uff1a\u5982\u679c\u5728 `__new__()` \u65b9\u6cd5\u4e2d\u4f7f\u7528\u4e86\u5c1a\u672a\u521d\u59cb\u5316\u7684\u5c6c\u6027\uff0c\u5c07\u6703\u5c0e\u81f4\u6b64\u932f\u8aa4\u3002\u78ba\u4fdd\u5728\u8fd4\u56de\u7269\u4ef6\u4e4b\u524d\u6b63\u78ba\u8a2d\u7f6e\u5c6c\u6027\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`__new__()` \u65b9\u6cd5\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; **\u55ae\u4f8b\u6a21\u5f0f**\uff1a\u78ba\u4fdd\u985e\u5225\u53ea\u6709\u4e00\u500b\u5be6\u4f8b\u3002<br \/>\n&#8211; **\u5143\u985e**\uff1a\u7576\u4f60\u9700\u8981\u5728\u5275\u5efa\u985e\u5225\u6642\u9032\u884c\u984d\u5916\u7684\u63a7\u5236\u6642\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0c`__new__()` \u65b9\u6cd5\u662f Python \u4e2d\u5275\u5efa\u7269\u4ef6\u7684\u4e00\u500b\u91cd\u8981\u5de5\u5177\uff0c\u5c24\u5176\u5728\u9700\u8981\u63a7\u5236\u7269\u4ef6\u5275\u5efa\u904e\u7a0b\u6642\u3002\u638c\u63e1\u9019\u4e00\u65b9\u6cd5\u5c07\u6709\u52a9\u65bc\u63d0\u9ad8\u4f60\u7684 Python \u7de8\u7a0b\u80fd\u529b\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u7269\u4ef6\u5c0e\u5411\u7279\u6027\uff0c\u6b61\u8fce\u53c3\u8003 [\u9019\u7bc7\u6587\u7ae0](https:\/\/vocus.cc\/article\/6054b535fd897800059ac30e)\uff01<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>### Q1: `__new__()` \u548c `__init__()` \u6709\u4f55\u4e0d\u540c\uff1f<br \/>\nA: `__new__()` \u8ca0\u8cac\u5275\u5efa\u7269\u4ef6\uff0c\u800c `__init__()` \u8ca0\u8cac\u521d\u59cb\u5316\u7269\u4ef6\u7684\u5c6c\u6027\u3002\u901a\u5e38\uff0c`__new__()` \u5728\u7269\u4ef6\u5275\u5efa\u4e4b\u524d\u57f7\u884c\uff0c\u800c `__init__()` \u5728\u7269\u4ef6\u5275\u5efa\u4e4b\u5f8c\u57f7\u884c\u3002<\/p>\n<p>### Q2: \u4ec0\u9ebc\u6642\u5019\u9700\u8981\u4f7f\u7528 `__new__()` \u65b9\u6cd5\uff1f<br \/>\nA: \u7576\u4f60\u9700\u8981\u63a7\u5236\u7269\u4ef6\u7684\u5275\u5efa\u904e\u7a0b\uff0c\u4f8b\u5982\u5be6\u73fe\u55ae\u4f8b\u6a21\u5f0f\u6216\u4f7f\u7528\u5143\u985e\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528 `__new__()` \u65b9\u6cd5\u3002<\/p>\n<p>### Q3: \u4f7f\u7528 `__new__()` \u65b9\u6cd5\u6642\u9700\u8981\u6ce8\u610f\u4ec0\u9ebc\uff1f<br \/>\nA: \u78ba\u4fdd\u5728 `__new__()` \u65b9\u6cd5\u4e2d\u6b63\u78ba\u8fd4\u56de\u7269\u4ef6\u5be6\u4f8b\uff0c\u4e26\u4e14\u5728\u521d\u59cb\u5316\u5c6c\u6027\u6642\u4e0d\u4f7f\u7528\u5c1a\u672a\u8a2d\u7f6e\u7684\u5c6c\u6027\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-3088","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-NO","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3088","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=3088"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3088\/revisions"}],"predecessor-version":[{"id":3089,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3088\/revisions\/3089"}],"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=3088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}