__ne__
class Foo:
def __init__(self, num):
self.num = num
def __eq__(self, other):
return self.num == other.num
def __ne__(self, other): # __ne__ represents the != operator
return self.num != other.num
__ne__
class Foo:
def __init__(self, num):
self.num = num
def __eq__(self, other):
return self.num == other.num
def __ne__(self, other): # __ne__ represents the != operator
return self.num != other.num
__ne__
"""
Magic method __ne__ stands for not equal. So this magic method defines or
overwrites what should "!=" sign do with objects.
"""
class Test:
def __init__(self, x):
self.x = x
def __ne__(self, other):
"""
Compares attribute "x" of objects, self stands for 1st object
and other stands for object after "!=" sign. (so t is self and t2 other)
"""
return self.x != other.x # Should be used on objects of the same type
t = Test(3)
t2 = Test(2)
print(t != t2) # Now I call the __ne__ method by using "!=" sign.
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us