我正在修改一个应用程序,try 为我的应用程序模型使用PYDANIC,为我的数据库模型使用SQLAlChemy.
我有现有的类,我按照别人教我的那样在__init__
方法中定义了属性:
class Measure:
def __init__(
self,
t_received: int,
mac_address: str,
data: pd.DataFrame,
battery_V: float = 0
):
self.t_received = t_received
self.mac_address = mac_address
self.data = data
self.battery_V = battery_V
在PYDANIC和SQLAlChemy中,在文档之后,我必须定义__init__
方法之外的那些属性,例如在PYDANIC中:
import pydantic
class Measure(pydantic.BaseModel):
t_received: int
mac_address: str
data: pd.DataFrame
battery_V: float
为什么会这样呢?这不是很糟糕的做法吗?对该类的其他方法(类方法、静态方法、属性...)有影响吗?
注意,这也是very unhandy,因为当我实例化该类的对象时,我没有得到关于构造函数需要哪些参数的建议!