标签:div sub __add__ 比较 比较运算符 except ISE 逻辑运算 als
比较运算符
__cmp__(self, other) : 包含两个对象比较的所有情况
__eq__(self, other) : 判断两个对象是否相等
__It__(self, other) : 判断前者是否小于后者
__gt__(self, other) : 判断前者是否大于后者
数字运算符
__add__(self, other) : 加
__sub__(self, other) : 减
__mul__(self, other) : 乘
__div__(self, other) : 除
逻辑运算符
__or__(self, other) : 或运算
__and__(self, other) : 和运算
实例
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)
—————————————
标签:div sub __add__ 比较 比较运算符 except ISE 逻辑运算 als
原文地址:https://www.cnblogs.com/liyanyan665/p/11408543.html