day27-python之迭代器协议
程序员文章站
2023-12-24 20:08:39
1.item系列方法 2.slots属性 3.内置方法 4.描述符 5.描述符优先级 6.改变对象的字符串显示 7.析构方法 8.自定义格式化方法format 9.迭代器协议 10.迭代器协议实现斐波那契数列 ......
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)