1.item系列方法
# class Foo: # def __getitem__(self, item): # print('getitem',item) # 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) # class Foo: def __getitem__(self, item): print('getitem',item) 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) f1 = Foo() print(f1.__dict__) f1.name = 'egon' # f1['name'] = 'egon' f1['age'] = 18 # print(f1.__dict__) # del f1.name # print(f1.age) # del f1['name'] # print(f1.__dict__) print(f1['age']) raise StopAsyncIteration # f1=Foo() # print(f1.__dict__) # # f1.name='egon' #---->setattr-------->f1.__dict__['name']='egon' # f1['name']='egon'#--->setitem--------->f1.__dict__['name']='egon' # f1['age']=18 # # print('===>',f1.__dict__) # # # del f1.name # # print(f1.__dict__) # # # # print(f1.age) # del f1['name'] # print(f1.__dict__) # # print(f1['age']) # raise S
2.slots属性
class Foo: __slots__ = ['name','age'] f1 = Foo() f1.name = 'egon' print(f1.name) # class Foo: # __slots__=['name','age'] #{'name':None,'age':None} # __slots__='name' #{'name':None,'age':None} # f1=Foo() # f1.name='egon' # print(f1.name) # f1.age=18 #--->setattr----->f1.__dict__['age']=18 f1.age = 18 # print(f1.__dict__) print(Foo.__slots__) print(f1.__slots__) f1.name = 'egon' f1.age = 17 print(f1.name) print(f1.age) # print(f1.__dict__) # print(Foo.__slots__) # print(f1.__slots__) # f1.name='egon' # f1.age=17 # print(f1.name) # print(f1.age) # f1.gender='male' # f1.gender = 'male' f2 = Foo() print(f2.__slots__) f2.name = 'alex' f2.age = 18 print(f2.name) print(f2.age) # f2=Foo() # print(f2.__slots__) # f2.name='alex' # f2.age=18 # print(f2.name) # print(f2.age)
3.内置方法
class Foo: x = 1 def __init__(self,y): self.y = y def __getattr__(self, item): print('------> from getattr:你找的属性不存在') def __setattr__(self, key, value): print('-----> from setattr') self.__dict__[key] = value def __delattr__(self, item): print('-----> from delattr') self.__dict__.pop(item) f1 = Foo(10) # class Foo: # x=1 # def __init__(self,y): # self.y=y # # def __getattr__(self, item): # print('----> from getattr:你找的属性不存在') # # # def __setattr__(self, key, value): # print('----> from setattr') # # self.key=value #这就无限递归了,你好好想想 # # self.__dict__[key]=value #应该使用它 # # def __delattr__(self, item): # print('----> from delattr') # # del self.item #无限递归了 # self.__dict__.pop(item) # # # f1=Foo(10) # f1.x
4.描述符
# class Foo: # def __get__(self, instance, owner): # print('===>get方法') # def __set__(self, instance, value): # print('===>set方法',instance,value) # instance.__dict__['x']=value #b1.__dict__ # def __delete__(self, instance): # print('===>delete方法') class Foo: def __get__(self, instance, owner): print('===>get方法') def __set__(self, instance, value): print('===>set方法',instance,value) instance.__dict__['x'] = value def __delete__(self, instance): print('===>delete方法') class Bar: x = Foo() def __init__(self,n): self.x = n b1 = Bar(10) print(b1.__dict__) b1.x = 11111111111 print(b1.__dict__) # class Bar: # x=Foo() #在何地? # def __init__(self,n): # self.x=n #b1.x=10 # b1=Bar(10) # print(b1.__dict__) # b1.x=11111111111111111 # print(b1.__dict__) # b1.y = 111111111111111111111111 print(b1.__dict__) print(Bar.__dict__) # b1.y=11111111111111111111111111111111111111 # print(b1.__dict__) # print(Bar.__dict__) b1 = Bar() b1.x = 1 # del b1.x print(b1.x) #在何时? # b1=Bar() # b1.x # # b1.x=1 # # del b1.x # print(b1.x) # # b1.x=1 # print(b1.__dict__) # # del b1.x
5.描述符优先级
# class Foo: # def __get__(self, instance, owner): # print('===>get方法') # # def __set__(self, instance, value): # print('===>set方法',instance,value) # # def __delete__(self, instance): # print('===>delete方法') # class Foo: # def __get__(self, instance, owner): # print('===>get方法') # def __set__(self, instance, value): # print('===>set方法',instance,value) # # instance.__dict__['x']=value #b1.__dict__ # def __delete__(self, instance): # print('===>delete方法') # # # class Bar: # x=Foo() #在何地? # print(Bar.x) # Bar.x=1 # print(Bar.__dict__) # print(Bar.x) # print(Bar.__dict__) # b1=Bar() # b1.x #get # b1.x=1 # set # del b1.x # delete # b1=Bar() # Bar.x=111111111111111111111111111111111111111 # b1.x # # del Bar.x # b1.x class Foo: def __get__(self, instance, owner): print('===>get方法') def __delete__(self, instance): print('===>delete方法') class Bar: x = Foo() def __getattr__(self, item): print('-------->') b1 = Bar() # b1.x = 1 print(b1.__dict__) # class Foo: # def __get__(self, instance, owner): # print('===>get方法') # # # # def __delete__(self, instance): # # print('===>delete方法') # # # class Bar: # x=Foo() #在何地? # def __getattr__(self, item): # print('----->') # # b1=Bar() # b1.x=1 # print(b1.__dict__) # b1.xxxxxxxxxxxxxxxxxxxxxxx
6.改变对象的字符串显示
# l=list('hello') # # print(l) # file=open('test.txt','w') # print(file) # class Foo: # def __init__(self,name,age): # self.name=name # self.age=age # def __str__(self): # return '名字是%s 年龄是%s' %(self.name,self.age) # # f1=Foo('egon',18) # print(f1) #str(f1)--->f1.__str__() # # x=str(f1) # print(x) # # y=f1.__str__() # print(y) class Foo: def __init__(self,name,age): self.name = name self.age = age def __str__(self): return '折是str' def __repr__(self): return '名字是%s 年龄是%s' %(self.name,self.age) f1 = Foo('as',12) f1.__repr__() # class Foo: # def __init__(self,name,age): # self.name=name # self.age=age # # def __str__(self): # # return '折是str' # def __repr__(self): # return '名字是%s 年龄是%s' %(self.name,self.age) # # f1=Foo('egon',19) # #repr(f1)---->f1.__repr__() # print(f1) #str(f1)---》f1.__str__()------>f1.__repr__()
7.析构方法
# class Foo: # def __init__(self,name): # self.name=name # def __del__(self): # print('我执行啦') # # f1=Foo('alex') # # # del f1 #删除实例会触发__del__ # del f1.name #删除实例的属性不会触发__del__ # print('--------------------->') # # #程序运行完毕会自动回收内存,触发__del__ class Foo: def __init__(self,name): self.name = name def __del__(self): print('我执行啦') f1 = Foo('andy') # del f1.name
8.自定义格式化方法format
# x='{0}{0}{0}'.format('dog') # # print(x) # class Date: # def __init__(self,year,mon,day): # self.year=year # self.mon=mon # self.day=day # d1=Date(2016,12,26) # # x='{0.year}{0.mon}{0.day}'.format(d1) # y='{0.year}:{0.mon}:{0.day}'.format(d1) # z='{0.mon}-{0.day}-{0.year}'.format(d1) # print(x) # print(y) # print(z) # x='{0.year}{0.mon}{0.day}'.format(d1) # y='{0.year}:{0.mon}:{0.day}' # z='{0.mon}-{0.day}-{0.year}' # format_dic={ # 'ymd':'{0.year}{0.mon}{0.day}', # 'm-d-y':'{0.mon}-{0.day}-{0.year}', # 'y:m:d':'{0.year}:{0.mon}:{0.day}' # } format_dic = { 'ymd':'{0.year}{0.mon}{0.day}', 'm-d-y':'{0.mon}-{0.day}-{0.year}', 'y:m:d':'{0.year}:{0.mon}:{0.day}' } class Date: def __init__(self,year,mon,day): self.year = year self.mon = mon self.day = day def __format__(self, format_spec): print('我执行啦') print('--->',format_spec) if not format_spec or format_spec not in format_dic: format_spec = 'ymd' fm = format_dic[format_spec] return fm.format(self) d1 = Date(2016,12,13) print(format(d1,'ymd')) print(format(d1,'y:m:d')) print(format(d1,'m-d-y')) print(format(d1,'m-d:y')) # class Date: # def __init__(self,year,mon,day): # self.year=year # self.mon=mon # self.day=day # def __format__(self, format_spec): # print('我执行啦') # print('--->',format_spec) # if not format_spec or format_spec not in format_dic: # format_spec='ymd' # fm=format_dic[format_spec] # return fm.format(self) # d1=Date(2016,12,26) # # format(d1) #d1.__format__() # # print(format(d1)) # print(format(d1,'ymd')) # print(format(d1,'y:m:d')) # print(format(d1,'m-d-y')) # print(format(d1,'m-d:y')) # print('===========>',format(d1,'asdfasdfsadfasdfasdfasdfasdfasdfasdfasdfasdfasdfasd')) print('=============>',format(d1,'assddd'))
9.迭代器协议
# class Foo: # def __init__(self,n): # self.n=n # def __iter__(self): # return self # # def __next__(self): # if self.n == 13: # raise StopIteration('终止了') # self.n+=1 # return self.n class Foo: def __init__(self,n): self.n = n def __iter__(self): return self def __next__(self): if self.n == 13: raise StopIteration('终止了') self.n+=1 return self.n f1 = Foo(10) print(f1.__next__()) print(f1.__next__()) # l=list('hello') # for i in l: # print(i) # f1=Foo(10) # # print(f1.__next__()) # # print(f1.__next__()) # # print(f1.__next__()) # # print(f1.__next__()) # # for i in f1: # obj=iter(f1)------------>f1.__iter__() # print(i) #obj.__next_() for i in f1: print(i)
10.迭代器协议实现斐波那契数列
# class Fib: # def __init__(self): # self._a=1 # self._b=1 # # def __iter__(self): # return self # def __next__(self): # if self._a > 100: # raise StopIteration('终止了') # self._a,self._b=self._b,self._a + self._b # return self._a class Fib: def __init__(self): self._a = 1 self._b = 1 def __iter__(self): return self def __next__(self): if self._a > 100: raise StopIteration('终止了') self._a,self._b=self._b,self._a+self._b return self._a f1 = Fib() print(next(f1)) print(next(f1)) print(next(f1)) print(next(f1)) print(next(f1)) print(next(f1)) # f1=Fib() # print(next(f1)) # print(next(f1)) # print(next(f1)) # print(next(f1)) # print(next(f1)) # print('==================================') # for i in f1: # print(i)