[Python] Python equivalent of Typescript interface

Discussão em 'Python' iniciado por Stack, Abril 19, 2025.

  1. Stack

    Stack Membro Participativo

    Recently I have been working with Typescript a lot, it allows to express things like:

    interface Address {
    street: string;
    housenumber: number;
    housenumberPostfix?: string;
    }

    interface Person {
    name: string;
    adresses: Address[]
    }

    const person: Person = {
    name: 'Joe',
    adresses: [
    { street: 'Sesame', housenumber: 1 },
    { street: 'Baker', housenumber: 221, housenumberPostfix: 'b' }
    ]
    }


    Pretty concise and giving all the luxuries as type checking and code completion while coding with Persons.

    How is this done in Python?

    I have been looking at Mypy and ABC but did not yet succeed in finding the pythonic way to do something similar as the above (my attempts resulted in way too much boilerplate to my taste).

    Continue reading...

Compartilhe esta Página