0

My question is: ID of the class and ID instance is the same, why is false when it is set to is?

class _Class:
    def __init__(self):
        self.result = 0

    def testInstace(self, Instance1):
        print("self id :", id(self))
        print("inst id :", id(Instance1))

        if(id(self) == id(Instance1)):
            print("if is: ", id(self) is id(Instance1))
            print("if ==: ", id(self) == id(Instance1))

            print("type: ", type((self)), "\ttype", type((Instance1)))

if __name__ == '__main__':
    cal1 = _Class()
    cal1.testInstace(cal1)

result :

self id : 2098081049952
inst id : 2098081049952
if is:  False
if ==:  True
type:  <class '__main__._Class'>    type <class '__main__._Class'>
JHJ
  • 1
  • 1
    "ID of the class and ID instance is the same" No, they are not. `self` and `Instance1` are the same object, so they have the same ID. But `self` **is not** "the class"; it is the **instance of** the class upon which the method is called. That said: there is nothing strange about the behaviour here. `is` has **nothing to do with** equality. You are asking whether the *integer object* that you get back from `id` is the *same object* in both cases. And - why would it be? – Karl Knechtel Mar 08 '22 at 02:04
  • Also, instead of checking whether the _integer object_ being returned by `id` is the same with `id(self) is id(Instance1)`, you could check whether the _actual objects_ `self` and `Instance1` are the same with `self is Instance1`. Or `isinstance(obj, _Class)`. – aneroid Mar 08 '22 at 02:48
  • @KarlKnechtel The `is` statement is invalid. `id(self)` is of type `integer` whose `return` value is an address value, id(Instance1) is also of type `integer` where `return` value is an address value, In the case of `==`, since it has the same value as `integer`, `return` `true` and In the case of `is`, is it okay to think that the address of the `return value` may have a different address? That is, if the address value of `id(self)` is 0x12341234 and `value` is 3146062344304 Can you think of the case where the address value of `id(instance)` is 0x1111111 and `value` is 3146062344304? – JHJ Mar 08 '22 at 05:25
  • @aneroid It helped a lot. Thank you, aneroid. but. isinstance() arg 2 must be a type or tuple of types So, `self` and `Instance1` cannot be resolved with isInstance. – JHJ Mar 08 '22 at 05:34
  • @KarlKnechtel That means `self` object is not equal to `Instance1` object right? To add, like a call of reference. right? – JHJ Mar 08 '22 at 05:42
  • 1
    `is` works correctly but you use it in wrong way; You should check `self is Instance1` instead of `id(self) is id(Instance1)` because your code works like `a = id(self)` `b = id(Instance1)` and `a is b` - it creates two objects `a`, `b` with the same value but with different addresses in memory, and `is` check addresses of these objects. You doesn't compare if `self` and `instances1` is the same object but you compare if `a` and `b` is the same object. – furas Mar 08 '22 at 19:55
  • "...and In the case of `is`, is it okay to think that the address of the return value may have a different address?" I had some difficulty with your English, but I think you understand it correctly. – Karl Knechtel Mar 09 '22 at 02:28
  • @furas I understood perfectly. thank you!! :D – JHJ Mar 10 '22 at 01:32
  • @KarlKnechtel I understood perfectly. thank you!! :D – JHJ Mar 10 '22 at 01:32

0 Answers0