0

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.

Or b
  • 666
  • 1
  • 5
  • 22
  • 1
    Comparing between differing types is a short walk away from weak typing, and you need only look at JavaScript shenanigans for why that's often a bad idea – Cory Kramer Sep 22 '20 at 17:58
  • Well, in JavaScript '0'==0 is True :) – Cory Nezin Sep 22 '20 at 18:00
  • 1
    Also... 1.0 == 1 in about any language – Cory Nezin Sep 22 '20 at 18:01
  • Since you can override what `==` does in Python, you certainly can have objects of two different types test as equal via `==`. I'm not sure when it would ever make sense to do that. – CryptoFool Sep 22 '20 at 18:02
  • math comparisons are kinda different, as type promotion goes on there as well, which muddies the water as to if you're really comparing different types (rather than one type having been promoted to the other first) – CryptoFool Sep 22 '20 at 18:04
  • 1
    There are indeed cases where it makes sense that two objects compare equal. The most natural example is probably two different types representing a number. Another natural case is when you have two objects from different classes where one object is from a base class and the other is from a derived class, etc.. You might also have a `__eq__` method in a class that compares equally with any other second object as long as the second object has the same attribute values as the first object regardless of its type. In the end, it depends on what you decide "equal" means. – darcamo Sep 22 '20 at 18:07
  • @darcamo - Thank you for the insights regarding the `==` comparison. Do you know if there is a class method for `IS` comparison like the __eq__ method? – Or b Sep 22 '20 at 20:14
  • @CoryNezin '0'==0 returns False in Python3. 1.0==1 does return True. – Or b Sep 22 '20 at 20:18
  • 1
    @Orb Overloading `is` does not make sense and [it is not possible](https://stackoverflow.com/questions/3993239/python-class-override-is-behavior). The operator `is` compares the *identity* of the objects (if they are the same object in memory). Think like this, two twins might be equal by some measure (appearance, for instance), but they are not the same person. – darcamo Sep 22 '20 at 21:41
  • @CoryNezin Anyways the statement `1 is 1.0` returns False. – Or b Sep 24 '20 at 13:40
  • @Orb yeah because they are not literally the same object. `'asdf'*100 is 'asdf'*100` also returns false – Cory Nezin Sep 24 '20 at 14:10

1 Answers1

0

L1 is L2 returns false because L2 is a copy of list L1 not the list itself.

Ekure Edem
  • 310
  • 2
  • 10
  • I wrote it in my in question. my question was *not* why the last line returns False. – Or b Sep 24 '20 at 07:49