与类有关的几个函数
hasattr() # 判断对象object是否包含名为name的特性
getattr() # 得到对象的某个属性值 (获取器)
setattr() # 给对象添加某个属性值 (设置器 )
delattr() # 删除对象属性 (删除器)
isinstance() # 检查对象是否是类的对象,返回True或False
issubclass() # 检查一个类是否是另一个类的子类。返回True或False
image.png
image
类的特殊方法
- 类属性
__dict__ # 类的属性(包含一个字典,由类的数据属性组成)
__doc __ # 类的文档字符串 - 类方法
__init __ # 初始化
__repr __ # 直接返回这个对象 repr() 函数就是调用对象的这个方法
__str __ # print(obj) 如果类里面定义了 __repr __,没有定义 __str __ print(obj)也会返回 __repr __的内容,或者说 __repr __的优先级更高
__call __ # Obj() 使实例可被调用 - 类属性
__add__(self,other) #x+y
__sub__(self,other) #x-y
__mul__(self,other) #xy
__mod__(self,other) #x%y
__iadd__(self,other) #x+=y
__isub__(self,other) #x-=y
__radd__(self,other) #y+x
__rsub__(self,other) #y-x
__imul__(self,other) #x=y
__imod__(self,other) #x%=y