相关疑难解决方法(0)

Pythonic way to check if a dataclass field has a default value

I've been using python 3.7 lately and was looking for ways to leverage the new dataclasses. Basically I had a method that iterates over the dataclass fields and checks if they have a default value:

from dataclasses import fields, MISSING

@classmethod
def from_json(cls)
    datacls_fields = fields(cls)
    for field in datacls_fields:
        if  (field.default != MISSING):
            #...
Run Code Online (Sandbox Code Playgroud)

However in the official documentation, it says:

MISSING value is a sentinel object used to detect if the default and default_factory parameters are provided. This …

python python-3.x python-dataclasses

7
推荐指数
1
解决办法
311
查看次数

标签 统计

python ×1

python-3.x ×1

python-dataclasses ×1