[Python] What are the differences between Set, FrozenSet, MutableSet and AbstractSet in python...

Discussão em 'Python' iniciado por Stack, Julho 5, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to annotate my code with types but I am a little confused when it comes to sets. I read some points in PEP 484:


    Note: Dict , List , Set and FrozenSet are mainly useful for annotating return values. For arguments, prefer the abstract collection types defined below, e.g. Mapping , Sequence or AbstractSet .
    ​

    and


    Set, renamed to AbstractSet . This name change was required because Set in the typing module means set() with generics.
    ​

    but this does not help.

    My first question is: what are the commonalities and differences between Set, FrozenSet, MutableSet and AbstractSet?

    My second question is: why if I try

    from collections import FrozenSet


    I get

    ImportError: cannot import name 'FrozenSet'


    ?

    I am using Python 3.4 and I have installed mypy-lang via pip.

    Continue reading...

Compartilhe esta Página