{"id":3148,"date":"2025-05-31T16:31:14","date_gmt":"2025-06-04T08:47:14","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3148"},"modified":"2025-06-04T16:31:14","modified_gmt":"2025-06-04T08:47:14","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-2","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-2\/","title":{"rendered":"2025 \u6700\u65b0\uff1aPython \u4e2d\u7684\u7269\u4ef6\u5275\u5efa\u65b9\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, new(), \u7269\u4ef6\u5275\u5efa, Python \u6559\u5b78\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684\u7269\u4ef6\u5275\u5efa\u65b9\u6cd5<\/h1>\n<p>\u5728 Python \u4e2d\uff0c\u5275\u5efa\u7269\u4ef6\u7684\u65b9\u5f0f\u591a\u7a2e\u591a\u6a23\uff0c\u5176\u4e2d\u4e00\u7a2e\u65b9\u6cd5\u662f\u4f7f\u7528 `__new__()` \u51fd\u5f0f\u3002\u96d6\u7136\u5728\u65e9\u671f\u7684 Python \u7248\u672c\u4e2d\uff0c\u9019\u500b\u51fd\u5f0f\u5e38\u88ab\u63d0\u53ca\uff0c\u4f46\u5728\u7576\u524d\u7684 Python 3.x \u4e2d\uff0c\u4f7f\u7528 `__new__()` \u4f86\u5275\u5efa\u7269\u4ef6\u7684\u60c5\u6cc1\u4e26\u4e0d\u5e38\u898b\u3002\u901a\u5e38\uff0c\u6211\u5011\u6703\u76f4\u63a5\u4f7f\u7528\u985e\u5225\u7684\u69cb\u9020\u51fd\u5f0f\u4f86\u5be6\u4f8b\u5316\u7269\u4ef6\u3002\u4ee5\u4e0b\u5c07\u4ecb\u7d39\u6700\u65b0\u7684\u7269\u4ef6\u5275\u5efa\u65b9\u6cd5\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u793a\u4f8b\u3002<\/p>\n<p>## Python \u7684\u7269\u4ef6\u5275\u5efa<\/p>\n<p>\u5728 Python \u4e2d\uff0c\u7269\u4ef6\u5275\u5efa\u7684\u57fa\u672c\u65b9\u6cd5\u662f\u76f4\u63a5\u8abf\u7528\u985e\u5225\u540d\u7a31\uff0c\u9032\u800c\u4f7f\u7528\u69cb\u9020\u51fd\u5f0f `__init__()` \u4f86\u521d\u59cb\u5316\u7269\u4ef6\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u7269\u4ef6\u7684\u57fa\u672c\u8a9e\u6cd5\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5b9a\u7fa9\u4e00\u500b\u985e\u5225<br \/>\nclass MyClass:<br \/>\n    def __init__(self, arg):<br \/>\n        self.arg = arg<\/p>\n<p># \u5275\u5efa\u4e00\u500b\u65b0\u7684\u7269\u4ef6<br \/>\nobj = MyClass(&#8216;Hello World&#8217;)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba `MyClass` \u7684\u985e\u5225\uff0c\u4e26\u4f7f\u7528 `__init__()` \u51fd\u5f0f\u4f86\u63a5\u6536\u53c3\u6578 `arg`\u3002\u63a5\u8457\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u540d\u70ba `obj` \u7684\u7269\u4ef6\uff0c\u4e26\u50b3\u5165\u5b57\u7b26\u4e32 `&#8217;Hello World&#8217;` \u4f5c\u70ba\u53c3\u6578\u3002<\/p>\n<p>## \u4f7f\u7528 `__new__()` \u51fd\u5f0f\u5275\u5efa\u7269\u4ef6<\/p>\n<p>\u96d6\u7136\u901a\u5e38\u4e0d\u9700\u8981\u4f7f\u7528 `__new__()`\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u4f8b\u5982\u9700\u8981\u63a7\u5236\u7269\u4ef6\u7684\u5275\u5efa\u904e\u7a0b\u6642\uff0c\u6211\u5011\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u5b83\u3002`__new__()` \u662f\u4e00\u500b\u975c\u614b\u65b9\u6cd5\uff0c\u8ca0\u8cac\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u7269\u4ef6\u5be6\u4f8b\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 `__new__()` \u7684\u7bc4\u4f8b\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<\/p>\n<p>    def __init__(self, arg):<br \/>\n        self.arg = arg<\/p>\n<p># \u4f7f\u7528 __new__() \u5275\u5efa\u7269\u4ef6<br \/>\nobj = MyClass(&#8216;Hello World&#8217;)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c`__new__()` \u9996\u5148\u5275\u5efa\u4e00\u500b\u65b0\u7684\u5be6\u4f8b\uff0c\u7136\u5f8c\u8fd4\u56de\u9019\u500b\u5be6\u4f8b\u3002\u63a5\u4e0b\u4f86\uff0c`__init__()` \u7528\u65bc\u521d\u59cb\u5316\u9019\u500b\u5be6\u4f8b\u7684\u5c6c\u6027\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/p>\n<p>\u5728\u4f7f\u7528 `__new__()` \u548c `__init__()` \u6642\uff0c\u5e38\u898b\u932f\u8aa4\u5305\u62ec\uff1a<\/p>\n<p>1. **\u672a\u6b63\u78ba\u8fd4\u56de\u5be6\u4f8b**\uff1a\u78ba\u4fdd\u5728 `__new__()` \u4e2d\u8fd4\u56de\u5be6\u4f8b\u3002<br \/>\n2. **\u6df7\u7528\u985e\u5225\u8207\u5be6\u4f8b\u65b9\u6cd5**\uff1a`__new__()` \u662f\u7528\u65bc\u985e\u5225\uff0c\u800c `__init__()` \u7528\u65bc\u5be6\u4f8b\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u5728\u8a2d\u8a08\u8907\u96dc\u7684\u7269\u4ef6\u7cfb\u7d71\u6642\uff0c\u7406\u89e3 `__new__()` \u548c `__init__()` \u7684\u5dee\u7570\u4ee5\u53ca\u4f55\u6642\u4f7f\u7528\u5b83\u5011\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u9019\u6a23\u53ef\u4ee5\u8b93\u4f60\u7684\u4ee3\u78bc\u66f4\u52a0\u9748\u6d3b\uff0c\u4e26\u80fd\u6eff\u8db3\u7279\u5b9a\u9700\u6c42\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u5b78\u7fd2 Python \u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08\u7684\u6700\u4f73\u5be6\u8e10\uff0c\u5efa\u8b70\u53c3\u8003\u9019\u7bc7\u8a73\u76e1\u7684\u6559\u5b78\u6587\u7ae0\uff1a[Python \u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08\u6307\u5357](https:\/\/vocus.cc) \u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>### Q1: \u4ec0\u9ebc\u662f `__new__()` \u548c `__init__()` \u7684\u5340\u5225\uff1f<br \/>\nA1: `__new__()` \u662f\u7528\u4f86\u5275\u5efa\u7269\u4ef6\u7684\u975c\u614b\u65b9\u6cd5\uff0c\u800c `__init__()` \u5247\u662f\u7528\u4f86\u521d\u59cb\u5316\u7269\u4ef6\u7684\u5be6\u4f8b\u65b9\u6cd5\u3002<\/p>\n<p>### Q2: \u5728\u4ec0\u9ebc\u60c5\u6cc1\u4e0b\u9700\u8981\u4f7f\u7528 `__new__()`\uff1f<br \/>\nA2: \u7576\u4f60\u9700\u8981\u63a7\u5236\u7269\u4ef6\u7684\u5275\u5efa\u904e\u7a0b\uff0c\u4f8b\u5982\u4f7f\u7528\u55ae\u4f8b\u6a21\u5f0f\u6642\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 `__new__()`\u3002<\/p>\n<p>### Q3: \u5982\u4f55\u8655\u7406\u7269\u4ef6\u5275\u5efa\u904e\u7a0b\u4e2d\u7684\u932f\u8aa4\uff1f<br \/>\nA3: \u78ba\u4fdd `__new__()` \u6b63\u78ba\u8fd4\u56de\u5be6\u4f8b\uff0c\u4e26\u5728 `__init__()` \u4e2d\u6aa2\u67e5\u50b3\u5165\u7684\u53c3\u6578\u662f\u5426\u7b26\u5408\u9810\u671f\uff0c\u4ee5\u9632\u6b62\u521d\u59cb\u5316\u5931\u6557\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u9019\u6a23\u7684\u512a\u5316\u4e0d\u50c5\u66f4\u65b0\u4e86\u8a9e\u6cd5\uff0c\u9084\u589e\u5f37\u4e86\u5167\u5bb9\u7684\u5be6\u7528\u6027\u548cSEO\u6548\u679c\u3002\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3 Python \u7684\u7269\u4ef6\u5275\u5efa\u65b9\u6cd5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u65b0\u7279\u6027new()\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5275\u5efa\u7269\u4ef6\uff0c\u63d0\u9ad8\u958b\u767c\u6548\u7387\uff0c\u4e26\u6e1b\u5c11\u7a0b\u5f0f\u78bc\u7684\u8907\u96dc\u5ea6\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528new()\u4f86\u5275\u5efa\u7269\u4ef6\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\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-3148","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-OM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3148","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=3148"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3148\/revisions"}],"predecessor-version":[{"id":3149,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3148\/revisions\/3149"}],"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=3148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}