{"id":304388,"date":"2021-01-15T00:27:23","date_gmt":"2021-01-14T16:27:23","guid":{"rendered":"http:\/\/4563.org\/?p=304388"},"modified":"2021-01-15T00:27:23","modified_gmt":"2021-01-14T16:27:23","slug":"a-b-%e4%b8%8eida-idb%e7%9a%84%e6%9c%ac%e8%b4%a8%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=304388","title":{"rendered":"&#8220;a == b&#8221; \u4e0e&#8221;id(a) == id(b)&#8221;\u7684\u672c\u8d28\u533a\u522b"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  &#8220;a == b&#8221; \u4e0e&#8221;id(a) == id(b)&#8221;\u7684\u672c\u8d28\u533a\u522b               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hanssx <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u7fa4\u53cb Jin \u53d1\u51fa\u4e00\u6bb5\u4ee3\u7801\uff0c<\/p>\n<pre><code>class Person:     def say(self):         pass      p1 = Person() p2 = Person()  print(p1.say, id(p1.say)) print(p2.say, id(p2.say)) print(p1.say == p2.say)  # False print(p1.say is p2.say)  # False print(p1.say is p1.say)  # False print(id(p1.say) == id(p2.say))  # True <\/code><\/pre>\n<p>\u8f93\u51fa:<\/p>\n<pre><code>&lt;bound method Person.say of &lt;__main__.Person object at 0x7f6739300898&gt;&gt; 140081602502024 &lt;bound method Person.say of &lt;__main__.Person object at 0x7f67393005c0&gt;&gt; 140081602502024 False False False True <\/code><\/pre>\n<p>python \u5b98\u7f51\u4e0a\u5bf9 id \u7684\u89e3\u91ca<\/p>\n<pre><code>id(object)  Return the \u201cidentity\u201d of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.  CPython implementation detail: This is the address of the object in memory.  Raises an auditing event builtins.id with argument id. <\/code><\/pre>\n<p>\u6211\u7406\u89e3\u5c31\u662f\u8fd4\u56de\u5185\u5b58\u5730\u5740<\/p>\n<p>StackOverFlow \u4e0a\u5bf9 is \u7684\u89e3\u91ca\uff1a https:\/\/stackoverflow.com\/questions\/132988\/is-there-a-difference-between-and-is \u4e5f\u5c31\u662f\u5f15\u7528\u6bd4\u8f83\uff0c\u4e0d\u5c31\u662f\u6bd4\u8f83\u5185\u5b58\u5730\u5740\uff1f<\/p>\n<p>\u7fa4\u53cb Sweeneys \u53d1\u4e86\u4e00\u7bc7 StackOverFlo \u7684\u7c7b\u4f3c\u95ee\u9898\u5e16\u5b50\uff1a https:\/\/stackoverflow.com\/questions\/2906177\/what-is-the-difference-between-a-is-b-and-ida-idb-in-python<\/p>\n<p>\u56de\u7b54\u4e2d\u6709\u4e00\u6bb5\u8bdd<\/p>\n<blockquote>\n<p>b.test happens to have the same id as the bound method you had before and it&#8217;s allowed to because no other objects have the same id now.<\/p>\n<\/blockquote>\n<p>\u8fd9\u610f\u601d\u5c31\u662f\u8bf4 a.test \u88ab gc \u4e4b\u540e\u6070\u597d\u91ca\u653e\u7684\u5185\u5b58\uff0c\u6b63\u597d\u88ab b.test \u6240\u4f7f\u7528\uff1f\u6240\u4ee5\u624d\u5bfc\u81f4 True \uff1f<\/p>\n<p>\u4e8e\u662f\u7fa4\u53cb Jin \u6539\u6210\u4e86\u5982\u4e0b\u4ee3\u7801\uff0c\u4f9d\u7136\u8f93\u51fa\u540c\u6837\u7684\u7ed3\u679c\uff0c<\/p>\n<pre><code>import gc gc.disable()  class Person:     def say(self):         pass      p1 = Person() p2 = Person()  print(p1.say, id(p1.say)) print(p2.say, id(p2.say)) print(p1.say == p2.say)  # False print(p1.say is p2.say)  # False print(p1.say is p1.say)  # False print(id(p1.say) == id(p2.say))  # True  <\/code><\/pre>\n<p>\u6211\u5c1d\u8bd5\u6539\u6210\u8fd9\u6837\uff0c\u4f9d\u7136\u8f93\u51fa\u540c\u6837\u7684\u7ed3\u679c\u3002<\/p>\n<pre><code>class Person:     def say(self):         pass      p1 = Person() p2 = Person()  print(p1.say, id(p1.say)) print(p2.say, id(p2.say)) print(p1.say == p2.say)  # False print(p1.say is p2.say)  # False print(p1.say is p1.say)  # False p1_ref = p1.say p2_ref = p2.say print(id(p1.say) == id(p2.say))  # True <\/code><\/pre>\n<p>\u8bdd\u8bf4\uff0cpython \u4e2d\u4e00\u4e2a\u7c7b\u4e2d\u7684 self \u65b9\u6cd5\u672c\u8eab\u5e94\u8be5\u53ea\u5728\u5185\u5b58\u4e2d\u5360\u4e00\u4e2a\u4f4d\u7f6e\u5427\uff1f\u7136\u540e\u8c03\u7528\u7684\u65f6\u5019\u628a\u5bf9\u8c61\u5730\u5740 self \u4f20\u8fdb\u6765\u8c03\u7528\uff0c\u6211\u8bb0\u5f97 C++\u662f\u8fd9\u6837\u7684\uff0c\u867d\u7136 Python \u5e95\u5c42\u662f c \u7684 struct \u5b9e\u73b0\u7684\uff0c\u4f46\u662f\u4e5f\u4e0d\u81f3\u4e8e\u8fd9\u4e48\u5f31\u667a\uff0c\u540c\u4e2a\u7c7b\u7684\u4e0d\u540c\u5bf9\u8c61\u7684 self \u65b9\u6cd5\u5360\u7528\u591a\u5757\u5185\u5b58\u5427\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>14<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5063761\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : hanssx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6539\u6210\u7fa4\u53cb\u786e\u5b9e\u5c31\u662f False \u4e86\uff0c\u4e3a\u5565\u6211\u4e0a\u9762\u6700\u540e\u5199\u7684\u90a3\u6bb5\u4e0d\u884c\uff1f<br \/>&#8220;`python<br \/>class Person:<br \/> def say(self):<br \/> pass<\/p>\n<p>p1 = Person()<br \/>p2 = Person()<\/p>\n<p>print(p1.say, id(p1.say))<br \/>print(p2.say, id(p2.say))<br \/>print(p1.say == p2.say) # False<br \/>print(p1.say is p2.say) # False<br \/>print(p1.say is p1.say) # False<br \/>p1_ref = p1.say<br \/>p2_ref = p2.say<br \/>print(id(p1_ref) == id(p2_ref)) # False<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5063762\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : AoEiuV020 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u592a\u590d\u6742\uff0c\u6ca1\u770b\u51fa\u6765\u6bcf\u4e2a\u90fd\u6539\u4e86\u5565\uff0c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5063763\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : forbxy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             class Dog:<br \/> def say(self):<br \/> pass<\/p>\n<p>d = Dog()<\/p>\n<p>id(d.say) == id(p2.say) # Ture                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5063764\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zk8802 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             func.__eq__() \u53ef\u80fd\u6bd4\u8f83\u7279\u6b8a\u3002\u6211\u731c\u5b83\u4f1a\u68c0\u67e5 self \u662f\u5426\u4e00\u81f4\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5063765\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : todd7zhang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u77e5\u9053\uff0c\u76f2\u731c id \u76f8\u7b49\u662f\u56e0\u4e3a <br \/>p1.say.im_func is p2.say.im_func <br \/>python3 \u5c31\u662f p1.say.__func__ is p2.say.__func__                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5063766\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : AlohaV2 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/stackoverflow.com\/questions\/13348031\/ids-of-bound-and-unbound-method-objects-sometimes-the-same-for-different-o                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5063767\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : AoEiuV020 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u521a\u77e5\u9053\uff0c\u8fd9\u4e2a id \u5c45\u7136\u662f\u6bcf\u6b21\u90fd\u4e0d\u4e00\u6837\u7684\uff0c\u90a3\u5c31\u4e0d\u53ef\u80fd\u662f\u8fd4\u56de\u5185\u5b58\u5730\u5740\u5565\u7684\u4e86\uff0c\u5177\u4f53\u8fd8\u662f\u8981\u770b\u8fd9\u4e2a\u65b9\u6cd5\u5e95\u5c42\u5b9e\u73b0\uff0c<br \/><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/02\/20210203_601a5b038c976.png\" rel=\"noreferrer\" alt=\"&quot;a == b&quot; \u4e0e&quot;id(a) == id(b)&quot;\u7684\u672c\u8d28\u533a\u522b\"> <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5063768\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : hanssx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @AoEiuV020 \u662f\u4e0d\u662f\u6bcf\u6b21\u5206\u914d\u7684\u5185\u5b58\u5730\u5740\u4e0d\u4e00\u6837\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5063769\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : AlohaV2 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @AoEiuV020 \u662f\u5185\u5b58\u5730\u5740\u76f8\u5173\u7684\u4e00\u4e2a\u4e1c\u897f<br \/>\u8c03\u7528 bound method \u7684\u8bdd\uff0cid(p1.say)\u5176\u5b9e\u662f\u65b0\u5efa\u4e86\u4e00\u4e2a bound method, \u91cc\u9762\u7ed1\u5b9a\u4e86 Person.say \u548c p1 \u8fd9\u4fe9\u4e1c\u897f\u3002<br \/>\u6240\u4ee5\u7c7b\u4f3c\u7684\u64cd\u4f5c\u8fd8\u6709<br \/>&#8220;`<br \/>class WhatEver:<br \/> def say(self):<br \/> pass<br \/> def run(self):<br \/> pass<\/p>\n<p>foo = WhatEver()<br \/>print(id(foo.say) == id(foo.run))<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5063770\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : todd7zhang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5b66\u5230\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5063771\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhanglintc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e3a\u5565\u8fd9\u4e2a\u662f False \u6765\u7740\uff1a<br \/>print(p1.say is p1.say) # False                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5063772\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : hanssx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u66f4\u65b0\u7fa4\u53cb Sweeneys \u56de\u590d\uff1a<br \/>\u201d<br \/>\u5bf9\uff0c\u6211 jio \u5f97\u5927\u5bb6\u4e0d\u80fd get \u7684\u70b9\u5728\u8fd9\uff0c\u5373\u6bcf\u6b21 instance.method_name \u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u65b9\u6cd5\u5bf9\u8c61\uff08 Method Object \uff09\u3002\u8fd8\u6709\u4e00\u70b9\u5c31\u662f\u751f\u547d\u5468\u671f\u7684\u95ee\u9898\uff0c\u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u4e0d\u91cd\u53e0\uff0c\u90a3\u4e48 id(object)\u5f97\u5230\u7684\u503c\u53ef\u80fd\u4f1a\u4e00\u6837\u3002<br \/>\u201c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5063773\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : milkpuff <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/github.com\/satwikkansal\/wtfpython#-strings-can-be-tricky-sometimes<br \/>github \u4e0a\u7684\u4e00\u4e2a\u4e13\u95e8\u603b\u7ed3 python \u5947\u7279\u7279\u6027\u7684\u4ed3\u5e93\uff0c\u53eb\u505a wtfpython\uff0c\u6709 23k star                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5063774\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : hanssx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @milkpuff \u6709\u70b9\u610f\u601d                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;a == b&#038;#82&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/304388"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=304388"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/304388\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304388"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}