• 用类的内置方法实现类型检查


    1.利用重写__setattr__来实现。

    import logging
    
    logger = logging.getLogger('logger')
    streamhandler = logging.StreamHandler()
    logger.setLevel(logging.DEBUG)
    streamhandler.setLevel(logging.DEBUG)
    streamhandler.setFormatter(logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
    logger.addHandler(streamhandler)
    
    
    def typeassist(**kwargs):
        def new_cls(cls):
            def redefine(self, key, value):
                if not isinstance(value, kwargs[key]):
                    logger.log(level=logging.ERROR, msg='TypeError')
                    self.__dict__[key] = None
                else:
                    self.__dict__[key] = value
            setattr(cls, '__setattr__', redefine)
            return cls
        return new_cls
    
    
    @typeassist(name=str, age=int, result=float)
    class Student:
        def __init__(self, name, age, result):
            self.name = name
            self.age = age
            self.result = result
    
        def __str__(self):
            return str(self.__dict__)
    
    
    m = Student('sasd', 56, 45.0)
    p = Student('SSS', 12, 4)
    print(p)
    print(m)

    2.利用描述符来实现。

    import logging
    
    logger = logging.getLogger('logger')
    streamhandler = logging.StreamHandler()
    logger.setLevel(logging.DEBUG)
    streamhandler.setLevel(logging.DEBUG)
    streamhandler.setFormatter(logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
    logger.addHandler(streamhandler)
    
    
    class Descriptor:
        def __init__(self, attrname, attrtype):
            self.attrname = attrname
            self.attrtype = attrtype
    
        def __set__(self, instance, value):
            if not isinstance(value, self.attrtype):
                logger.log(level=logging.ERROR, msg='TypeError')
                self.__dict__[instance] = None
                instance.__dict__[self.attrname] = None  # 注意,此处是为了维护属性字典的统一性
            else:
                self.__dict__[instance] = value
                instance.__dict__[self.attrname] = value  # 同上
    
        def __get__(self, instance, owner):
            return self.__dict__[instance]
    
    
    def typeassist(**kwargs):
        def new_cls(cls):
            for attrname, attrtype in kwargs.items():
                setattr(cls, attrname, Descriptor(attrname, attrtype))
            return cls
        return new_cls
    
    
    @typeassist(name=str, age=int, result=float)
    class Student:
        def __init__(self, name, age, result):
            self.name = name
            self.age = age
            self.result = result
    
        def __str__(self):
            return str(self.__dict__)
    
    
    m = Student('sasd', 56, 45.0)
    p = Student('SSS', 12, 4)
    print(p)
    print(m)
  • 相关阅读:
    软工试水日报-纯js原生简易动态表格 3/15
    软工试水日报-Echarts动态柱形图 3/14
    大二下学期第一次结对作业(第一阶段)
    大二下学期之第一次结对作业(第一阶段)
    大二下每周总结
    大二下学期之第一次结对作业(第一阶段)
    大二下学期第一次结对作业(第一阶段)
    大二下学期第一次结对作业(第一阶段)
    大二下学期每日总结之第一次个人作业(第三阶段)
    大二下学期每日总结之第一次个人作业(第三阶段)
  • 原文地址:https://www.cnblogs.com/poincare/p/9094746.html
Copyright © 2020-2023  润新知