__get__和__set__ 方法详解, 做验证

# 查出类的所有key值
cls.__table__.columns.keys()
cls.__table__.columns.values()
self.__class__.__name__

__get__方法详解: https://blog.csdn.net/qq_34979346/article/details/83758447

'''
class Int_validation:
def get(self, instance, owner):
return self.a

def __set__(self, instance, value):
    if isinstance(value, int) and 0 < value < 100:
        self.value = value        #这个要注意 要用value,不能用instance 否则会陷入死循环
        self.a = value        #这个要注意 要用value,不能用instance 否则会陷入死循环
    else:
        print("请输入合法的数字")

def __delete__(self, instance):
    print('结束了')

class Student:
age = Int_validation()

stu=Student()
stu.age = 50
print('>>>>>>>>>', stu.dict)
print(stu.age)

posted @ 2021-04-10 22:12  ty1539  阅读(91)  评论(0编辑  收藏  举报