In Python, if I have the following code:
r = Numeric(str)
i = int(r)
if r == i :
return i
return r
Is this equivalent to:
r = Numeric(str)
return r
Or do the == values of different types r and i give different return values r and i?
Question: "do the == values of different types r and i give different return values r and i?"
Answer: clearly they are different; they have different types.
>>> print(type(i))
<type 'int'>
>>> print(type(n))
<class '__main__.Numeric'>
In the above example, I declared a class called Numeric to have something to test. If you actually have a module that implements a class called Numeric, it won't say __main__.Numeric but something else.
If the class implements a __eq__() method function, then the results of == will depend on what that function does.
class AlwaysEqual(object):
def __init__(self, x):
self.x = x
def __eq__(self, other):
return True
With the above, we can now do:
>>> x = AlwaysEqual(42)
>>> print(x == 6*9)
True
>>> print(x == "The answer to life, the universe, and everything")
True