+ 8
🆘 Maybe it's something wrong with me but I don't understand why in the 🐍 code:
class C: # C(int) --> True pass a, b = C(), C() print(a == b) # False 🅰️ & 🅱️ are different 🤔
7 Respostas
+ 8
Not if you don't implement it. Then you inherit the __eq__ implementation of 'object', and that compares id (so basically like is).
+ 6
HonFu You are reliable‼️Thx‼️
Tomorrow I'll implement __eq__ 🙂
+ 5
class C:
pass
a, b = C(), C()
print(a)
print(b)
Output shows two different memory address.
+ 5
RKK Thx, I know‼️🙂
But "==" checks equality and not identity like "is"‼️🤔
+ 5
Morning, fresh mind, fresh ideas. Yesterday, after hard day I stucked on a trivial 🐍 problem, solved already‼️
Thx HonFu, RKK, rodwynnejones ‼️🙂
"Don't think that you can think when you can't think - take a rest‼️"
class C: # __eq__ implemented
def __eq__(self, other): # f.ex
return True if type(self) == type(other) else False
a, b = C(), C()
print (a == b) # True
print (a is b) # False
+ 3
If you haven't implemented == (__eq__) for your type, it returns the memory address of your object.
Since you create two separate objects, the result will be False.
+ 1
If your trying to check if a and b are instances of the same class then the "type" function:-
class C:
pass
a, b = C(), C()
print(type(a) == type(b)) #< outputs true