{"id":177282,"date":"2024-05-08T19:27:55","date_gmt":"2024-05-08T11:27:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/177282.html"},"modified":"2024-05-08T19:28:01","modified_gmt":"2024-05-08T11:28:01","slug":"python%e4%b8%adis-%e5%92%8c-%e7%9a%84%e5%8c%ba%e5%88%ab%e6%98%af%e5%95%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/177282.html","title":{"rendered":"python\u4e2dis \u548c== \u7684\u533a\u522b\u662f\u5565"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27061227\/da14041d-9cad-426a-ae7b-31613825ebe6.webp\" alt=\"python\u4e2dis \u548c== \u7684\u533a\u522b\u662f\u5565\" \/><\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong><code>is<\/code> \u548c <code>==<\/code> \u662f\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u64cd\u4f5c\u7b26\uff0c\u4f46\u5b83\u4eec\u4e4b\u95f4\u6709\u7740\u6839\u672c\u7684\u533a\u522b\uff1a<code>is<\/code>\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u8eab\u4efd\uff08\u5373\u5185\u5b58\u4e2d\u7684\u5730\u5740\uff09\uff0c\u800c<code>==<\/code>\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u662f\u5426\u76f8\u7b49<\/strong>\u3002\u5177\u4f53\u5730\u8bf4\uff0c\u5f53\u6211\u4eec\u4f7f\u7528 <code>is<\/code> \u65f6\uff0cPython\u4f1a\u68c0\u67e5\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff1b\u800c\u4f7f\u7528 <code>==<\/code> \u65f6\uff0c\u5219\u662f\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u5185\u5bb9\u662f\u5426\u4e00\u81f4\u3002\u8fd9\u79cd\u533a\u522b\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\u5c24\u5176\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001IS\u7684\u4f7f\u7528\u548c\u539f\u7406<\/h3>\n<\/p>\n<p><p><code>is<\/code> \u64cd\u4f5c\u7b26\u7684\u4f7f\u7528\u573a\u5408\u901a\u5e38\u6d89\u53ca\u5230\u5bf9\u5bf9\u8c61\u8eab\u4efd\u7684\u6bd4\u8f83\u3002\u5728Python\u4e2d\uff0c\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u8eab\u4efd\u6807\u8bc6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u51fd\u6570 <code>id()<\/code> \u6765\u67e5\u770b\u3002\u8fd9\u4e2a\u8eab\u4efd\u6807\u8bc6\u5bf9\u5e94\u4e8e\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c<code>is<\/code> \u64cd\u4f5c\u7b26\u53ef\u4ee5\u88ab\u89c6\u4e3a\u4e00\u79cd\u68c0\u67e5\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u5f15\u7528\u5185\u5b58\u4e2d\u76f8\u540c\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u5f53\u4e00\u4e2a\u53d8\u91cf\u88ab\u8d4b\u503c\u4e3a\u53e6\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u5b83\u4eec\u4f1a\u6307\u5411\u5185\u5b58\u4e2d\u7684\u540c\u4e00\u4e2a\u5bf9\u8c61<\/strong>\u3002\u8fd9\u610f\u5473\u7740<code>is<\/code>\u64cd\u4f5c\u7b26\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f1a\u8fd4\u56deTrue\u3002\u4f8b\u5982\uff0c\u5982\u679c<code>a = b = [1,2,3]<\/code>\uff0c\u90a3\u4e48 <code>a is b<\/code> \u5c06\u8fd4\u56deTrue\uff0c\u56e0\u4e3aa\u548cb\u90fd\u5f15\u7528\u5217\u8868 <code>[1,2,3]<\/code> \u5728\u5185\u5b58\u4e2d\u7684\u540c\u4e00\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u7136\u800c\uff0c\u5f53\u4e24\u4e2a\u76f8\u540c\u5185\u5bb9\u7684\u72ec\u7acb\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u5360\u636e\u4e0d\u540c\u4f4d\u7f6e\u65f6\uff0c<code>is<\/code> \u5c06\u8fd4\u56deFalse\u3002\u5373\u4f7f\u8fd9\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u5b8c\u5168\u76f8\u540c\uff0c\u5b83\u4eec\u4e5f\u88ab\u89c6\u4e3a\u4e0d\u540c\u7684\u5b9e\u4f53\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001==\u7684\u4f7f\u7528\u548c\u539f\u7406<\/h3>\n<\/p>\n<p><p>\u76f8\u6bd4\u4e4b\u4e0b\uff0c<code>==<\/code> \u64cd\u4f5c\u7b26\u662f\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002\u5b83\u4e0d\u5173\u5fc3\u8fd9\u4e24\u4e2a\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u662f\u5426\u5360\u636e\u540c\u4e00\u4f4d\u7f6e\u3002\u53ea\u8981\u5bf9\u8c61\u7684\u5185\u5bb9\u76f8\u540c\uff0c<code>==<\/code> \u5c31\u4f1a\u8fd4\u56deTrue\u3002<\/p>\n<\/p>\n<p><p><strong>\u8fd9\u610f\u5473\u7740\uff0c\u5373\u4fbf\u4e24\u4e2a\u5bf9\u8c61\u5728\u7269\u7406\u4e0a\u662f\u72ec\u7acb\u7684\uff0c\u53ea\u8981\u5b83\u4eec\u7684\u5185\u5bb9\u76f8\u7b49\uff0c<code>==<\/code>\u5c31\u8ba4\u4e3a\u5b83\u4eec\u76f8\u7b49<\/strong>\u3002\u4f8b\u5982\uff0c\u6709\u4e24\u4e2a\u5217\u8868 <code>a = [1, 2, 3]<\/code> \u548c <code>b = [1, 2, 3]<\/code>\uff0c\u5373\u4f7f <code>a<\/code> \u548c <code>b<\/code> \u5728\u5185\u5b58\u4e2d\u4f4d\u4e8e\u4e0d\u540c\u7684\u4f4d\u7f6e\uff0c<code>a == b<\/code> \u4e5f\u4f1a\u8fd4\u56deTrue\uff0c\u56e0\u4e3a\u5217\u8868\u7684\u5185\u5bb9\u662f\u76f8\u540c\u7684\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u884c\u4e3a\u7279\u522b\u9002\u7528\u4e8e\u57fa\u4e8e\u503c\u7684\u6bd4\u8f83\uff0c\u6bd4\u5982\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u6216\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u7b49\u3002\u5728\u7f16\u7a0b\u5b9e\u8df5\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u66f4\u5173\u5fc3\u53d8\u91cf\u7684\u503c\u800c\u4e0d\u662f\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4e3e\u4f8b\u8bf4\u660e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3 <code>is<\/code> \u548c <code>==<\/code> \u7684\u533a\u522b\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e9b\u5b9e\u9645\u7684\u4f8b\u5b50\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f8b\u5b501\uff1a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>y = 10<\/p>\n<p>print(x is y)  # \u8f93\u51fa: True<\/p>\n<p>print(x == y)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>x<\/code> \u548c <code>y<\/code> \u90fd\u6307\u5411\u6570\u503c <code>10<\/code>\u3002\u7531\u4e8ePython\u5bf9\u5c0f\u7684\u6574\u6570\u548c\u5b57\u7b26\u4e32\u7b49\u57fa\u672c\u6570\u636e\u7c7b\u578b\u505a\u4e86\u7f13\u5b58\uff0c\u56e0\u6b64 <code>x<\/code> \u548c <code>y<\/code> \u5728\u5185\u5b58\u4e2d\u5b9e\u9645\u4e0a\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u8fd9\u5c31\u5bfc\u81f4 <code>is<\/code> \u548c <code>==<\/code> \u90fd\u8fd4\u56deTrue\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f8b\u5b502\uff1a\u5bb9\u5668\u6570\u636e\u7c7b\u578b\u7684\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = [1, 2, 3]<\/p>\n<p>b = [1, 2, 3]<\/p>\n<p>print(a is b)  # \u8f93\u51fa: False<\/p>\n<p>print(a == b)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>a<\/code> \u548c <code>b<\/code> \u662f\u5185\u5bb9\u76f8\u540c\u4f46\u5728\u5185\u5b58\u4e2d\u4f4d\u7f6e\u4e0d\u540c\u7684\u4e24\u4e2a\u5217\u8868\u3002\u56e0\u6b64\uff0c<code>a is b<\/code> \u8fd4\u56deFalse\uff0c\u800c <code>a == b<\/code> \u8fd4\u56deTrue\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u9002\u7528\u573a\u666f\u548c\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u9009\u62e9\u4f7f\u7528 <code>is<\/code> \u6216 <code>==<\/code> \u5e94\u6839\u636e\u5177\u4f53\u573a\u666f\u548c\u9700\u6c42\u6765\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u4f7f\u7528 <code>is<\/code> \u4e3b\u8981\u9002\u5408\u4e8e\u68c0\u67e5\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u4e3aNone\u6216\u8005\u68c0\u67e5\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61<\/strong>\u3002\u4f8b\u5982\uff0c<code>if var is None:<\/code> \u5f80\u5f80\u6bd4 <code>if var == None:<\/code> \u66f4\u5feb\uff0c\u4e5f\u66f4\u7b26\u5408Python\u7684\u60ef\u7528\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528 <code>==<\/code> \u5219\u66f4\u9002\u5408\u4e8e\u57fa\u4e8e\u503c\u7684\u6bd4\u8f83<\/strong>\uff0c\u65e0\u8bba\u662f\u6bd4\u8f83\u57fa\u672c\u6570\u636e\u7c7b\u578b\u8fd8\u662f\u6bd4\u8f83\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u7406\u89e3 <code>is<\/code> \u548c <code>==<\/code> \u7684\u533a\u522b\u5bf9\u4e8e\u7f16\u5199\u9ad8\u8d28\u91cf\u3001\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u7684Python\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002<strong>\u901a\u8fc7\u6070\u5f53\u5730\u4f7f\u7528\u8fd9\u4e24\u4e2a\u64cd\u4f5c\u7b26\uff0c\u53ef\u4ee5\u66f4\u7cbe\u786e\u5730\u63a7\u5236\u7a0b\u5e8f\u7684\u903b\u8f91\u6d41\u7a0b\u548c\u6570\u636e\u6bd4\u8f83\u884c\u4e3a<\/strong>\u3002\u5f53\u4f60\u9700\u8981\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u8eab\u4efd\u65f6\uff0c\u4f7f\u7528 <code>is<\/code>\uff1b\u5f53\u4f60\u9700\u8981\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u65f6\uff0c\u4f7f\u7528 <code>==<\/code>\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. is\u548c==\u5728Python\u4e2d\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\nis\u548c==\u90fd\u662fPython\u4e2d\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u8fd0\u7b97\u7b26\uff0c\u4f46\u5b83\u4eec\u7684\u533a\u522b\u5728\u4e8e\u6240\u6bd4\u8f83\u7684\u5185\u5bb9\u548c\u65b9\u5f0f\u3002<\/p>\n<p><strong>2. \u4e3a\u4ec0\u4e48\u5728Python\u4e2d\u8981\u533a\u5206is\u548c==\u8fd0\u7b97\u7b26\uff1f<\/strong><br \/>\n\u5728Python\u4e2d\uff0cis\u8fd0\u7b97\u7b26\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u8eab\u4efd\u6807\u8bc6\uff0c\u4e5f\u5c31\u662f\u5224\u65ad\u5b83\u4eec\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5185\u5b58\u5730\u5740\u3002\u800c==\u8fd0\u7b97\u7b26\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002\u8fd9\u79cd\u533a\u5206\u662f\u56e0\u4e3a\u5728Python\u4e2d\uff0c\u53d8\u91cf\u662f\u6307\u5411\u5185\u5b58\u4e2d\u5bf9\u8c61\u7684\u5f15\u7528\uff0cis\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5bf9\u6bd4\u5bf9\u8c61\u4e4b\u95f4\u7684\u5185\u5b58\u5730\u5740\u6765\u5224\u65ad\u5b83\u4eec\u662f\u5426\u76f8\u540c\uff0c\u800c==\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5bf9\u6bd4\u5bf9\u8c61\u7684\u503c\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u9009\u62e9\u5728Python\u4e2d\u4f7f\u7528is\u8fd8\u662f==\u8fd0\u7b97\u7b26\uff1f<\/strong><br \/>\n\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e94\u8be5\u4f7f\u7528==\u8fd0\u7b97\u7b26\u6765\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002\u56e0\u4e3a\u5bf9\u8c61\u7684\u503c\u76f8\u7b49\u5e76\u4e0d\u610f\u5473\u7740\u5b83\u4eec\u5fc5\u7136\u6307\u5411\u540c\u4e00\u4e2a\u5185\u5b58\u5730\u5740\u3002\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u5224\u65ad\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u5b8c\u5168\u76f8\u540c\uff0c\u5373\u5b83\u4eec\u7684\u8eab\u4efd\u6807\u8bc6\u548c\u503c\u90fd\u76f8\u7b49\uff0c\u90a3\u4e48\u6211\u4eec\u5e94\u8be5\u4f7f\u7528is\u8fd0\u7b97\u7b26\u8fdb\u884c\u6bd4\u8f83\u3002\u5f53\u6211\u4eec\u9700\u8981\u5224\u65ad\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528is\u8fd0\u7b97\u7b26\u5feb\u901f\u6bd4\u8f83\u5b83\u4eec\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0cis \u548c == \u662f\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u64cd\u4f5c\u7b26\uff0c\u4f46\u5b83\u4eec\u4e4b\u95f4\u6709\u7740\u6839\u672c\u7684\u533a\u522b\uff1ais\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684 [&hellip;]","protected":false},"author":3,"featured_media":177289,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/177282"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=177282"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/177282\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/177289"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=177282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=177282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=177282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}