What are the differences between Set, FrozenSet, MutableSet and AbstractSet in python input module?

I'm trying to annotate my code with types, but I'm a little confused when it comes to sets. I read a few points in PEP 484 :

Note. Dict, List, Set, and FrozenSet are mostly useful for annotating return values. For arguments, prefer the abstract collection types defined below, for example. Display, sequence or AbstractSet.

and

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

but it doesn’t help.

My first question is: what are the common features 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 installed mypy-lang via pip.

+5
source share
3 answers

Be careful with annotations and typing. The ideas discussed in 484 are new and implemented in the typing module. This module is only available in Python3.5 (the latest typing also available from pip for Py2 and Py3).

https://docs.python.org/3/library/typing.html

This is the note that you indicated from section 484, which begins:

To open the use of static type checking in Python 3.5, as well as older versions, a uniform namespace is required. To this end, a new module is introduced in the standard library, called typing.

That in the lists of notes there are types of annotations, and not the actual classes of objects (built-in or from collections ). Do not confuse them.

Note that Dict , List , Set and FrozenSet all uppercase letters, where the functions (and type names) are Dict , List , Set , FrozenSet . In other words, to make a dictionary, you use dict() or {} , not Dict .

Annotations are new to 3.0 (not at 2.n at all). In the regular interpreter, all they do is populate the __annotations__ dictionary. There are no or no comments in the interpreter.

http://mypy-lang.org/ describes itself as an experimental input check. You need to look at the documentation to see how compatible it is with 484, etc.

https://docs.python.org/3/library/collections.abc.html#module-collections.abc contains some abstract definitions that I believe are used by typing . I have never used them. They are mainly intended for people developing new classes of objects, rather than "ordinary" users.

The typing tag for this question is probably not a good idea. He has few followers and is too general. It does not apply to this Python module.

Find [python] 484 for other SO questions regarding this annotation style.

https://github.com/python/typing - typing development repository.

There is a FrozenSet definition in this repository in the python2/typing.py (python2 backport) file, but not in src/typing.py . I am not sure of the meaning of this.

+3
source

The type of the set has been changed β€” content can be changed using methods such as add () and remove (). Since it is modified, it has no hash value and cannot be used as a dictionary key or as an element of another set. The frozenset type is immutable and hashed - its contents cannot be changed after creation; however, it can be used as a dictionary key or as an element of another set.

from: https://docs.python.org/3/library/stdtypes.html#frozenset

you do not need to enable it, it is built-in, you just do:

cities = frozenset(["Frankfurt", "Basel","Freiburg"])

checked in 3.4.2

0
source

Each of them is used for different things.

Kits are very similar to the mathematical concept of kits: https://en.wikipedia.org/wiki/Set_(mathematics)

A collection in Python is essentially a collection of unique objects. You can read more about sets, as well as see some examples here: http://www.python-course.eu/python3_sets_frozensets.php

Sets in Python are a set of unique objects (all are immutable), but FrozenSet is immutable. This means that you can change the Set, but you cannot change the FrozenSet: you need to create a new FrozenSet.

In Python3, FrozenSet is the default argument called "frozenset"

-1
source

Source: https://habr.com/ru/post/1244782/


All Articles