{"id":100,"date":"2026-08-06T00:29:50","date_gmt":"2026-08-05T16:29:50","guid":{"rendered":"https:\/\/ltrainyard.icu\/?p=100"},"modified":"2026-08-06T00:44:40","modified_gmt":"2026-08-05T16:44:40","slug":"day-17-%e9%ad%94%e6%b3%95%e6%96%b9%e6%b3%95%ef%bc%9a__str__-__eq__-__lt__-%e4%b8%8e%e8%bf%90%e7%ae%97%e7%ac%a6%e9%87%8d%e8%bd%bd","status":"publish","type":"post","link":"https:\/\/ltrainyard.icu\/index.php\/2026\/08\/06\/day-17-%e9%ad%94%e6%b3%95%e6%96%b9%e6%b3%95%ef%bc%9a__str__-__eq__-__lt__-%e4%b8%8e%e8%bf%90%e7%ae%97%e7%ac%a6%e9%87%8d%e8%bd%bd\/","title":{"rendered":"| Day 17 | \u9b54\u6cd5\u65b9\u6cd5\uff1a__str__ \/ __eq__ \/ __lt__ \u4e0e\u8fd0\u7b97\u7b26\u91cd\u8f7d |"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1 \u9b54\u6cd5\u65b9\u6cd5\u662f\u4ec0\u4e48 \u2014\u2014 \u540d\u5b57\u5e26\u300c\u9b54\u6cd5\u300d\uff0c\u80cc\u540e\u662f\u7ea6\u5b9a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u9b54\u6cd5\u65b9\u6cd5<\/strong>\uff08\u4e5f\u53eb\u300c\u7279\u6b8a\u65b9\u6cd5\u300d\uff09\u662f<strong>\u540d\u5b57\u524d\u540e\u5404\u4e24\u4e2a\u4e0b\u5212\u7ebf<\/strong>\u7684\u65b9\u6cd5\uff0c\u5982 <code>__init__<\/code>\u3001<code>__str__<\/code>\u3001<code>__eq__<\/code>\u3002\u5b83\u4eec\u4e0d\u7528\u4f60\u624b\u52a8\u8c03\u7528\uff0cPython \u5728\u7279\u5b9a\u573a\u666f\u4e0b<strong>\u81ea\u52a8\u89e6\u53d1<\/strong>\uff1a<code>print(obj)<\/code> \u81ea\u52a8\u627e <code>__str__<\/code>\uff0c<code>obj1 == obj2<\/code> \u81ea\u52a8\u627e <code>__eq__<\/code>\uff0c<code>len(obj)<\/code> \u81ea\u52a8\u627e <code>__len__<\/code>\u3002\u5b66\u4f1a\u9b54\u6cd5\u65b9\u6cd5\uff0c\u4f60\u7684\u5bf9\u8c61\u5c31\u80fd\u652f\u6301 <code>==<\/code>\u3001<code>&lt;<\/code>\u3001<code>len()<\/code>\u3001<code>+<\/code>\u3001<code>in<\/code> \u8fd9\u4e9b\u300c\u5185\u7f6e\u5f85\u9047\u300d\u2014\u2014\u4ee3\u7801\u5199\u8d77\u6765\u548c\u7528 Python \u5185\u7f6e\u7c7b\u578b\u4e00\u6837\u81ea\u7136\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9b54\u6cd5\u65b9\u6cd5\u672c\u8d28\u662f<strong>\u7ea6\u5b9a<\/strong>\uff1a\u89e3\u91ca\u5668\u5728\u5b9e\u73b0 <code>str()<\/code>\u3001<code>len()<\/code>\u3001<code>==<\/code>\u3001<code>+<\/code>\u3001<code>in<\/code> \u7b49\u5185\u7f6e\u64cd\u4f5c\u65f6\uff0c\u5185\u90e8\u90fd\u4f1a\u53bb\u67e5\u5bf9\u8c61\u7684\u7c7b\u91cc\u6709\u6ca1\u6709\u5bf9\u5e94\u7684 <code>__xxx__<\/code> \u65b9\u6cd5\uff0c\u6709\u5c31\u8c03\u7528\uff0c\u6ca1\u6709\u5c31\u62a5\u9519\u6216\u8d70\u9ed8\u8ba4\u884c\u4e3a\u2014\u2014\u6bd4\u5982 <code>len(obj)<\/code> \u7684\u5b9e\u73b0\u5927\u81f4\u662f <code>obj.__len__()<\/code>\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u300c\u4e00\u5207\u7686\u5bf9\u8c61\u300d\uff1a<strong>\u8bed\u6cd5\u5c42\u9762\u7684\u4e8b\u4ef6\uff0c\u5e95\u5c42\u90fd\u662f\u65b9\u6cd5\u8c03\u7528<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9b54\u6cd5\u65b9\u6cd5\u50cf<strong>\u9152\u5e97\u623f\u95f4\u7684\u95e8\u724c\u7ea6\u5b9a<\/strong>\uff1a\u95e8\u4e0a\u5199\u7740\u300c\u8bf7\u52ff\u6253\u6270\u300d\uff08\u7ea6\u5b9a\uff09\uff0c\u670d\u52a1\u5458\u8def\u8fc7\u5c31\u81ea\u52a8\u4e0d\u6572\u95e8\uff1b\u95e8\u4e0a\u5199\u7740\u300c\u8bf7\u6253\u626b\u300d\uff0c\u670d\u52a1\u5458\u8def\u8fc7\u5c31\u81ea\u52a8\u8fdb\u6765\u3002\u4f60\u4e0d\u9700\u8981\u558a\u4f4f\u6bcf\u4e2a\u670d\u52a1\u5458\u4ea4\u4ee3\u4e00\u904d\u2014\u2014<strong>\u6302\u5bf9\u724c\u5b50\uff0c\u670d\u52a1\u81ea\u52a8\u53d1\u751f<\/strong>\u3002\u53cc\u4e0b\u5212\u7ebf\u5c31\u662f\u90a3\u5757\u724c\u5b50\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u516d\u4e2a\u5e38\u89c1\u9b54\u6cd5\u65b9\u6cd5\uff0c\u770b\u770b\u5b83\u4eec\u5206\u522b\u5728\u4ec0\u4e48\u573a\u666f\u88ab\u81ea\u52a8\u8c03\u7528<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Demo:\n    def __init__(self, value):\n        self.value = value          # \u521b\u5efa\u5bf9\u8c61\u65f6\u81ea\u52a8\u8c03\u7528\n\n    def __str__(self):\n        return f\"Demo(\u503c={self.value})\"   # print\/str \u65f6\u81ea\u52a8\u8c03\u7528\n\n    def __eq__(self, other):\n        return self.value == other.value  # == \u65f6\u81ea\u52a8\u8c03\u7528\n\n    def __len__(self):\n        return self.value                 # len() \u65f6\u81ea\u52a8\u8c03\u7528\n\n    def __contains__(self, item):\n        return item in str(self.value)    # in \u65f6\u81ea\u52a8\u8c03\u7528\n\nd1 = Demo(10)\nd2 = Demo(10)\nprint(d1)                # \u89e6\u53d1 __str__\nprint(d1 == d2)          # \u89e6\u53d1 __eq__\nprint(len(d1))           # \u89e6\u53d1 __len__\nprint(\"1\" in d1)         # \u89e6\u53d1 __contains__\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Demo(\u503c=10)\nTrue\n10\nTrue\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6574\u4e2a\u7a0b\u5e8f\u91cc<strong>\u6ca1\u6709\u4e00\u884c\u624b\u52a8\u8c03\u7528\u9b54\u6cd5\u65b9\u6cd5<\/strong>\uff0c\u5168\u662f Python \u81ea\u52a8\u89e6\u53d1\u3002\u8fd9\u5c31\u662f\u9b54\u6cd5\u65b9\u6cd5\u7684\u5168\u90e8\u79d8\u5bc6\uff1a<strong>\u4f60\u53ea\u7ba1\u6309\u7ea6\u5b9a\u5b9a\u4e49\uff0cPython \u8d1f\u8d23\u5728\u5408\u9002\u65f6\u673a\u8c03\u7528<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u6ca1\u5b9a\u4e49\u5bf9\u5e94\u9b54\u6cd5\u65b9\u6cd5\u65f6\uff0cPython \u4f1a\u600e\u6837<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Empty:\n    pass\n\ne = Empty()\n# print(len(e))    # \u4f1a\u62a5\u9519\uff1aTypeError: object of type 'Empty' has no len()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>len(e)<\/code> \u53bb\u627e <code>__len__<\/code>\uff0c\u6ca1\u627e\u5230\uff0c\u4e8e\u662f\u629b\u51fa <code>TypeError<\/code>\u2014\u2014<strong>\u5185\u7f6e\u64cd\u4f5c\u53ea\u4f1a\u8d70\u9b54\u6cd5\u65b9\u6cd5\u8fd9\u4e00\u6761\u8def<\/strong>\u3002\u770b\u5230\u8fd9\u7c7b\u62a5\u9519\uff0c\u7b2c\u4e00\u53cd\u5e94\u5c31\u662f\u53bb\u8865\u5bf9\u5e94\u7684\u9b54\u6cd5\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u628a\u9b54\u6cd5\u65b9\u6cd5\u540d\u62fc\u9519\uff08<code>__str_<\/code> \u5c11\u4e00\u4e2a\u4e0b\u5212\u7ebf\uff09 \u2192 \u95ee\u9898\uff1a\u4e0d\u62a5\u9519\uff01\u4f46\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u81ea\u52a8\u8c03\u7528\uff0c\u884c\u4e3a\u300c\u9759\u9ed8\u5931\u6548\u300d \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u524d\u540e\u5404\u4e24\u4e2a\u4e0b\u5212\u7ebf\uff0c\u5199\u5b8c\u6570\u4e00\u904d\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u4ee5\u4e3a\u9b54\u6cd5\u65b9\u6cd5\u8981\u624b\u52a8\u8c03\u7528\uff1a<code>obj.__str__()<\/code> \u2192 \u95ee\u9898\uff1a\u80fd\u7528\u4f46\u4e0d\u89c4\u8303\uff0c\u5931\u53bb\u4e86\u300c\u81ea\u52a8\u89e6\u53d1\u300d\u7684\u610f\u4e49 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528 <code>str(obj)<\/code>\u3001<code>print(obj)<\/code>\uff0c\u8ba9 Python \u81ea\u52a8\u627e\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5b9a\u4e49\u9b54\u6cd5\u65b9\u6cd5\u5374\u4e0d return \u6b63\u786e\u7c7b\u578b \u2192 \u95ee\u9898\uff1a\u62a5 <code>TypeError: __str__ returned non-string<\/code> \u7b49 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u6bcf\u79cd\u9b54\u6cd5\u65b9\u6cd5\u6709\u7ea6\u5b9a\u8fd4\u56de\u7c7b\u578b\uff1a<code>__str__<\/code> \u8fd4\u56de\u5b57\u7b26\u4e32\u3001<code>__eq__<\/code> \u8fd4\u56de\u5e03\u5c14\u2026\u2026<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u53cc\u4e0b\u5212\u7ebf\u662f\u7ea6\u5b9a\uff0cPython \u81ea\u52a8\u6765\u89e6\u53d1\uff1b\u9b54\u6cd5\u65b9\u6cd5\u4e0d\u7528\u8c03\uff0c\u5185\u7f6e\u64cd\u4f5c\u8d70\u8fd9\u6761\u8def\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2 <strong>str<\/strong> \u4e0e <strong>repr<\/strong> \u2014\u2014 \u300c\u7ed9\u4eba\u770b\u300d\u4e0e\u300c\u7ed9\u8c03\u8bd5\u770b\u300d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>__str__<\/code> \u9762\u5411<strong>\u6700\u7ec8\u7528\u6237<\/strong>\uff1a<code>print()<\/code>\u3001<code>str()<\/code> \u8c03\u7528\uff0c\u8ffd\u6c42\u53cb\u597d\u53ef\u8bfb\uff1b<code>__repr__<\/code> \u9762\u5411<strong>\u5f00\u53d1\u8005<\/strong>\uff1a\u4ea4\u4e92\u5f0f\u7ec8\u7aef\u76f4\u63a5\u8f93\u5bf9\u8c61\u540d\u65f6\u663e\u793a\uff0c\u8ffd\u6c42\u300c\u660e\u786e\u3001\u65e0\u6b67\u4e49\u300d\uff0c\u6700\u597d\u80fd\u770b\u51fa\u8fd9\u4e2a\u5bf9\u8c61\u662f\u600e\u4e48\u6784\u9020\u51fa\u6765\u7684\u3002\u5c31\u50cf<strong>\u7528\u6237\u8bf4\u660e\u4e66 vs \u7ef4\u4fee\u5de5\u5355<\/strong>\uff1a\u540c\u4e00\u4ef6\u4ea7\u54c1\u914d\u4e24\u4efd\u6587\u6863\uff0c\u8bfb\u8005\u4e0d\u540c\uff0c\u4fe1\u606f\u91cf\u5b8c\u5168\u4e0d\u540c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e24\u8005\u7684\u56de\u9000\u89c4\u5219\u662f\uff1a<code>str(obj)<\/code> \u4f18\u5148\u627e <code>__str__<\/code>\uff0c<strong>\u627e\u4e0d\u5230\u5c31\u9000\u800c\u7528 <code>__repr__<\/code><strong>\uff1b\u800c <code>repr(obj)<\/code>\uff08\u4ee5\u53ca\u4ea4\u4e92\u5f0f\u7ec8\u7aef\u663e\u793a\uff09\u53ea\u7528 <code>__repr__<\/code>\uff0c\u627e\u4e0d\u5230\u5c31\u7528 <code>object<\/code> \u57fa\u7c7b\u7684\u9ed8\u8ba4\u5730\u5740\u683c\u5f0f\u3002\u53e6\u5916\u8bb0\u4f4f\uff1a<\/strong>\u5bb9\u5668\u6253\u5370\u5143\u7d20\u65f6\u7528\u7684\u662f repr<\/strong>\u2014\u2014<code>print([obj])<\/code> \u91cc\u5217\u8868\u5143\u7d20\u8d70 <code>__repr__<\/code> \u800c\u4e0d\u662f <code>__str__<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>str \u4e0e repr \u7684\u5206\u5de5<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Book:\n    def __init__(self, title, price):\n        self.title = title\n        self.price = price\n\n    def __str__(self):\n        return f\"\u300a{self.title}\u300b{self.price} \u5143\"\n\n    def __repr__(self):\n        return f\"Book(title={self.title!r}, price={self.price!r})\"\n\nb = Book(\"Python \u5165\u95e8\", 39.9)\nprint(str(b))       # \u8d70 __str__\uff1a\u53cb\u597d\nprint(repr(b))      # \u8d70 __repr__\uff1a\u5e26\u6784\u9020\u4fe1\u606f\nprint(&#91;b])          # \u5bb9\u5668\u91cc\u7684\u5143\u7d20\u8d70 __repr__\uff01\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u300aPython \u5165\u95e8\u300b39.9 \u5143\nBook(title='Python \u5165\u95e8', price=39.9)\n&#91;Book(title='Python \u5165\u95e8', price=39.9)]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>print(str(b))<\/code> \u8f93\u51fa\u4e66\u540d\u52a0\u4ef7\u683c\uff0c\u662f\u7ed9\u4eba\u770b\u7684\uff1b<code>repr(b)<\/code> \u8f93\u51fa\u300c\u7528\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u80fd\u518d\u9020\u51fa\u8fd9\u4e2a\u5bf9\u8c61\u300d\u7684\u98ce\u683c\u2014\u2014<code>!r<\/code> \u8868\u793a\u300c\u7528 repr \u65b9\u5f0f\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u300d\uff08\u5e26\u5f15\u53f7\uff09\uff1b<code>print([b])<\/code> \u662f\u5173\u952e\u7ec6\u8282\uff1a**\u5217\u8868\u6253\u5370\u5143\u7d20\u65f6\u8c03\u7528\u7684\u662f\u5143\u7d20\u7684 <code>__repr__<\/code>**\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u56de\u9000\u89c4\u5219<\/strong>\uff1a\u5982\u679c\u7c7b\u53ea\u5b9a\u4e49 <code>__repr__<\/code> \u800c\u6ca1\u5b9a\u4e49 <code>__str__<\/code>\uff0c<code>print(obj)<\/code> \u4f1a\u81ea\u52a8\u56de\u9000\u8c03\u7528 <code>__repr__<\/code>\uff0c\u4e24\u79cd\u8f93\u51fa\u76f8\u540c\uff1b\u4e24\u4e2a\u90fd\u6ca1\u5b9a\u4e49\u65f6\u624d\u663e\u793a\u5185\u5b58\u5730\u5740\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u53ea\u5b9a\u4e49 <code>__str__<\/code> \u4e0d\u5b9a\u4e49 <code>__repr__<\/code>\uff0c\u5728\u7ec8\u7aef\u76f4\u63a5\u8f93\u5bf9\u8c61\u540d \u2192 \u95ee\u9898\uff1a\u663e\u793a\u96be\u770b\u7684\u5185\u5b58\u5730\u5740\uff08\u7ec8\u7aef\u53ea\u7528 repr\uff09 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u8c03\u8bd5\u7c7b\u81f3\u5c11\u5b9a\u4e49 <code>__repr__<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u4ee5\u4e3a <code>print([obj])<\/code> \u8d70 <code>__str__<\/code> \u2192 \u95ee\u9898\uff1a\u5bb9\u5668\u5143\u7d20\u8d70 <code>__repr__<\/code>\uff0c\u8f93\u51fa\u548c\u9884\u671f\u4e0d\u4e00\u6837 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u60f3\u8ba9\u5217\u8868\u91cc\u7684\u5bf9\u8c61\u4e5f\u597d\u770b\uff0c\u5b9a\u4e49 <code>__repr__<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>__repr__<\/code> \u91cc\u7528 <code>!r<\/code> \u5374\u4e0d\u77e5\u9053\u542b\u4e49 \u2192 \u95ee\u9898\uff1a\u8f93\u51fa\u4e0d\u5e26\u5f15\u53f7\uff0c\u770b\u4e0d\u51fa\u662f\u5b57\u7b26\u4e32 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a<code>!r<\/code> \u8868\u793a\u6309 repr \u683c\u5f0f\u5316\uff08\u5b57\u7b26\u4e32\u4f1a\u5e26\u5f15\u53f7\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1astr \u7ed9\u7528\u6237\uff0crepr \u7ed9\u8c03\u8bd5\uff1bstr \u7f3a\u4e86\u627e repr\uff0c\u5bb9\u5668\u6253\u5370\u7528 repr\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3 <strong>eq<\/strong> \u4e0e\u54c8\u5e0c \u2014\u2014 \u76f8\u7b49\u7684\u5b9a\u4e49\u5f71\u54cd set \/ dict<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u5b9a\u4e49 <code>__eq__<\/code> \u65f6\uff0c<code>==<\/code> \u6bd4\u8f83\u7684\u662f<strong>\u5185\u5b58\u5730\u5740<\/strong>\uff08\u672c\u8d28\u662f <code>is<\/code>\uff09\uff0c\u4e24\u4e2a\u5185\u5bb9\u76f8\u540c\u7684\u5bf9\u8c61\u4e5f\u4e0d\u76f8\u7b49\u3002\u5b9a\u4e49 <code>__eq__<\/code> \u540e\uff0c\u4f60\u53ef\u4ee5\u89c4\u5b9a\u300c\u4ec0\u4e48\u7b97\u76f8\u7b49\u300d\u2014\u2014\u5b66\u751f\u5206\u6570\u76f8\u540c\u7b97\u76f8\u7b49\u3001\u5750\u6807\u76f8\u540c\u7b97\u76f8\u7b49\u3002\u4f46\u8fd9\u91cc\u85cf\u7740\u4e00\u4e2a\u5927\u5751\uff1a<strong>\u5b9a\u4e49 <code>__eq__<\/code> \u4f1a\u8ba9\u5bf9\u8c61\u9ed8\u8ba4\u53d8\u5f97\u300c\u4e0d\u53ef\u54c8\u5e0c\u300d\uff08unhashable\uff09<\/strong>\uff0c\u653e\u8fdb <code>set<\/code> \u6216\u5f53 <code>dict<\/code> \u7684\u952e\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u91cc <code>==<\/code> \u8c03 <code>__eq__<\/code>\uff0c<code>hash()<\/code> \u8c03 <code>__hash__<\/code>\u3002\u89c4\u5219\u662f\uff1a<strong>\u4e24\u4e2a\u76f8\u7b49\u7684\u5bf9\u8c61\uff0c\u54c8\u5e0c\u503c\u5fc5\u987b\u76f8\u540c<\/strong>\uff08\u5426\u5219 set\/dict \u7684\u53bb\u91cd\u548c\u67e5\u627e\u5c31\u4e71\u5957\u4e86\uff09\u3002<code>object<\/code> \u57fa\u7c7b\u9ed8\u8ba4\u7528 id \u505a\u54c8\u5e0c\uff08\u4e0e\u9ed8\u8ba4\u7684 <code>==<\/code> \u8bed\u4e49\u4e00\u81f4\uff09\u3002\u4e00\u65e6\u4f60\u81ea\u5b9a\u4e49\u4e86 <code>__eq__<\/code>\uff0cPython \u65e0\u6cd5\u4fdd\u8bc1\u4f60\u5b9a\u4e49\u7684\u76f8\u7b49\u5173\u7cfb\u548c\u9ed8\u8ba4\u54c8\u5e0c\u8fd8\u517c\u5bb9\uff0c\u4e8e\u662f<strong>\u81ea\u52a8\u628a <code>__hash__<\/code> \u8bbe\u4e3a None<\/strong>\u2014\u2014\u5bf9\u8c61\u4e0d\u53ef\u54c8\u5e0c\u3002\u60f3\u8981 set \u6b63\u5e38\u5de5\u4f5c\uff0c\u5fc5\u987b\u540c\u65f6\u91cd\u5199 <code>__hash__<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9ed8\u8ba4\u7684 <code>==<\/code> \u53ea\u8ba4\u300c\u540c\u4e00\u4e2a\u4eba\u300d\uff08id\uff09\uff0c\u5b9a\u4e49\u4e86 <code>__eq__<\/code> \u540e\u300c\u540c\u540d\u5c31\u7b97\u540c\u4e00\u4eba\u300d\uff1b\u4f46 <code>set<\/code> \u67e5\u91cd\u9760\u7684\u662f<strong>\u54c8\u5e0c\u503c = \u6863\u6848\u67dc\u7f16\u53f7<\/strong>\uff0c\u4f60\u6539\u4e86\u76f8\u7b49\u89c4\u5219\u5374\u6ca1\u6539\u7f16\u53f7\u89c4\u5219\uff0c\u6863\u6848\u67dc\u5c31\u4e71\u5957\u4e86\u2014\u2014\u6240\u4ee5 Python \u5e72\u8106\u7981\u6b62\u4f60\u628a\u4eba\u653e\u8fdb\u6863\u6848\u67dc\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u9ed8\u8ba4 == \u6bd4\u8f83\u5185\u5b58\u5730\u5740<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Point:\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y\n\np1 = Point(1, 2)\np2 = Point(1, 2)\nprint(p1 == p2)      # \u5185\u5bb9\u4e00\u6837\uff0c\u4f46\u9ed8\u8ba4\u6bd4\u8f83\u5730\u5740\nprint(p1 is p2)      # \u5730\u5740\u5f53\u7136\u4e0d\u540c\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>False\nFalse\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ca1\u5b9a\u4e49 <code>__eq__<\/code> \u65f6 <code>==<\/code> \u9000\u5316\u6210 <code>is<\/code>\uff08\u6bd4\u8f83\u5185\u5b58\u5730\u5740\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u5b9a\u4e49 <strong>eq<\/strong> \u540e\uff0c\u5185\u5bb9\u76f8\u7b49\u5373\u76f8\u7b49<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Point:\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y\n\n    def __eq__(self, other):\n        if not isinstance(other, Point):\n            return False\n        return self.x == other.x and self.y == other.y\n\np1 = Point(1, 2)\np2 = Point(1, 2)\nprint(p1 == p2)     # \u5750\u6807\u76f8\u540c\uff0cTrue\nprint(p1 == \"Point\")  # \u7c7b\u578b\u4e0d\u540c\uff0c\u8fd4\u56de False\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True\nFalse\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>__eq__<\/code> \u91cc\u5148\u5224\u65ad\u7c7b\u578b\uff08\u4e0d\u662f Point \u76f4\u63a5\u8fd4\u56de False\uff0c\u907f\u514d\u548c\u5b57\u7b26\u4e32\u7b49\u6bd4\u8f83\u65f6\u629b\u5f02\u5e38\uff09\uff0c\u518d\u6bd4\u8f83\u5750\u6807\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 **\u5b9a\u4e49 <strong>eq<\/strong> \u540e set \u4f1a\u62d2\u7edd\u2014\u2014\u5fc5\u987b\u540c\u65f6\u5b9a\u4e49 <strong>hash<\/strong>**\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Point:\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y\n\n    def __eq__(self, other):\n        if not isinstance(other, Point):\n            return False\n        return self.x == other.x and self.y == other.y\n\n    def __hash__(self):\n        return hash((self.x, self.y))   # \u7528\u5750\u6807\u5143\u7ec4\u7684\u54c8\u5e0c\n\np1 = Point(1, 2)\np2 = Point(1, 2)\nprint(len({p1, p2}))        # \u5185\u5bb9\u76f8\u540c\uff0cset \u53ea\u4fdd\u7559\u4e00\u4e2a\nprint(hash(p1) == hash(p2)) # \u76f8\u7b49\u7684\u5bf9\u8c61\u54c8\u5e0c\u5fc5\u987b\u76f8\u540c\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\nTrue\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u540c\u65f6\u5b9a\u4e49 <code>__eq__<\/code> \u548c <code>__hash__<\/code> \u540e set \u5c31\u80fd\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002**\u94c1\u5f8b\uff1a\u5b9a\u4e49 <code>__eq__<\/code> \u5c31\u4e00\u5b9a\u8981\u5b9a\u4e49 <code>__hash__<\/code>**\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u53ea\u5b9a\u4e49 <code>__eq__<\/code> \u5c31\u628a\u5bf9\u8c61\u653e set \u2192 \u95ee\u9898\uff1a\u62a5 <code>TypeError: unhashable type: 'Point'<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u540c\u65f6\u5b9a\u4e49 <code>__hash__<\/code>\uff0c\u6216\u6362\u4e2a\u6570\u636e\u7ed3\u6784\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>__eq__<\/code> \u91cc\u7528 <code>self.x == other.x<\/code> \u5374\u4e0d\u68c0\u67e5\u7c7b\u578b \u2192 \u95ee\u9898\uff1a\u548c\u522b\u7684\u7c7b\u578b\u6bd4\u8f83\u65f6\u629b <code>AttributeError<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5148 <code>isinstance(other, Point)<\/code> \u518d\u6bd4\u5c5e\u6027\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>__eq__<\/code> \u8fd4\u56de\u975e\u5e03\u5c14\uff08\u5982\u8fd4\u56de\u6570\u5b57\u3001None\uff09 \u2192 \u95ee\u9898\uff1a\u884c\u4e3a\u602a\u5f02\uff08<code>if<\/code> \u5224\u65ad\u65f6\u6309\u771f\u503c\u89e3\u91ca\uff09 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a<code>__eq__<\/code> \u660e\u786e\u8fd4\u56de True \/ False\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u4e24\u4e2a\u76f8\u7b49\u5bf9\u8c61\u54c8\u5e0c\u4e0d\u540c \u2192 \u95ee\u9898\uff1aset\/dict \u884c\u4e3a\u9519\u4e71\uff1a\u53bb\u91cd\u5931\u6548\u3001\u67e5\u627e\u4e0d\u5230 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u4fdd\u8bc1\u76f8\u7b49 \u21d2 \u54c8\u5e0c\u76f8\u7b49\uff08\u4e00\u8d77\u5b9a\u4e49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1aeq \u5b9a\u76f8\u7b49\uff0chash \u5b9a\u6863\u6848\uff1b\u4e24\u4e2a\u90fd\u5b9a\u4e49\uff0cset \u624d\u4e0d\u4e71\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4 <strong>lt<\/strong> \u4e0e\u6392\u5e8f \u2014\u2014 \u53ea\u5199\u4e00\u4e2a\u65b9\u6cd5\uff0csorted \u5168\u81ea\u52a8<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>sorted()<\/code> \u6392\u5e8f\u5bf9\u8c61\u5217\u8868\u65f6\uff0c\u9700\u8981\u77e5\u9053\u300c\u8c01\u5728\u524d\u8c01\u5728\u540e\u300d\u2014\u2014\u5b83\u5c31\u662f\u9760 <code>&lt;<\/code> \u6bd4\u8f83\uff08\u8c03\u7528 <code>__lt__<\/code>\uff09\u505a\u5230\u7684\u3002\u6240\u4ee5<strong>\u53ea\u5b9a\u4e49 <code>__lt__<\/code> \u4e00\u4e2a\u65b9\u6cd5\uff0c\u4f60\u7684\u7c7b\u5c31\u80fd\u88ab <code>sorted()<\/code>\u3001<code>min()<\/code>\u3001<code>max()<\/code> \u4f7f\u7528<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>sorted()<\/code> \u5185\u90e8\u7528 Timsort \u7b97\u6cd5\u5bf9\u5143\u7d20\u4e24\u4e24\u6bd4\u8f83\uff0c\u6bd4\u8f83\u65f6\u6267\u884c\u7684\u662f <code>a &lt; b<\/code>\uff0c\u5373 <code>a.__lt__(b)<\/code>\u3002\u6240\u4ee5\u5bf9\u8c61\u8981\u80fd\u6392\u5e8f\uff0c**\u6700\u5c0f\u8981\u6c42\u662f\u5b9a\u4e49 <code>__lt__<\/code>**\u3002Python 3 \u91cc <code>&gt;<\/code>\u3001<code>&lt;=<\/code>\u3001<code>&gt;=<\/code> \u5404\u6709\u72ec\u7acb\u7684\u65b9\u6cd5\uff08<code>__gt__<\/code>\u3001<code>__le__<\/code>\u3001<code>__ge__<\/code>\uff09\uff0c\u7f3a\u4e86\u5c31\u7528\u4e0d\u4e86\u2014\u2014<code>@total_ordering<\/code> \u88c5\u9970\u5668\u4f1a\u4f9d\u636e\u4f60\u5b9a\u4e49\u7684 <code>__eq__<\/code> \u548c <code>__lt__<\/code>\uff0c\u81ea\u52a8\u63a8\u5bfc\u8865\u9f50\u5176\u4f59\u56db\u4e2a\u6bd4\u8f83\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>__lt__<\/code> \u50cf<strong>\u6392\u961f\u89c4\u5219<\/strong>\uff1a\u8001\u5e08\u53ea\u8bf4\u4e86\u4e00\u6761\u89c4\u5219\u300c\u6309\u4e2a\u5b50\u77ee\u7684\u5728\u524d\u300d\uff0c\u5168\u73ed\u540c\u5b66\u5c31\u80fd\u81ea\u52a8\u6392\u597d\u961f\uff1b<code>total_ordering<\/code> \u50cf<strong>\u6821\u89c4\u81ea\u52a8\u8865\u5168<\/strong>\uff1a\u77e5\u9053\u4e86\u300c\u77ee\u5728\u524d\u300d\uff0c\u300c\u9ad8\u5728\u540e\u300d\uff08<code>&gt;<\/code>\uff09\u3001\u300c\u4e0d\u6bd4\u4ed6\u77ee\u300d\uff08<code>&gt;=<\/code>\uff09\u8fd9\u4e9b\u8bf4\u6cd5\u81ea\u52a8\u63a8\u5bfc\u51fa\u6765\uff0c\u4e0d\u7528\u6bcf\u6761\u90fd\u5199\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u53ea\u5b9a\u4e49 __lt__\uff0csorted \/ min \/ max \u5168\u80fd\u7528<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student:\n    def __init__(self, name, score):\n        self.name = name\n        self.score = score\n\n    def __lt__(self, other):\n        return self.score &lt; other.score     # \u6309\u5206\u6570\u5347\u5e8f\n\n    def __repr__(self):\n        return f\"{self.name}({self.score}\u5206)\"\n\nstudents = &#91;\n    Student(\"\u674e\u56db\", 78),\n    Student(\"\u5f20\u4e09\", 90),\n    Student(\"\u738b\u4e94\", 85),\n]\nprint(sorted(students))    # \u6392\u5e8f\uff1a\u4e24\u4e24\u6bd4\u8f83 &lt;\nprint(min(students))       # \u6700\u5c0f\u503c\uff1a\u4e00\u8def\u6bd4\u8f83 &lt;\nprint(max(students))       # \u6700\u5927\u503c\uff1a\u4e00\u8def\u6bd4\u8f83 &lt;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\u674e\u56db(78\u5206), \u738b\u4e94(85\u5206), \u5f20\u4e09(90\u5206)]\n\u674e\u56db(78\u5206)\n\u5f20\u4e09(90\u5206)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u4e2a\u65b9\u6cd5\uff0c\u4e09\u79cd\u80fd\u529b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>total_ordering \u8865\u9f50\u5168\u90e8\u6bd4\u8f83\u8fd0\u7b97<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from functools import total_ordering\n\n@total_ordering\nclass Student:\n    def __init__(self, name, score):\n        self.name = name\n        self.score = score\n\n    def __eq__(self, other):\n        return self.score == other.score\n\n    def __lt__(self, other):\n        return self.score &lt; other.score\n\n    def __repr__(self):\n        return f\"{self.name}({self.score}\u5206)\"\n\na = Student(\"\u5f20\u4e09\", 90)\nb = Student(\"\u674e\u56db\", 78)\nprint(a &gt; b)     # \u81ea\u52a8\u63a8\u5bfc\uff1a90 &gt; 78\nprint(a &gt;= b)    # \u81ea\u52a8\u63a8\u5bfc\uff1a90 &gt;= 78\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True\nTrue\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ea\u5199\u4e86 <code>__eq__<\/code> \u548c <code>__lt__<\/code>\uff0c\u4f46 <code>&gt;<\/code>\u3001<code>&gt;=<\/code> \u4e5f\u80fd\u7528\u4e86\u3002\u6ce8\u610f\u5b83<strong>\u8981\u6c42\u4f60\u5fc5\u987b\u63d0\u4f9b <code>__eq__<\/code> \u548c\u81f3\u5c11\u4e00\u4e2a\u6392\u5e8f\u65b9\u6cd5<\/strong>\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u53ea\u5b9a\u4e49 <code>__lt__<\/code> \u5c31\u7528 <code>><\/code> \u6bd4\u8f83 \u2192 \u95ee\u9898\uff1a\u62a5 <code>TypeError: '>' not supported between instances<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528 <code>@total_ordering<\/code> \u8865\u9f50\uff0c\u6216\u5b9a\u4e49 <code>__gt__<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>__lt__<\/code> \u91cc\u4e0d return \u5e03\u5c14 \u2192 \u95ee\u9898\uff1a\u6392\u5e8f\u7ed3\u679c\u9519\u4e71 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a<code>__lt__<\/code> \u5fc5\u987b\u8fd4\u56de True\/False\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u7528 <code>@total_ordering<\/code> \u5374\u6ca1\u5b9a\u4e49 <code>__eq__<\/code> \u2192 \u95ee\u9898\uff1a\u62a5\u9519\uff1atotal_ordering \u8981\u6c42\u81f3\u5c11\u4e00\u4e2a\u6392\u5e8f\u65b9\u6cd5 + <code>__eq__<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u4e24\u4e2a\u90fd\u5b9a\u4e49\u518d\u88c5\u9970\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5fd8\u8bb0 <code>from functools import total_ordering<\/code> \u2192 \u95ee\u9898\uff1a\u62a5 <code>NameError<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5148\u5bfc\u5165\u518d\u4f7f\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1alt \u4e00\u4e2a\u5c31\u80fd\u6392\uff0csorted min max \u5168\u901a\u5403\uff1b\u60f3\u8981\u5168\u6bd4\u8f83\uff0ctotal_ordering \u5e2e\u4f60\u8865\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5 <strong>add<\/strong> \/ <strong>len<\/strong> \/ <strong>contains<\/strong> \u2014\u2014 \u300c\u8fd4\u56de\u65b0\u5bf9\u8c61\u300d\u7684\u7ea6\u5b9a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>__add__<\/code> \u8ba9\u5bf9\u8c61\u652f\u6301 <code>+<\/code>\uff0c<code>__len__<\/code> \u8ba9\u5bf9\u8c61\u652f\u6301 <code>len()<\/code>\uff0c<code>__contains__<\/code> \u8ba9\u5bf9\u8c61\u652f\u6301 <code>in<\/code>\u3002\u8fd9\u4e09\u4e2a\u9b54\u6cd5\u65b9\u6cd5\u8ba9\u81ea\u5b9a\u4e49\u5bb9\u5668\uff08\u8d2d\u7269\u8f66\u3001\u4e66\u67b6\u3001\u961f\u4f0d\uff09\u7528\u8d77\u6765\u548c list\/dict \u4e00\u6837\u987a\u624b\u3002\u7279\u522b\u8981\u6ce8\u610f <code>__add__<\/code> \u7684\u7ea6\u5b9a\uff1a<strong>\u8fd4\u56de\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u4fee\u6539\u81ea\u5df1<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>a + b<\/code> \u4f1a\u8c03\u7528 <code>a.__add__(b)<\/code>\uff1b<code>len(x)<\/code> \u8c03\u7528 <code>x.__len__()<\/code>\uff1b<code>item in x<\/code> \u8c03\u7528 <code>x.__contains__(item)<\/code>\uff08\u6ca1\u6709 <code>__contains__<\/code> \u65f6\uff0cPython \u4f1a\u9000\u800c\u7528 <code>__iter__<\/code> \u9010\u4e2a\u904d\u5386\u2014\u2014Day 18 \u8bb2\u8fed\u4ee3\u5668\uff09\u3002<code>__add__<\/code> \u8fd4\u56de\u65b0\u5bf9\u8c61\u662f<strong>\u4e0d\u53ef\u53d8\u8bed\u4e49<\/strong>\uff1a\u52a0\u6cd5\u5728\u6570\u5b66\u4e0a\u4e0d\u6539\u53d8\u64cd\u4f5c\u6570\uff0cPython \u5185\u7f6e\u7c7b\u578b\uff08int\u3001str\u3001tuple\uff09\u90fd\u9075\u5b88\u8fd9\u70b9\uff0c\u81ea\u5b9a\u4e49\u7c7b\u4e5f\u5e94\u9075\u5faa\uff0c\u5426\u5219 <code>a + b<\/code> \u4e4b\u540e a \u6084\u6084\u53d8\u4e86\uff0c\u8c03\u7528\u65b9\u4f1a\u4e00\u8138\u61f5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>__add__<\/code> \u50cf<strong>\u628a\u4e24\u76d2\u79ef\u6728\u5012\u8fdb\u4e00\u4e2a\u65b0\u76d2\u5b50<\/strong>\uff1a\u539f\u6765\u7684\u4e24\u76d2\u539f\u5c01\u4e0d\u52a8\uff08\u4e0d\u4fee\u6539\u81ea\u8eab\uff09\uff0c\u65b0\u76d2\u5b50\u88c5\u7684\u662f\u5408\u8d77\u6765\u7684\u90a3\u4efd\u3002\u5982\u679c\u5012\u5b8c\u65e7\u76d2\u5b50\u91cc\u7684\u79ef\u6728\u6ca1\u4e86\uff0c\u4f60\u5c31\u6ca1\u6cd5\u300c\u518d\u6765\u4e00\u6b21\u300d\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u8d2d\u7269\u8f66\u4e09\u4ef6\u5957\u2014\u2014+ \u5408\u5e76\u3001len \u957f\u5ea6\u3001in \u6210\u5458<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Cart:\n    def __init__(self, items=None):\n        self.items = items if items else &#91;]\n\n    def __add__(self, other):\n        return Cart(self.items + other.items)   # \u65b0\u5bf9\u8c61\n\n    def __len__(self):\n        return len(self.items)\n\n    def __contains__(self, item):\n        return item in self.items\n\n    def __repr__(self):\n        return f\"Cart({self.items})\"\n\ncart_a = Cart(&#91;\"\u82f9\u679c\", \"\u725b\u5976\"])\ncart_b = Cart(&#91;\"\u9762\u5305\"])\ncart_c = cart_a + cart_b     # \u89e6\u53d1 __add__\nprint(cart_c)                # \u5408\u5e76\u7ed3\u679c\nprint(cart_a)                # \u539f\u8f66\u6ca1\u53d8\nprint(cart_a is cart_c)      # \u786e\u5b9e\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u5bf9\u8c61\nprint(len(cart_c))           # \u89e6\u53d1 __len__\uff1a3\nprint(\"\u725b\u5976\" in cart_c)      # \u89e6\u53d1 __contains__\uff1aTrue\nprint(len(Cart()))           # \u7a7a\u8d2d\u7269\u8f66\u957f\u5ea6\u4e3a 0\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Cart(&#91;'\u82f9\u679c', '\u725b\u5976', '\u9762\u5305'])\nCart(&#91;'\u82f9\u679c', '\u725b\u5976'])\nFalse\n3\nTrue\n0\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>cart_a + cart_b<\/code> \u89e6\u53d1 <code>cart_a.__add__(cart_b)<\/code>\uff0c\u7528 <code>self.items + other.items<\/code> \u62fc\u51fa<strong>\u65b0\u7684\u5217\u8868<\/strong>\u3001\u5305\u8fdb<strong>\u65b0\u7684 Cart<\/strong> \u8fd4\u56de\u2014\u2014<code>cart_a<\/code> \u6ca1\u88ab\u6539\u52a8\uff1b<code>len(cart)<\/code> \u8d70 <code>__len__<\/code>\uff1b<code>\"\u725b\u5976\" in cart<\/code> \u8d70 <code>__contains__<\/code>\u3002\u6ce8\u610f <code>Cart()<\/code> \u6ca1\u4f20\u53c2\u6570\u65f6 <code>items<\/code> \u9ed8\u8ba4\u662f <code>[]<\/code>\uff08<code>items if items else []<\/code> \u5904\u7406\u4e86\u300c\u4f20 None\u300d\u7684\u60c5\u51b5\uff09\u2014\u2014<strong>\u7a7a\u5bb9\u5668\u4e5f\u5e94\u8be5\u662f\u5408\u6cd5\u5bb9\u5668<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>__add__<\/code> \u91cc\u76f4\u63a5 <code>self.items += other.items<\/code> \u7136\u540e return self \u2192 \u95ee\u9898\uff1a\u539f\u5bf9\u8c61\u88ab\u4fee\u6539\uff0c\u4e0e\u5185\u7f6e <code>+<\/code> \u8bed\u4e49\u4e0d\u7b26\uff0c<code>a + b<\/code> \u540e a \u53d8\u4e86 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u65b0\u5efa\u5bf9\u8c61\uff1a<code>return Cart(self.items + other.items)<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u9ed8\u8ba4\u53c2\u6570\u5199\u6210 <code>def __init__(self, items=[])<\/code> \u2192 \u95ee\u9898\uff1a\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u540c\u4e00\u4e2a\u5217\u8868\uff0c\u4e92\u76f8\u6c61\u67d3 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5199 <code>items=None<\/code>\uff0c\u65b9\u6cd5\u91cc <code>self.items = items if items else []<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>__contains__<\/code> \u5fd8 return \u2192 \u95ee\u9898\uff1a\u8fd4\u56de None\uff0c<code>in<\/code> \u7ed3\u679c\u6052\u4e3a False\uff08None \u662f\u5047\u503c\uff09 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5fc5\u987b return \u5e03\u5c14\u503c\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>__len__<\/code> \u8fd4\u56de\u8d1f\u6570\u6216\u975e\u6574\u6570 \u2192 \u95ee\u9898\uff1a\u62a5 <code>ValueError<\/code> \/ <code>TypeError<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u8fd4\u56de <code>len(self.items)<\/code> \u8fd9\u7c7b\u975e\u8d1f\u6574\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1aadd \u52a0\u51fa\u65b0\u5bf9\u8c61\uff0c\u539f\u7269\u4e0d\u52a8\u5b88\u7ea6\u5b9a\uff1blen \u4e0e contains\uff0c\u5bb9\u5668\u4e09\u4ef6\u5957\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6 \u52a8\u624b\u5b9e\u8df5\uff1a\u589e\u5f3a\u7248\u5bf9\u8c61\u4f53\u9a8c\u7a0b\u5e8f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u505a\u4e00\u4e2a <code>Book<\/code> \u7c7b\uff0c\u628a\u4eca\u5929\u5b66\u7684\u9b54\u6cd5\u65b9\u6cd5\u5168\u90e8\u7528\u4e0a\uff0c\u518d\u5199\u4e00\u4e2a\u6f14\u793a\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># magic_books.py \u2014\u2014 \u589e\u5f3a\u7248\u5bf9\u8c61\u4f53\u9a8c\u7a0b\u5e8f\nclass Book:\n    def __init__(self, title, price):\n        self.title = title\n        self.price = price\n\n    def __str__(self):\n        return f\"\u300a{self.title}\u300b{self.price} \u5143\"\n\n    def __repr__(self):\n        return f\"Book({self.title!r}, {self.price!r})\"\n\n    def __eq__(self, other):\n        if not isinstance(other, Book):\n            return False\n        return self.title == other.title\n\n    def __lt__(self, other):\n        return self.price &lt; other.price\n\n    def __len__(self):\n        return len(self.title)\n\n    def __add__(self, other):\n        return Book(f\"{self.title} + {other.title}\", self.price + other.price)\n\n    def __contains__(self, keyword):\n        return keyword in self.title\n\n\ndef demo():\n    book1 = Book(\"Python \u5165\u95e8\", 39.9)\n    book2 = Book(\"Python \u5165\u95e8\", 99.0)      # \u4e0d\u540c\u7248\u672c\u4e0d\u540c\u4ef7\n    book3 = Book(\"Python \u8fdb\u9636\", 59.9)\n    print(\"--- __str__ \/ __repr__ ---\")\n    print(book1)\n    print(repr(book1))\n    print(\"--- __eq__ \u6309\u4e66\u540d\u6bd4\u8f83 ---\")\n    print(book1 == book2)     # \u540c\u540d\uff0cTrue\n    print(book1 == book3)     # \u4e0d\u540c\u540d\uff0cFalse\n    print(\"--- __lt__ + sorted \u6309\u4ef7\u683c\u6392\u5e8f ---\")\n    for b in sorted(&#91;book3, book1, book2]):\n        print(b)\n    print(\"--- __len__ \u4e66\u540d\u957f\u5ea6 ---\")\n    print(len(book1))\n    print(\"--- __add__ \u5408\u5e76 ---\")\n    bundle = book1 + book3\n    print(bundle)\n    print(\"--- __contains__ ---\")\n    print(\"\u5165\u95e8\" in book1)\n    print(\"\u722c\u866b\" in book1)\n\n\nif __name__ == \"__main__\":\n    demo()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>--- __str__ \/ __repr__ ---\n\u300aPython \u5165\u95e8\u300b39.9 \u5143\nBook('Python \u5165\u95e8', 39.9)\n--- __eq__ \u6309\u4e66\u540d\u6bd4\u8f83 ---\nTrue\nFalse\n--- __lt__ + sorted \u6309\u4ef7\u683c\u6392\u5e8f ---\n\u300aPython \u5165\u95e8\u300b39.9 \u5143\n\u300aPython \u8fdb\u9636\u300b59.9 \u5143\n\u300aPython \u5165\u95e8\u300b99.0 \u5143\n--- __len__ \u4e66\u540d\u957f\u5ea6 ---\n9\n--- __add__ \u5408\u5e76 ---\n\u300aPython \u5165\u95e8 + Python \u8fdb\u9636\u300b99.8 \u5143\n--- __contains__ ---\nTrue\nFalse\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6392\u5e8f\u8f93\u51fa\u4e86\u4e09\u672c\u4e66\uff0839.9\u300159.9\u300199.0\uff09\uff0c\u6309\u4ef7\u683c\u5347\u5e8f\u6392\u5217\uff0c\u8bf4\u660e <code>__lt__<\/code> \u88ab <code>sorted()<\/code> \u6b63\u786e\u8c03\u7528\u3002\u6ce8\u610f <code>len(\"Python \u5165\u95e8\")<\/code> \u8ba1\u7b97\u7684\u662f\u5b57\u7b26\u4e2a\u6570\uff08\u542b\u7a7a\u683c\uff09\uff1aPython 6 + \u7a7a\u683c 1 + \u5165\u95e8 2 = 9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5347\u7ea7\u6311\u6218<\/strong>\uff1a\u7ed9 <code>Book<\/code> \u52a0 <code>__sub__<\/code>\uff08\u4e70\u4e66\u62b5\u6263\uff09\u548c <code>__getitem__<\/code>\uff08\u7528\u4e0b\u6807\u53d6\u4e66\u540d\u4e2d\u7684\u5b57\uff09\uff0c\u518d\u5b9a\u4e49\u4e00\u4e2a <code>BookShelf<\/code> \u7c7b\uff0c\u7528 <code>__len__<\/code> \u7edf\u8ba1\u4e66\u67b6\u4e0a\u7684\u4e66\uff0c\u7528 <code>__getitem__<\/code> \u6309\u4e0b\u6807\u53d6\u4e66\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4eca\u65e5\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9b54\u6cd5\u65b9\u6cd5 = \u53cc\u4e0b\u5212\u7ebf + \u81ea\u52a8\u89e6\u53d1\uff1b\u5185\u7f6e\u64cd\u4f5c\uff08<code>==<\/code>\u3001<code>+<\/code>\u3001<code>in<\/code>\u3001<code>len()<\/code>\u3001<code>print<\/code>\uff09\u5e95\u5c42\u5168\u662f\u65b9\u6cd5\u8c03\u7528\n<code>__str__<\/code> \u7ed9\u7528\u6237\u3001<code>__repr__<\/code> \u7ed9\u8c03\u8bd5\uff1b<code>str<\/code> \u7f3a\u4e86\u9000 <code>__repr__<\/code>\uff0c\u5bb9\u5668\u5143\u7d20\u6253\u5370\u8d70 <code>__repr__<\/code>\n\u9ed8\u8ba4 <code>==<\/code> \u6bd4\u8f83\u5185\u5b58\u5730\u5740\uff1b\u5b9a\u4e49 <code>__eq__<\/code> \u540e\u5185\u5bb9\u76f8\u540c\u5373\u76f8\u7b49\uff0c\u4f46\u5fc5\u987b\u540c\u65f6\u5b9a\u4e49 <code>__hash__<\/code> \u624d\u80fd\u8fdb set\/dict\n\u53ea\u5b9a\u4e49 <code>__lt__<\/code> \u5c31\u80fd\u88ab <code>sorted()<\/code> \/ <code>min()<\/code> \/ <code>max()<\/code> \u4f7f\u7528\uff1b<code>@total_ordering<\/code> \u4e00\u952e\u8865\u9f50\u5168\u90e8\u6bd4\u8f83\n<code>__add__<\/code> \u8fd4\u56de\u65b0\u5bf9\u8c61\u3001\u4e0d\u4fee\u6539\u81ea\u5df1\uff1b<code>__len__<\/code> \u8fd4\u56de\u975e\u8d1f\u6574\u6570\uff1b<code>__contains__<\/code> \u8fd4\u56de\u5e03\u5c14\n\u53ef\u53d8\u9ed8\u8ba4\u53c2\u6570\u7684\u5751\uff1a<code>def __init__(self, items=[])<\/code> \u4f1a\u8ba9\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u5217\u8868\n\u589e\u5f3a\u7248\u5bf9\u8c61\u4f53\u9a8c\u7a0b\u5e8f\uff08Book \u7c7b\uff09\u6240\u6709\u6f14\u793a\u529f\u80fd\u8fd0\u884c\u6b63\u5e38<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u62a5\u9519<\/th><th>\u539f\u56e0<\/th><th>\u4fee\u590d<\/th><\/tr><\/thead><tbody><tr><td><code>TypeError: 'Book' object is not callable<\/code><\/td><td>\u628a\u5bf9\u8c61\u5f53\u51fd\u6570\u8c03\u7528\uff08\u5982 <code>book()<\/code>\uff09<\/td><td>\u68c0\u67e5\u662f\u5426\u628a <code>__call__<\/code> \u76f8\u5173\u7528\u6cd5\u641e\u6df7\uff0c\u6216\u8c03\u7528\u4e86\u4e0d\u5b58\u5728\u7684\u51fd\u6570<\/td><\/tr><tr><td><code>TypeError: '&lt;' not supported between instances<\/code><\/td><td>\u6ca1\u5b9a\u4e49 <code>__lt__<\/code> \u5c31\u7528 <code>&lt;<\/code> \u6216 <code>sorted()<\/code> \u6bd4\u8f83\u5bf9\u8c61<\/td><td>\u5728\u7c7b\u91cc\u5b9a\u4e49 <code>__lt__<\/code><\/td><\/tr><tr><td><code>TypeError: 'Book' object does not support indexing<\/code><\/td><td>\u5bf9\u8c61\u4e0d\u652f\u6301\u4e0b\u6807 <code>[]<\/code><\/td><td>\u5b9a\u4e49 <code>__getitem__<\/code><\/td><\/tr><tr><td><code>RecursionError<\/code><\/td><td><code>__str__<\/code> \u91cc <code>print(self)<\/code> \u53c8\u89e6\u53d1 <code>__str__<\/code>\uff0c\u65e0\u9650\u9012\u5f52<\/td><td><code>__str__<\/code> \u91cc\u7528 <code>self.\u5c5e\u6027<\/code>\uff0c\u4e0d\u8981\u518d <code>print(self)<\/code><\/td><\/tr><tr><td><code>__add__<\/code> \u8fd4\u56de\u503c\u4e0d\u5bf9<\/td><td>\u5fd8\u4e86\u8fd4\u56de\u65b0\u5bf9\u8c61\uff0c\u6216\u8fd4\u56de\u4e86\u522b\u7684\u7c7b\u578b<\/td><td>\u786e\u8ba4\u8fd4\u56de\u540c\u7c7b\u65b0\u5bf9\u8c61\uff0c\u4e0d\u4fee\u6539\u539f\u5bf9\u8c61<\/td><\/tr><tr><td><code>TypeError: unhashable type: 'Point'<\/code><\/td><td>\u5b9a\u4e49\u4e86 <code>__eq__<\/code> \u5374\u6ca1\u5b9a\u4e49 <code>__hash__<\/code>\uff0c\u5bf9\u8c61\u4e0d\u80fd\u8fdb set\/dict<\/td><td>\u540c\u65f6\u5b9a\u4e49 <code>__hash__<\/code>\uff0c\u6216\u907f\u514d\u4f7f\u7528 set\/dict<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>1 \u9b54\u6cd5\u65b9\u6cd5\u662f\u4ec0\u4e48 \u2014\u2014 \u540d\u5b57\u5e26\u300c\u9b54\u6cd5\u300d\uff0c\u80cc\u540e\u662f\u7ea6\u5b9a \u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f \u9b54\u6cd5\u65b9\u6cd5\uff08\u4e5f\u53eb\u300c\u7279\u6b8a\u65b9\u6cd5\u300d\uff09\u662f\u540d\u5b57\u524d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[16,9],"tags":[17],"class_list":["post-100","post","type-post","status-publish","format-standard","hentry","category-python","category-code-dev","tag-python-day"],"_links":{"self":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/comments?post=100"}],"version-history":[{"count":2,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/100\/revisions"}],"predecessor-version":[{"id":146,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/100\/revisions\/146"}],"wp:attachment":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/media?parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/categories?post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/tags?post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}