0基础学Pythone(5)——描述符相关的魔术方法

来源:互联网 发布:wordpress cms 主题 编辑:程序博客网 时间:2024/05/04 06:50
 声明一个人类:     使用描述符管理用户的身份证号:
 获取身份证号 隐藏出生年日
 设置身份证号 18位数而且年月日必须符合规则
  删除身份证号 此人死亡允许删除,不死亡禁止删除




class Human:
    name = '***'
    def __init__(self):
        self.sfzh = '101010199909090909'
    def __get__(self,obj,cls):
        res = str(self.sfzh[:6]) + '******' + str(self.sfzh[-4:])
        return res
    def __set__(self,obj,value):
        mouth = value[11:12]
        day = value[13:14]
        if 1 <= int(mouth) <= 12 and 1 <= int(day) <= 31:
            self.sfzh = value
        else:
            print('NoNoNo')
    def __delete__(self, obj):
        if obj.live:
            pass
        else:
            del self.sfzh


class Hhh:
    ren = Human()
    live = True  #True就是活着
h = Hhh()
#print(h.ren)


h.ren = '111111199811111111'
print(h.ren)
#del h.ren
#print(h.ren)
原创粉丝点击