【发布时间】:2019-01-15 03:26:32
【问题描述】:
我想在实例创建后验证类型是对还是错,
我尝试使用@dataclass 装饰器,但不允许我使用__init__ 方法,我也尝试使用自定义类类型
还按类型的顺序进行了一些验证(例如,如果是int,则field>0 或如果是str,则为干净的空格),
我可以使用 dict 来验证类型,但我想知道是否有办法以 Python 的方式进行
class Car(object):
""" My class with many fields """
color: str
name: str
wheels: int
def __init__(self):
""" Get the type of fields and validate """
pass
【问题讨论】:
-
相关:Validating complex types in dataclasses。如果您遇到问题是因为您的类型不是像
str、int或float这样的直截了当的东西,那么该帖子中的答案可能会有所帮助。
标签: python-3.x oop python-3.7