Why there is no UserSet class defined in Python?

Question:

Why python does not provide UserSetclass to extend and define user defined Set. It does provide UserDict,UserList, UserString but no UserSet.

I’m referring to Python Documentation

Asked By: Jyotirup

||

Answers:

PEP 3108 provides some insight into why no UserSet or similar implementations were added in similar fashion to UserDict and the like.

Guido pronounced that “silly old stuff” is to be deleted from the
stdlib for Py3K. This is open-ended on purpose. Each module to be
removed needs to have a justification as to why it should no longer be
distributed with Python.

UserDict, UserList, and UserString are all listed in the “Obsolete” section of PEP 3108 with the following reason noted and an indication that they were moved to the collections module.

Not as useful since types can be a superclass

Given that they were considered “obsolete”, it is unsurprising that similar classes were not implemented for other types such as sets.

Since types can be superclasses and abstract base classes provide a reasonable alternative for creating subclasses of various set, sequence, mapping, and other types with significant changes that would be difficult to implement by extending the built-in types themselves, it seems unnecessary to include these “starter” classes for each and every type beyond what is already provided in collections.abc.

Following is a very basic implementation of UserSet with a data attribute that stores the contents of the class in a real set (similar to the approach taken with UserDict and the like). While it might be useful to you to have such an implementation included in the standard library, it is not terribly difficult to implement yourself (and if everything that was potentially “useful” to someone was included in the standard library, Python would become awfully bloated).

from collections.abc import Hashable, MutableSet

class UserSet(Hashable, MutableSet):
    __hash__ = MutableSet._hash

    def __init__(self, iterable=()):
        self.data = set(iterable)

    def __contains__(self, value):
        return value in self.data

    def __iter__(self):
        return iter(self.data)

    def __len__(self):
        return len(self.data)

    def __repr__(self):
        return repr(self.data)

    def add(self, item):
        self.data.add(item)

    def discard(self, item):
        self.data.discard(item)


s = UserSet([1,1,2,3])
print(s)
# {1, 2, 3}
Answered By: benvc

I’m not sure whether it is widely known or not but I managed to create and successfully use a subclass of the built-in set without the need to manually manipulate with the underlying data using the self.data attribute or similar. Working with the data is mediated using the self attribute itself. I’m yet to find the drawbacks of such solution.

class IntegerSet(set):
    def sum(self) -> int:
        return sum(self)


iset = IntegerSet((1, 1, 2, 3))
iset.add(4)
print(iset.sum())  # 10
Answered By: Jeyekomon
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.