在普通的Python类中,我可以定义类属性,例如:
class Example:
x = 3
def __init__(self):
pass
如果我执行Example.x
或Example().x
,那么输出结果将是3
。
当我继承pydantic的BaseModel
时,我无法确定如何定义类属性,因为通常定义类属性的方式会被BaseModel覆盖。
例如:
class Example(BaseModel):
x = 3
print(Example.x)
--> type object 'Example' has no attribute 'x'
我希望能够定义类级别的属性。请问这个正确的方法或语法是什么?