zoukankan      html  css  js  c++  java
  • Python之面向对象slots与迭代器协议

    Python之面向对象slots与迭代器协议

      slots:

     1 # class People:
     2 #     x=1
     3 #     def __init__(self,name):
     4 #         self.name=name
     5 #     def run(self):
     6 #         pass
     7 
     8 
     9 # print(People.__dict__)
    10 #
    11 # p=People('alex')
    12 # print(p.__dict__)
    13 
    14 
    15 class People:
    16     __slots__=['x','y','z']
    17 
    18 p=People()
    19 print(People.__dict__)
    20 
    21 p.x=1
    22 p.y=2
    23 p.z=3
    24 print(p.x,p.y,p.z)
    25 # print(p.__dict__)
    26 
    27 p1=People()
    28 p1.x=10
    29 p1.y=20
    30 p1.z=30
    31 print(p1.x,p1.y,p1.z)
    32 print(p1.__dict__)
    View Code

      item系列:

     1 #把对象操作属性模拟成字典的格式
     2 class Foo:
     3     def __init__(self,name):
     4         self.name=name
     5     def __setattr__(self, key, value):
     6         print('setattr===>')
     7     def __getitem__(self, item):
     8         # print('getitem',item)
     9         return self.__dict__[item]
    10     def __setitem__(self, key, value):
    11         print('setitem-----<')
    12         self.__dict__[key]=value
    13     def __delitem__(self, key):
    14         self.__dict__.pop(key)
    15         # self.__dict__.pop(key)
    16     # def __delattr__(self, item):
    17     #     print('del obj.key时,我执行')
    18     #     self.__dict__.pop(item)
    19 
    20 f=Foo('George')
    21 f.name='Wang'
    22 f['name']='George'
    23 # print(f.name)
    24 # f.name='George'
    25 # f['age']=18
    26 # print(f.__dict__)
    27 #
    28 # del f['age'] #del f.age
    29 # print(f.__dict__)
    30 
    31 # print(f['name'])
    View Code

      __next__、__iter__ 实现迭代器协议:

      1 # from collections import Iterable,Iterator
      2 # class Foo:
      3 #     def __init__(self,start):
      4 #         self.start=start
      5 
      6 #     def __iter__(self):
      7 #         return self
      8 
      9 #     def __next__(self):
     10 #         return 'aSB'
     11 
     12 
     13 # f=Foo(0)
     14 # f.__iter__()
     15 # f.__next__()
     16 
     17 # print(isinstance(f,Iterable))
     18 # print(isinstance(f,Iterator))
     19 
     20 # print(next(f)) #f.__next__()
     21 # print(next(f)) #f.__next__()
     22 # print(next(f)) #f.__next__()
     23 
     24 
     25 # for i in f: # res=f.__iter__() #next(res)
     26 #     print(i)
     27 
     28 
     29 
     30 # from collections import Iterable,Iterator
     31 # class Foo:
     32 #     def __init__(self,start):
     33 #         self.start=start
     34 
     35 #     def __iter__(self):
     36 #         return self
     37 
     38 #     def __next__(self):
     39 #         if self.start > 10:
     40 #             raise StopIteration
     41 #         n=self.start
     42 #         self.start+=1
     43 #         return n
     44 
     45 
     46 # f=Foo(0)
     47 
     48 
     49 # print(next(f))
     50 # print(next(f))
     51 # print(next(f))
     52 # print(next(f))
     53 # print(next(f))
     54 # print(next(f))
     55 # print(next(f))
     56 # print(next(f))
     57 # print(next(f))
     58 # print(next(f))
     59 # print(next(f))
     60 # print(next(f))
     61 
     62 # for i in f:
     63 #     print('====>',i)
     64 
     65 
     66 
     67 
     68 # class Range:
     69 #     '123'
     70 #     def __init__(self,start,end):
     71 #         self.start=start
     72 #         self.end=end
     73 
     74 #     def __iter__(self):
     75 #         return self
     76 
     77 #     def __next__(self):
     78 #         if self.start == self.end:
     79 #             raise StopIteration
     80 #         n=self.start
     81 #         self.start+=1
     82 #         return n
     83 
     84 # for i in Range(0,3):
     85 #     print(i)
     86 
     87 
     88 
     89 # print(Range.__doc__)
     90 
     91 
     92 class Foo:
     93     '我是描述信息'
     94     pass
     95 
     96 class Bar(Foo):
     97     pass
     98 print(Bar.__doc__) #该属性无法继承给子类
     99 
    100 b=Bar()
    101 print(b.__class__)
    102 print(b.__module__)
    103 print(Foo.__module__)
    104 print(Foo.__class__) #?
    View Code
  • 相关阅读:
    十四、自定义构建购物计算组件&表单组件
    从微信小程序到鸿蒙js开发【04】——list组件
    HarmonyOS应用开发-Component体系介绍(一)
    HarmonyOS单模块编译与源码导读
    烧录失败导致boot无法加载的解决措施,再也不怕烧成砖了
    从微信小程序到鸿蒙js开发【03】——fetch获取数据&简单天气预报
    抛弃床的温暖,只为了它丨云库一款跑在鸿蒙系统上的应用丨篇二
    swift 弹窗
    Swift字符串常用操作总结
    swift 随机数
  • 原文地址:https://www.cnblogs.com/george92/p/9253320.html
Copyright © 2011-2022 走看看