🆘 Maybe it's something wrong with me but I don't understand why in the 🐍 code: | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 🤔

18th Apr 2020, 10:04 PM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
7 Answers
+ 8
Not if you don't implement it. Then you inherit the __eq__ implementation of 'object', and that compares id (so basically like is).
18th Apr 2020, 10:30 PM
HonFu
HonFu - avatar
+ 6
HonFu You are reliable‼️Thx‼️ Tomorrow I'll implement __eq__ 🙂
18th Apr 2020, 10:36 PM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
+ 5
class C: pass a, b = C(), C() print(a) print(b) Output shows two different memory address.
18th Apr 2020, 10:12 PM
Rohit
+ 5
RKK Thx, I know‼️🙂 But "==" checks equality and not identity like "is"‼️🤔
18th Apr 2020, 10:29 PM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
+ 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
19th Apr 2020, 5:40 AM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
+ 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.
18th Apr 2020, 10:29 PM
HonFu
HonFu - avatar
+ 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
19th Apr 2020, 12:07 AM
rodwynnejones
rodwynnejones - avatar