I am trying to detect potential bugs or refactor suggestions for comparison operations between two objects.
As far as I got it, there are 2 types of simple object Boolean equality comparison that I know of: ==
and is
.
==
is indicates if the values of two objects are the same. for example:
A=5
B=6
C=A
A==B --> returns False
A==C -> returns True
L1=[1,2,3]
L2=L1
L1==L2 --> returns True
L2=L1[:]
L1==L2 --> returns True
is
indicated if both objects point to the same location in memory. The same examples exchanging ==
with is
yields:
A=5
B=6
C=A
A is B --> returns False
A is C -> returns True
L1=[1,2,3]
L2=L1
L1 is L2 --> returns True
L2=L1[:]
L1 is L2 --> returns **False**
where the last comparison returns False because although L1 and L2 have the same values, L2 has only a copy of the data stored in L1, meaning L1 and L2 pointing to different list
objects.
My question is: For those 2 comparison types, could you think of an example where comparing objects of different types be useful for your program i.e. returns `True. Would you say that a code with comparison of 2 different types is most probably a bug?
I am using Python3
.