{"id":3064,"date":"2025-05-28T16:18:01","date_gmt":"2025-06-04T09:01:00","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3064"},"modified":"2025-06-04T16:18:01","modified_gmt":"2025-06-04T09:01:00","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84repr%e5%87%bd%e6%95%b8-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84repr%e5%87%bd%e6%95%b8-2\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 repr() \u51fd\u6578\uff1a\u7528\u6cd5\u3001\u512a\u9ede\u8207\u5be6\u969b\u7bc4\u4f8b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, repr(), Python \u6559\u5b78, Python \u51fd\u6578\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 repr() \u51fd\u6578<\/h1>\n<p>\u5728 Python \u7a0b\u5f0f\u8a2d\u8a08\u4e2d\uff0c`repr()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u51fd\u6578\uff0c\u7528\u4f86\u7372\u53d6\u5c0d\u8c61\u7684\u5b57\u4e32\u8868\u793a\u3002\u9019\u4e0d\u50c5\u80fd\u5e6b\u52a9\u6211\u5011\u7406\u89e3\u5c0d\u8c61\u7684\u5167\u5bb9\uff0c\u9084\u80fd\u5728\u8abf\u8a66\u904e\u7a0b\u4e2d\u63d0\u4f9b\u6709\u7528\u7684\u8cc7\u8a0a\u3002\u672c\u6587\u5c07\u63a2\u7d22 `repr()` \u51fd\u6578\u7684\u7528\u6cd5\u3001\u512a\u9ede\u4ee5\u53ca\u5be6\u969b\u7bc4\u4f8b\uff0c\u4ee5\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u638c\u63e1\u9019\u4e00\u5de5\u5177\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f repr() \u51fd\u6578\uff1f<\/h2>\n<p>`repr()` \u51fd\u6578\u7684\u4e3b\u8981\u529f\u80fd\u662f\u8fd4\u56de\u5c0d\u8c61\u7684\u5b57\u4e32\u8868\u793a\uff0c\u901a\u5e38\u9019\u500b\u8868\u793a\u80fd\u5920\u7528\u65bc\u8abf\u8a66\u548c\u958b\u767c\u3002\u7576\u60a8\u8abf\u7528 `repr()` \u51fd\u6578\u6642\uff0c\u5b83\u6703\u8a66\u5716\u8fd4\u56de\u4e00\u500b\u53ef\u4ee5\u7528\u65bc\u91cd\u65b0\u5275\u5efa\u8a72\u5c0d\u8c61\u7684\u5b57\u4e32\u3002<\/p>\n<h2>repr() \u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<p>`repr()` \u51fd\u6578\u7684\u4f7f\u7528\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u5c07\u5c0d\u8c61\u4f5c\u70ba\u53c3\u6578\u50b3\u905e\u7d66\u5b83\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\na = [1, 2, 3]\nprint(repr(a))  # \u8f38\u51fa: [1, 2, 3]\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5c07\u4e00\u500b\u5217\u8868\u5c0d\u8c61\u50b3\u905e\u7d66 `repr()` \u51fd\u6578\uff0c\u5b83\u8fd4\u56de\u8a72\u5217\u8868\u7684\u5b57\u4e32\u8868\u793a\u3002<\/p>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u8cc7\u6599\u985e\u578b\uff0c`repr()` \u51fd\u6578\u4e5f\u53ef\u4ee5\u7528\u65bc\u81ea\u5b9a\u7fa9\u985e\u5225\u3002\u82e5\u60a8\u5e0c\u671b\u81ea\u5b9a\u7fa9\u985e\u5225\u7684\u5be6\u4f8b\u80fd\u5920\u6709\u66f4\u6709\u610f\u7fa9\u7684\u5b57\u4e32\u8868\u793a\uff0c\u53ef\u4ee5\u91cd\u5beb `__repr__` \u65b9\u6cd5\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>    def __repr__(self):<br \/>\n        return f&#8217;Person(name={self.name!r}, age={self.age!r})&#8217;<\/p>\n<p>p = Person(&#8216;Alice&#8217;, 30)<br \/>\nprint(repr(p))  # \u8f38\u51fa: Person(name=&#8217;Alice&#8217;, age=30)<br \/>\n&#8220;`<\/p>\n<h2>repr() \u51fd\u6578\u7684\u512a\u9ede<\/h2>\n<p>`repr()` \u51fd\u6578\u7684\u4e3b\u8981\u512a\u9ede\u5305\u62ec\uff1a<\/p>\n<p>1. **\u6e05\u6670\u7684\u8868\u793a**\uff1a\u5b83\u80fd\u5920\u63d0\u4f9b\u5c0d\u8c61\u7684\u6e05\u6670\u5b57\u4e32\u8868\u793a\uff0c\u65b9\u4fbf\u958b\u767c\u8005\u7406\u89e3\u5c0d\u8c61\u7684\u5167\u5bb9\u3002<br \/>\n2. **\u8abf\u8a66\u5e6b\u52a9**\uff1a\u5728\u8abf\u8a66\u904e\u7a0b\u4e2d\uff0c\u4f7f\u7528 `repr()` \u53ef\u4ee5\u8fc5\u901f\u6aa2\u67e5\u5c0d\u8c61\u7684\u72c0\u614b\u548c\u5c6c\u6027\u3002<br \/>\n3. **\u81ea\u5b9a\u7fa9\u652f\u6301**\uff1a\u901a\u904e\u91cd\u5beb `__repr__` \u65b9\u6cd5\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u70ba\u81ea\u5b9a\u7fa9\u985e\u5225\u63d0\u4f9b\u5c08\u5c6c\u7684\u5b57\u4e32\u8868\u793a\uff0c\u589e\u5f37\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `repr()` \u51fd\u6578\u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u554f\u984c\uff1a<\/p>\n<p>&#8211; **\u8fd4\u56de\u503c\u4e0d\u5982\u9810\u671f**\uff1a\u78ba\u4fdd\u60a8\u7684\u5c0d\u8c61\u6b63\u78ba\u5be6\u73fe\u4e86 `__repr__` \u65b9\u6cd5\uff0c\u5426\u5247\u5c07\u8fd4\u56de\u9ed8\u8a8d\u7684\u7269\u4ef6\u8868\u793a\u3002<br \/>\n&#8211; **\u7121\u6cd5\u8fd4\u56de\u5b8c\u6574\u8cc7\u8a0a**\uff1a\u67d0\u4e9b\u5c0d\u8c61\u53ef\u80fd\u4e0d\u652f\u6301 `repr()`\uff0c\u7279\u5225\u662f\u975e\u6a19\u6e96\u5c0d\u8c61\uff0c\u9019\u6642\u9700\u8981\u6aa2\u67e5\u5c0d\u8c61\u7684\u985e\u578b\u53ca\u5176\u5be6\u73fe\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`repr()` \u51fd\u6578\u5e38\u7528\u65bc\u65e5\u5e38\u958b\u767c\u53ca\u8abf\u8a66\u904e\u7a0b\u3002\u5c0d\u65bc\u9700\u8981\u5927\u91cf\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\uff0c\u4f7f\u7528 `repr()` \u4f86\u6aa2\u67e5\u8cc7\u6599\u72c0\u614b\u662f\u63d0\u9ad8\u958b\u767c\u6548\u7387\u7684\u826f\u597d\u505a\u6cd5\u3002\u6b64\u5916\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 `repr()` \u53ef\u4ee5\u5e6b\u52a9\u60a8\u66f4\u6df1\u5165\u5730\u7406\u89e3 Python \u7684\u8cc7\u6599\u6a21\u578b\uff0c\u9032\u800c\u63d0\u5347\u60a8\u7684\u7de8\u7a0b\u80fd\u529b\u3002<\/p>\n<p>\u82e5\u60a8\u60f3\u4e86\u89e3\u66f4\u591a Python \u51fd\u6578\u7684\u4f7f\u7528\uff0c\u6b61\u8fce\u53c3\u8003 [\u9019\u88e1\u7684\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/5e7c3b1c0d13490001b0e4f6)\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cPython \u7684 `repr()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u4e14\u5be6\u7528\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u7372\u53d6\u5c0d\u8c61\u7684\u5b57\u4e32\u8868\u793a\uff0c\u4e26\u5728\u8abf\u8a66\u904e\u7a0b\u4e2d\u63d0\u4f9b\u5fc5\u8981\u7684\u8cc7\u8a0a\u3002\u7121\u8ad6\u662f\u57fa\u672c\u8cc7\u6599\u985e\u578b\u9084\u662f\u81ea\u5b9a\u7fa9\u985e\u5225\uff0c`repr()` \u90fd\u80fd\u8b93\u6211\u5011\u66f4\u597d\u5730\u7406\u89e3\u548c\u64cd\u4f5c\u5c0d\u8c61\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: repr() \u548c str() \u6709\u4ec0\u9ebc\u5340\u5225\uff1f**<br \/>\nA1: `repr()` \u4e3b\u8981\u7528\u65bc\u7372\u53d6\u5c0d\u8c61\u7684\u6b63\u5f0f\u5b57\u4e32\u8868\u793a\uff0c\u901a\u5e38\u9069\u5408\u958b\u767c\u548c\u8abf\u8a66\uff0c\u800c `str()` \u7528\u65bc\u7372\u53d6\u5c0d\u8c61\u7684\u975e\u6b63\u5f0f\u5b57\u4e32\u8868\u793a\uff0c\u901a\u5e38\u9069\u5408\u7528\u65bc\u8f38\u51fa\u7d66\u7d42\u7aef\u7528\u6236\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8b93\u81ea\u5b9a\u7fa9\u985e\u5225\u7684\u8f38\u51fa\u66f4\u5177\u53ef\u8b80\u6027\uff1f**<br \/>\nA2: \u900f\u904e\u91cd\u5beb `__str__` \u548c `__repr__` \u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u70ba\u81ea\u5b9a\u7fa9\u985e\u5225\u63d0\u4f9b\u4e0d\u540c\u5c64\u6b21\u7684\u5b57\u4e32\u8868\u793a\uff0c\u8b93\u8f38\u51fa\u65e2\u9069\u5408\u958b\u767c\u8005\u8abf\u8a66\u53c8\u9069\u5408\u7d42\u7aef\u7528\u6236\u7406\u89e3\u3002<\/p>\n<p>**Q3: \u5728\u4f55\u6642\u61c9\u8a72\u4f7f\u7528 repr()\uff1f**<br \/>\nA3: \u7576\u60a8\u9700\u8981\u6aa2\u67e5\u5c0d\u8c61\u7684\u5167\u90e8\u72c0\u614b\u6216\u9032\u884c\u8abf\u8a66\u6642\uff0c\u4f7f\u7528 `repr()` \u662f\u975e\u5e38\u6709\u5e6b\u52a9\u7684\uff0c\u56e0\u70ba\u5b83\u63d0\u4f9b\u4e86\u66f4\u8a73\u7d30\u7684\u5c0d\u8c61\u4fe1\u606f\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684repr()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u5c07\u4efb\u4f55Python\u5c0d\u8c61\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\uff0c\u4e26\u63d0\u4f9b\u4e00\u7a2e\u53ef\u8b80\u6027\u5f37\u7684\u8868\u793a\u6cd5\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u4e86\u89e3Python\u5c0d\u8c61\u7684\u5167\u5bb9\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-3064","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-Nq","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3064","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=3064"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3064\/revisions"}],"predecessor-version":[{"id":12944,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3064\/revisions\/12944"}],"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=3064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}