[Python] dataclasses: how to ignore None values using asdict()?

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    @dataclass
    class Car:
    brand: str
    color: str


    How can I get a dict that ignore None values? Something like:

    >>> car = Car(brand="Audi", color=None)
    >>> asdict(car, some_option_to_ignore_none_values=True)
    > {'brand': 'Audi'}

    Continue reading...

Compartilhe esta Página