1. 与类有关的几个函数
hasattr() #判断对象object是否包含名为name的特性getattr() # 得到对象的某个属性值 (获取器)setattr() # 给对象添加某个属性值 (设置器 ),(实例调用,只对当前实例有用),类调用,对所有实例有用delattr() # 删除对象属性 (删除器)isinstance() # 检查对象是否是类的对象,返回True或Falseissubclass() # 检查一个类是否是另一个类的子类。返回True或False复制代码
代码示例:
class A(): name=1 def test(self): raise IOError("错误")print(hasattr(A,"nam")) #Falseprint(getattr(A,"name")) #1setattr(A,"name","xfy")print(getattr(A,"name")) #xfydelattr(A,"name")setattr(A,"name","xfy")print(getattr(A,"name"))#xfyprint(hasattr(A,"test"))#Trueprint(getattr(A,"test")) #a=A()print(isinstance(a,A)) #True复制代码
注:
- setattr:实例调用,只对当前实例有用,类调用,对所有实例有用。
- delattr方法只能传类,不能传实例
- hasattr传入类就是查看该类是否有某属性,传实例是查看实例对应的该类是否有某属性。(所以就是无论怎样都是查看类),在python类的定义中方法也是一种特殊的属性。
再看示例:
a.name=12 # 并不会修改类的nameprint(a.name) #12print(A.name)#1复制代码
实例修改类变量只是改了自己当前范围的值,并不能改变累的属性。
2. 类的特殊方法
#类属性: __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) #x*y __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 不v复制代码
示例:
class B(): ''' 这是一个测试用的类 ''' def __init__(self,name,age): self.name=name self.age=age def __str__(self): return "这是一个B类" def __call__(self, *args, **kwargs): return self.nameb=B("xfy",20)print(B.__doc__)#这是一个测试用的类print(b.__dict__)#{'name': 'xfy', 'age': 20}print(b)#这是一个B类print(b.__repr__())#<__main__.B object at 0x000001AC26405F28>print(b.__str__())#这是一个B类print(b())#xfy复制代码
注: __repr__和__str__的区别
__repr__是给程序员和机器看的。
__str__可重写给用户看了。
实现两个实例像数字一样相加
示例:
#为什么1+2,但是b+c不行#我们来看dir(1)print(dir(1))#['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']#1有一个__add__接口,如果我们也能在B里实现这个__add__,不也能跟随我们的意愿做数学操作了吗?class B(): def __init__(self,name,age): self.name=name self.age=age def __add__(self, other): return self.age+other.ageb=B("xfy",20)c=B("stefan",21)print(b+c)#41,b+c是两个人年龄的和复制代码
所以只要我们在类里加了相应的接口就能一些特殊的功能。