[Python] Default values for TypedDict

Discussão em 'Python' iniciado por Stack, Janeiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    Let's consider I have the following TypedDict:

    class A(TypedDict):
    a: int
    b: int


    What is the best practice for setting default values for this class?

    I tried to add a constructor but it doesn't seem to work.

    class A(TypedDict):
    a: int
    b: int
    def __init__(self):
    TypedDict.__init__(self)
    a = 0
    b = 1


    EDIT: I don't want to use dataclass because I need to serialize and deserialize to JSON files and dataclasses have some problem with it.

    What do you think?

    Continue reading...

Compartilhe esta Página