Comparison operators
__cmp __ (self, other): comprising two comparison objects in all cases
__eq __ (self, other): determining whether the two objects are equal
__It __ (self, other): determines whether or not the former is smaller than the latter
__gt __ (self, other) : determining whether the former greater than the latter
number operator
__add __ (self, other): plus
__sub __ (self, other): Save
__mul __ (self, other): by
__div __ (self, other): In addition to
logical operators
__or __ (self, other ): oR operation
__and __ (self, other): calculating and
examples
class Program (object):
def __init__(self, name, age):
self.name = name
if isinstance(age, int):
self.age = age
else:
raise Exception("age must be int")
def __eq__(self, other):
if isinstance(other, Program):
if self.age == other.age:
return True
else:
return False
else:
raise Exception("the type of object must be Program")
def __add__(self, other):
if isinstance(other, Program):
return self.age + other.age
else:
raise Exception("the type of object must be Program")
if __name__ == '__main__':
p1 = Program('mike', 21)
p2 = Program('john', 20)
print(p1 == p2)
print(p1 + p2)
—————————————