class Property:
def __init__(self, *args, **kwargs):
#initialize the property with any information it needs to do get and set
def __get__(self,obj, type=None):
#logic to get from database or cache
def __set__(self,obj, value):
#logic to set the value and sync with database if necessary.
然后在你的类实体类中你有这样的东西:
class Student:
student_id = Property(...)
name = Property(...)
classes = Property(...)
一种方法(我推荐的方法)是使用descriptors。你知道吗
首先,为属性创建一个类,例如:
然后在你的类实体类中你有这样的东西:
当然,在实践中,您可能有多种属性类型。我猜SQLAlchemy是这样做的,其中列类型是描述符。你知道吗
相关问题 更多 >
编程相关推荐