{"id":3504,"date":"2025-06-03T15:27:43","date_gmt":"2025-06-04T07:30:42","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3504"},"modified":"2025-06-04T15:27:43","modified_gmt":"2025-06-04T07:30:42","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84methods%e5%87%bd%e6%95%b8%e5%8a%9f%e8%83%bd%ef%bc%8c%e6%8f%90%e5%8d%87%e4%bd%a0%e7%9a%84%e7%a8%8b%e5%bc%8f%e8%83%bd%e5%8a%9b","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84methods%e5%87%bd%e6%95%b8%e5%8a%9f%e8%83%bd%ef%bc%8c%e6%8f%90%e5%8d%87%e4%bd%a0%e7%9a%84%e7%a8%8b%e5%bc%8f%e8%83%bd%e5%8a%9b\/","title":{"rendered":"2025 \u6700\u65b0 Python \u7a0b\u5f0f\u6559\u5b78\uff1a\u6df1\u5165\u4e86\u89e3 `__new__` \u65b9\u6cd5\u53ca\u5176\u61c9\u7528"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, __new__, \u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08, Python \u7a0b\u5f0f\u6559\u5b78\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 `__new__` \u65b9\u6cd5\u53ca\u5176\u61c9\u7528<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`__new__` \u65b9\u6cd5\u662f\u4e00\u500b\u91cd\u8981\u7684\u985e\u5225\u65b9\u6cd5\uff0c\u5b83\u8ca0\u8cac\u5275\u5efa\u4e00\u500b\u65b0\u7684\u7269\u4ef6\u5be6\u4f8b\u3002\u8207 `__init__` \u65b9\u6cd5\u4e0d\u540c\uff0c`__new__` \u662f\u4e00\u500b\u975c\u614b\u65b9\u6cd5\uff0c\u4e3b\u8981\u7528\u65bc\u63a7\u5236\u7269\u4ef6\u7684\u5275\u5efa\u904e\u7a0b\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u4ecb\u7d39 `__new__` \u65b9\u6cd5\u7684\u4f7f\u7528\u65b9\u5f0f\u3001\u5be6\u4f5c\u7bc4\u4f8b\u4ee5\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u4f60\u63d0\u5347\u7a0b\u5f0f\u80fd\u529b\u3002<\/p>\n<p>## `__new__` \u65b9\u6cd5\u7684\u57fa\u672c\u7528\u6cd5<\/p>\n<p>`__new__` \u65b9\u6cd5\u7684\u57fa\u672c\u8a9e\u6cd5\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>### \u5275\u5efa\u4e00\u500b\u65b0\u7684\u7269\u4ef6<\/p>\n<p>\u4e0b\u9762\u7684\u7bc4\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 `__new__` \u65b9\u6cd5\u5275\u5efa\u4e00\u500b\u65b0\u7684\u7269\u4ef6\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __new__(cls, arg1, arg2):<br \/>\n        instance = super(MyClass, cls).__new__(cls)<br \/>\n        instance.arg1 = arg1<br \/>\n        instance.arg2 = arg2<br \/>\n        return instance<\/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\u4e0a\u8ff0\u7bc4\u4f8b\u4e2d\uff0c`MyClass` \u5b9a\u7fa9\u4e86 `__new__` \u65b9\u6cd5\uff0c\u4e26\u5728\u5176\u4e2d\u5275\u5efa\u4e86\u4e00\u500b\u65b0\u7684\u5be6\u4f8b\uff0c\u7136\u5f8c\u5c07\u5c6c\u6027 `arg1` \u548c `arg2` \u8a2d\u7f6e\u70ba\u50b3\u5165\u7684\u53c3\u6578\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `__new__` \u65b9\u6cd5\u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<\/p>\n<p>1. **\u672a\u6b63\u78ba\u8abf\u7528\u7236\u985e\u7684 `__new__` \u65b9\u6cd5**\uff1a\u78ba\u4fdd\u5728 `__new__` \u65b9\u6cd5\u4e2d\u8abf\u7528 `super()` \u4ee5\u5275\u5efa\u65b0\u7684\u5be6\u4f8b\u3002<br \/>\n2. **\u5fd8\u8a18\u8fd4\u56de\u65b0\u7684\u5be6\u4f8b**\uff1a\u5fc5\u9808\u8fd4\u56de\u901a\u904e `__new__` \u5275\u5efa\u7684\u5be6\u4f8b\uff0c\u5426\u5247\u7121\u6cd5\u6b63\u78ba\u4f7f\u7528\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`__new__` \u65b9\u6cd5\u5728\u8a31\u591a\u8a2d\u8a08\u6a21\u5f0f\u4e2d\u90fd\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u55ae\u4f8b\u6a21\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u55ae\u4f8b\u6a21\u5f0f\u5be6\u73fe\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># \u6e2c\u8a66\u55ae\u4f8b<br \/>\nsingleton1 = Singleton()<br \/>\nsingleton2 = Singleton()<\/p>\n<p>print(singleton1 is singleton2)  # \u8f38\u51fa\uff1aTrue<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u78ba\u4fdd\u4e86 `Singleton` \u985e\u5225\u53ea\u6709\u4e00\u500b\u5be6\u4f8b\u5b58\u5728\u3002<\/p>\n<p>## \u7e3d\u7d50<\/p>\n<p>Python \u7684 `__new__` \u65b9\u6cd5\u70ba\u7269\u4ef6\u5275\u5efa\u63d0\u4f9b\u4e86\u9748\u6d3b\u7684\u63a7\u5236\u65b9\u5f0f\u3002\u638c\u63e1\u5176\u4f7f\u7528\u53ef\u4ee5\u5e6b\u52a9\u4f60\u5728\u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08\u4e2d\u66f4\u6709\u6548\u5730\u7ba1\u7406\u7269\u4ef6\u5be6\u4f8b\u3002<\/p>\n<p>\u5982\u679c\u60f3\u8981\u66f4\u6df1\u5165\u4e86\u89e3 Python \u7684\u5176\u4ed6\u9032\u968e\u4e3b\u984c\uff0c\u8acb\u53c3\u8003\u6211\u5011\u7684 [Vocus Python \u6559\u5b78](https:\/\/vocus.cc)\uff0c\u90a3\u88e1\u6709\u66f4\u591a\u6709\u7528\u7684\u8cc7\u6e90\u8207\u6587\u7ae0\u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>### Q1: `__new__` \u65b9\u6cd5\u8207 `__init__` \u65b9\u6cd5\u6709\u4ec0\u9ebc\u5340\u5225\uff1f<br \/>\nA1: `__new__` \u65b9\u6cd5\u8ca0\u8cac\u5275\u5efa\u7269\u4ef6\u5be6\u4f8b\uff0c\u800c `__init__` \u65b9\u6cd5\u5247\u7528\u65bc\u521d\u59cb\u5316\u5df2\u5275\u5efa\u7684\u7269\u4ef6\u3002`__new__` \u662f\u975c\u614b\u65b9\u6cd5\uff0c`__init__` \u662f\u5be6\u4f8b\u65b9\u6cd5\u3002<\/p>\n<p>### Q2: \u4ec0\u9ebc\u6642\u5019\u61c9\u8a72\u4f7f\u7528 `__new__` \u65b9\u6cd5\uff1f<br \/>\nA2: \u7576\u4f60\u9700\u8981\u63a7\u5236\u7269\u4ef6\u7684\u5275\u5efa\u904e\u7a0b\u6216\u9700\u8981\u5be6\u73fe\u55ae\u4f8b\u6a21\u5f0f\u7b49\u8a2d\u8a08\u6a21\u5f0f\u6642\uff0c\u61c9\u8a72\u4f7f\u7528 `__new__` \u65b9\u6cd5\u3002<\/p>\n<p>### Q3: \u5982\u4f55\u907f\u514d\u5728 `__new__` \u65b9\u6cd5\u4e2d\u51fa\u73fe\u932f\u8aa4\uff1f<br \/>\nA3: \u78ba\u4fdd\u6b63\u78ba\u8abf\u7528\u7236\u985e\u7684 `__new__` \u65b9\u6cd5\u4e26\u8fd4\u56de\u5275\u5efa\u7684\u5be6\u4f8b\uff0c\u6b64\u5916\uff0c\u9032\u884c\u5145\u5206\u7684\u6e2c\u8a66\u4ee5\u6aa2\u67e5\u7269\u4ef6\u7684\u5275\u5efa\u908f\u8f2f\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684methods()\u51fd\u6578\u529f\u80fd\uff0c\u53ef\u4ee5\u8b93\u4f60\u66f4\u6709\u6548\u7387\u7684\u958b\u767c\u7a0b\u5f0f\uff0c\u63d0\u5347\u4f60\u7684\u7a0b\u5f0f\u80fd\u529b\uff0c\u8b93\u4f60\u66f4\u5bb9\u6613\u89e3\u6c7a\u554f\u984c\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-3504","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-Uw","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3504","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=3504"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3504\/revisions"}],"predecessor-version":[{"id":3505,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3504\/revisions\/3505"}],"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=3504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}