class Integer(object): def __init__(self, age): self.age = age def __get__(self, instance, owner): print '__get__-----', self, instance, owner if instance is None: return self return self.age def __set__(self, instance, value): print '__set__-----', self, instance, value if value < 0 or type(eval(str(value))) == float: raise ValueError('Age must int and not negative ') self.age = value def __del__(self): del self.age pass class SexType(object): def __init__(self, sex): self.sex = sex def __get__(self, instance, owner): if instance is None: return self return self.sex def __set__(self, instance, value): if value not in ['M', 'W']: raise ValueError('The value must be M/W ') self.sex = value def __del__(self): del self.sex pass class Person(object): age = Integer('age') sex = SexType('sex') def __init__(self, name, sex, age): self.name = name self.sex = sex self.age = age @property def info(self): return 'Person info --name:{},--sex:{},--age:{}'.format(self.name, self.sex, self.age) A = Person(name='ttxsgoto', sex='W', age= 15 ) print A.__dict__ print Person.__dict__ print A.info ''' {'name': 'ttxsgoto'} {'info': <property object at 0x1028ad418>, '__module__': '__main__', 'age': <__main__.Integer object at 0x1028b30d0>, 'sex': <__main__.SexType object at 0x1028b3110>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None, '__init__': <function __init__ at 0x1028b2578>} __get__----- <__main__.Integer object at 0x1028b30d0> <__main__.Person object at 0x1028b3150> <class '__main__.Person'> Person info --name:ttxsgoto,--sex:W,--age:15 '''
|