{"id":3346,"date":"2025-05-30T16:03:44","date_gmt":"2025-06-04T08:09:43","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3346"},"modified":"2025-06-04T16:03:44","modified_gmt":"2025-06-04T08:09:43","slug":"python%e4%b8%ad%e7%9a%84del%e5%87%bd%e6%95%b8%ef%bc%9a%e7%b0%a1%e5%96%ae%e6%98%93%e7%94%a8%e7%9a%84%e5%88%aa%e9%99%a4%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e4%b8%ad%e7%9a%84del%e5%87%bd%e6%95%b8%ef%bc%9a%e7%b0%a1%e5%96%ae%e6%98%93%e7%94%a8%e7%9a%84%e5%88%aa%e9%99%a4%e6%96%b9%e6%b3%95\/","title":{"rendered":"Python del() \u51fd\u6578\uff1a\u5168\u9762\u638c\u63e1\u522a\u9664\u7269\u4ef6\u3001\u5217\u8868\u8207\u5b57\u5178\u7684\u65b9\u6cd5"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, del() \u51fd\u6578, \u522a\u9664\u7269\u4ef6, \u522a\u9664\u5217\u8868, \u522a\u9664\u5b57\u5178\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python del() \u51fd\u6578<\/h1>\n<p>Python \u7684 `del()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u4e14\u9748\u6d3b\u7684\u5de5\u5177\uff0c\u7528\u65bc\u522a\u9664\u7269\u4ef6\u3001\u5217\u8868\u3001\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u53ca\u5c6c\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39 `del()` \u51fd\u6578\u7684\u6700\u65b0\u8a9e\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u4e26\u63d0\u4f9b\u5be6\u4f5c\u7bc4\u4f8b\u3001\u932f\u8aa4\u6392\u9664\u65b9\u6cd5\u53ca\u5ef6\u4f38\u61c9\u7528\uff0c\u5e6b\u52a9\u4f60\u5168\u9762\u638c\u63e1\u9019\u500b\u51fd\u6578\u7684\u4f7f\u7528\u3002<\/p>\n<h2>del() \u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p>`del()` \u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\ndel object_name<br \/>\n&#8220;`<\/p>\n<p>\u9019\u88e1\u7684 `object_name` \u53ef\u4ee5\u662f\u8b8a\u6578\u3001\u5217\u8868\u7d22\u5f15\u6216\u5b57\u5178\u7684\u9375\u3002<\/p>\n<h2>\u4f7f\u7528 del() \u51fd\u6578\u522a\u9664\u7269\u4ef6\u7684\u5c6c\u6027<\/h2>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 `del()` \u51fd\u6578\u4f86\u522a\u9664\u985e\u5225\u7684\u5c6c\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Person:<br \/>\n    def __init__(self, name, age):<br \/>\n        self.name = name<br \/>\n        self.age = age<\/p>\n<p>p = Person(&#8220;John&#8221;, 30)<\/p>\n<p># \u522a\u9664 p \u7269\u4ef6\u7684 age \u5c6c\u6027<br \/>\ndel p.age<\/p>\n<p>try:<br \/>\n    print(p.age)  # \u5617\u8a66\u8a2a\u554f\u5df2\u522a\u9664\u7684\u5c6c\u6027<br \/>\nexcept AttributeError as e:<br \/>\n    print(e)  # \u8f38\u51fa\u932f\u8aa4\u8a0a\u606f<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b `Person` \u985e\u5225\u4e26\u5275\u5efa\u4e86\u4e00\u500b `Person` \u7269\u4ef6 `p`\u3002\u4f7f\u7528 `del()` \u51fd\u6578\u522a\u9664 `p` \u7269\u4ef6\u7684 `age` \u5c6c\u6027\u5f8c\uff0c\u518d\u5617\u8a66\u8a2a\u554f\u8a72\u5c6c\u6027\u6642\u6703\u5f15\u767c `AttributeError` \u932f\u8aa4\u3002<\/p>\n<h2>\u4f7f\u7528 del() \u51fd\u6578\u522a\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20<\/h2>\n<p>`del()` \u51fd\u6578\u4e5f\u53ef\u4ee5\u7528\u4f86\u522a\u9664\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nnumbers = [1, 2, 3, 4, 5]\n<p># \u522a\u9664 numbers \u5217\u8868\u4e2d\u7684\u7b2c\u4e09\u500b\u5143\u7d20<br \/>\ndel numbers[2]\n<p>print(numbers)  # \u8f38\u51fa: [1, 2, 4, 5]\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u5efa\u7acb\u4e86\u4e00\u500b `numbers` \u5217\u8868\uff0c\u7136\u5f8c\u4f7f\u7528 `del()` \u51fd\u6578\u522a\u9664\u4e86\u7b2c\u4e09\u500b\u5143\u7d20\u3002\u53ef\u4ee5\u770b\u5230\uff0c\u5217\u8868\u4e2d\u7684\u7b2c\u4e09\u500b\u5143\u7d20\u5df2\u6210\u529f\u522a\u9664\u3002<\/p>\n<h2>\u4f7f\u7528 del() \u51fd\u6578\u522a\u9664\u5b57\u5178\u4e2d\u7684\u9375\u503c\u5c0d<\/h2>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 `del()` \u51fd\u6578\u4f86\u522a\u9664\u5b57\u5178\u4e2d\u7684\u9375\u503c\u5c0d\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nperson = {<br \/>\n    &#8220;name&#8221;: &#8220;John&#8221;,<br \/>\n    &#8220;age&#8221;: 30<br \/>\n}<\/p>\n<p># \u522a\u9664 person \u5b57\u5178\u4e2d\u7684 age \u9375\u503c\u5c0d<br \/>\ndel person[&#8220;age&#8221;]\n<p>print(person)  # \u8f38\u51fa: {&#8216;name&#8217;: &#8216;John&#8217;}<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5efa\u7acb\u4e86\u4e00\u500b `person` \u5b57\u5178\uff0c\u4e26\u4f7f\u7528 `del()` \u51fd\u6578\u522a\u9664\u4e86 `age` \u9375\u503c\u5c0d\u3002\u6700\u5f8c\u8f38\u51fa\u7684\u5b57\u5178\u4e2d\u4e0d\u518d\u5305\u542b `age`\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528 `del()` \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<br \/>\n&#8211; `KeyError`: \u5617\u8a66\u522a\u9664\u4e0d\u5b58\u5728\u7684\u5b57\u5178\u9375\u3002<br \/>\n&#8211; `IndexError`: \u5617\u8a66\u522a\u9664\u4e0d\u5b58\u5728\u7684\u5217\u8868\u7d22\u5f15\u3002<\/p>\n<p>\u5efa\u8b70\u5728\u522a\u9664\u4e4b\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528 `in` \u95dc\u9375\u5b57\u6aa2\u67e5\u9375\u6216\u7d22\u5f15\u662f\u5426\u5b58\u5728\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nif &#8220;age&#8221; in person:<br \/>\n    del person[&#8220;age&#8221;]\nelse:<br \/>\n    print(&#8220;\u9375\u4e0d\u5b58\u5728&#8221;)<br \/>\n&#8220;`<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`del()` \u51fd\u6578\u4e0d\u50c5\u9650\u65bc\u522a\u9664\u55ae\u4e00\u5143\u7d20\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u6e05\u7a7a\u6574\u500b\u5217\u8868\u6216\u5b57\u5178\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u6e05\u7a7a\u5217\u8868<br \/>\nnumbers.clear()<\/p>\n<p># \u6e05\u7a7a\u5b57\u5178<br \/>\nperson.clear()<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u8a18\u61b6\u9ad4\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8cc7\u6e90\u6d6a\u8cbb\u3002<\/p>\n<p>\u5728 2025 \u5e74\uff0c\u96a8\u8457 Python \u8a9e\u8a00\u7684\u6301\u7e8c\u6f14\u9032\uff0c\u78ba\u4fdd\u4f60\u6301\u7e8c\u95dc\u6ce8\u66f4\u65b0\u548c\u6700\u4f73\u5be6\u8e10\uff0c\u4e26\u63a2\u7d22\u66f4\u591a Python \u7684\u5f37\u5927\u529f\u80fd\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u5b78\u7fd2\u66f4\u591a Python \u7684\u6280\u5de7\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7 [Python \u6559\u5b78\u6587](https:\/\/vocus.cc\/article\/605c9b4afdc9ea001c3ba8a4)\uff0c\u7372\u53d6\u6700\u65b0\u8cc7\u8a0a\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: del() \u51fd\u6578\u53ef\u4ee5\u522a\u9664\u6240\u6709\u985e\u578b\u7684\u7269\u4ef6\u55ce\uff1f**<br \/>\nA1: \u662f\u7684\uff0c`del()` \u51fd\u6578\u53ef\u4ee5\u522a\u9664\u8b8a\u6578\u3001\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3001\u5b57\u5178\u4e2d\u7684\u9375\u503c\u5c0d\u7b49\u591a\u7a2e\u7269\u4ef6\u3002<\/p>\n<p>**Q2: \u4f7f\u7528 del() \u51fd\u6578\u6703\u91cb\u653e\u8a18\u61b6\u9ad4\u55ce\uff1f**<br \/>\nA2: \u662f\u7684\uff0c\u7576\u522a\u9664\u4e00\u500b\u7269\u4ef6\u5f8c\uff0cPython \u7684\u5783\u573e\u56de\u6536\u6a5f\u5236\u5c07\u6709\u6a5f\u6703\u91cb\u653e\u8a72\u7269\u4ef6\u4f54\u7528\u7684\u8a18\u61b6\u9ad4\u3002<\/p>\n<p>**Q3: \u6709\u4ec0\u9ebc\u65b9\u6cd5\u53ef\u4ee5\u5b89\u5168\u5730\u522a\u9664\u5b57\u5178\u7684\u9375\u503c\u5c0d\uff1f**<br \/>\nA3: \u4f7f\u7528 `pop()` \u65b9\u6cd5\u53ef\u4ee5\u5b89\u5168\u5730\u522a\u9664\u5b57\u5178\u7684\u9375\u503c\u5c0d\uff0c\u82e5\u9375\u4e0d\u5b58\u5728\u5247\u4e0d\u6703\u5f15\u767c\u932f\u8aa4\u3002<\/p>\n<p>\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5c0d\u4f60\u7406\u89e3 `del()` \u51fd\u6578\u6709\u6240\u5e6b\u52a9\uff0c\u4e26\u63d0\u5347\u4f60\u7684 Python \u7a0b\u5f0f\u8a2d\u8a08\u6280\u80fd\uff01<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>del()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u51fd\u6578\uff0c\u53ef\u4ee5\u7528\u65bc\u522a\u9664\u5c0d\u8c61\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\uff0c\u64cd\u4f5c\u7c21\u55ae\u6613\u7528\uff0c\u672c\u6587\u5c07\u4ecb\u7d39del()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528del()\u51fd\u6578\u4f86\u6e05\u7406Python\u7a0b\u5e8f\u7684\u5783\u573e\u6578\u64da\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-3346","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-RY","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3346","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=3346"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3346\/revisions"}],"predecessor-version":[{"id":12862,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3346\/revisions\/12862"}],"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=3346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}