0

Two items may be unequal in many ways. Can python tell what is the reason?

For example: 5 is not 6, int(5) is not float(5), "5" is not "5 ", ...


Edit: I did not ask what kinds of equality test there are, but why and how those are not equal. I think my question is not duplicate.

PlasticDuck
  • 105
  • 1
  • 6
  • `is` is identity, not equality. – Ignacio Vazquez-Abrams Apr 27 '16 at 08:14
  • I'm not sure what it is that you're trying to ask, but hopefully the dupe will clarify some of your concerns. If not, please edit the question to explain what precisely you want to know. – jonrsharpe Apr 27 '16 at 08:15
  • Please read: http://stackoverflow.com/questions/13650293/understanding-pythons-is-operator – BlackMamba Apr 27 '16 at 08:16
  • @jonrsharpe Event though the question contains the words `is not`, I don't think it's about the operator `is`. – bereal Apr 27 '16 at 08:20
  • 1
    @bereal what *is* it about? – jonrsharpe Apr 27 '16 at 08:26
  • Do you mean you want to know precisely *why* two values evaluated as unequal, as opposed to just that they did? If so there's nothing built-in to do that AFAIK, but you could write something to check simple cases (different types, same type different value, ...) – jonrsharpe Apr 27 '16 at 08:27
  • @jonrsharpe yeah, that was my guess, too. – bereal Apr 27 '16 at 08:29
  • 1
    @bereal given the... varied quality of answers so far I think it's better to stay closed until edited, although I'm also no longer convinced that the dupe is correct. – jonrsharpe Apr 27 '16 at 08:30
  • Maybe this is a Perl person, where `6 + "5" + "5 "` gives 16, but `6 . "5" . "5 "` gives 655. – cdarke Apr 27 '16 at 08:31
  • jonsharpe, I wanted to ask if python program is able to put out more info why and how items are not equal. I did not ask different ways to test equality, and that's why I don't think my question is duplicate. – PlasticDuck Apr 27 '16 at 11:13
  • What do you mean *"if the Python program is able to..."*? As I said above, you can certainly *write a function* to investigate what the difference is, but Python, given `a == b`, will just tell you whether or not they're equal. You could also look into `py.test`, which does some interesting things around assertions to give you more information when your tests fail. – jonrsharpe Apr 27 '16 at 11:14
  • I meant, a function (built in or made by user) that would answer "why and how items are not equal". I think you repeat answer to something I did not ask. Would you please mark the question not duplicate, because it is not clear to you what I asked? – PlasticDuck Apr 27 '16 at 11:23
  • `print('{} is not {}'.format(a, b)')` – Matthias Apr 27 '16 at 11:53

2 Answers2

2

There are several checks you can do:

# Both variables point to the same object (same memory space)
a is b

# Both variables evaluates to the same value
a == b

# Both variables are of same type
type(a) == type(b)
Jérôme
  • 13,328
  • 7
  • 56
  • 106
0

is checks if the two items are the exact same object. This check identity

== checks if the two objects are equal values

You use is not None to make sure that the object the "real" none and not just false-y.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Keatinge
  • 4,330
  • 6
  • 25
  • 44