isinstance和issubclass,__getattribute__,__getitem__,__setitem__,delitem__,__str__(三十五)
isinstance(obj,cls)检查是否obj是否是类 cls 的对象
issubclass(sub, super)检查sub类是否是 super 类的派生类
class Foo: def __init__(self,x): self.x = x class SubFoo(Foo): def __init__(self): pass f1 = Foo(2) print(isinstance(f1,Foo)) # True print(issubclass(SubFoo,Foo)) # True
__getattribute__
当__getattribute__与__getattr__同时存在,只会执行__getattrbute__,除非__getattribute__在执行过程中抛出异常AttributeError
class Foo: def __init__(self,x): self.x=x def __getattribute__(self, item): print('不管是否存在,我都会执行') f1=Foo(10) f1.x f1.xxxxxx
class Foo: def __init__(self,x): self.x = x def __getattr__(self, item): print("执行__getattr...") #return self.__dict__[item] def __getattribute__(self, item): print("不管是否存在,都会执行__getattribute...") raise AttributeError("%s not exit" %(item)) class SubFoo(Foo): def __init__(self): pass f1 = Foo(2) f1.x f1.y ''' 不管是否存在,都会执行__getattribute... 执行__getattr... 不管是否存在,都会执行__getattribute... 执行__getattr... '''
item
class Foo: def __init__(self,name): self.Name = name def __getitem__(self, item): print('__getitem__') return self.__dict__[item] def __setitem__(self, key, value): print('__setitem__') self.__dict__[key] = value def __delitem__(self, key): print('__delitem__') self.__dict__.pop(key) f = Foo('lisi') print(f.__dict__) # {'Name': 'lisi'} f['age'] = 18 # __setitem__ print(f.__dict__) # {'Name': 'lisi', 'age': 18} print(f['Name']) # __getitem__ lisi del f['age'] # __delitem__
__str__,__repr__,__format__
改变对象的字符串显示__str__,__repr__
自定制格式化字符串__format__
class School: def __init__(self,name): self.name = name def __str__(self): return self.name scl = School("CPU") print(scl) # CPU
class School: def __init__(self,name): self.name = name # def __str__(self): # return self.name scl = School("CPU") print(scl) # <__main__.School object at 0x000001D071CF5A58>
format_dict={ 'nat':'{obj.name}-{obj.addr}-{obj.type}',#学校名-学校地址-学校类型 'tna':'{obj.type}:{obj.name}:{obj.addr}',#学校类型:学校名:学校地址 'tan':'{obj.type}/{obj.addr}/{obj.name}',#学校类型/学校地址/学校名 } class School: def __init__(self,name,addr,type): self.name=name self.addr=addr self.type=type def __repr__(self): return 'School(%s,%s)' %(self.name,self.addr) def __str__(self): return '(%s,%s)' %(self.name,self.addr) def __format__(self, format_spec): # if format_spec if not format_spec or format_spec not in format_dict: format_spec='nat' fmt=format_dict[format_spec] return fmt.format(obj=self) s1=School('oldboy1','北京','私立') print('from repr: ',repr(s1)) print('from str: ',str(s1)) print(s1) ''' str函数或者print函数--->obj.__str__() repr或者交互式解释器--->obj.__repr__() 如果__str__没有被定义,那么就会使用__repr__来代替输出 注意:这俩方法的返回值必须是字符串,否则抛出异常 ''' print(format(s1,'nat')) print(format(s1,'tna')) print(format(s1,'tan')) print(format(s1,'asfdasdffd'))
format_time = { 'YMD':'{0.year}{0.month}{0.day}', 'Y-M-D':'{0.year}-{0.month}-{0.day}', 'Y:M:D':'{0.year}:{0.month}:{0.day}', } class Date: def __init__(self,year,month,day): self.year = year self.month = month self.day = day def __format__(self, format_spec): if not format_spec or format_spec not in format_time: format_spec = 'YMD' return format_time[format_spec].format(self) d = Date(2019, 3, 2) print(format(d, 'Y-M-D')) # 2019-3-2 print(format(d, 'Y:M:D')) # 2019:3:2