简体   繁体   English

为什么 Python 中没有定义 UserSet 类?

[英]Why there is no UserSet class defined in Python?

Why python does not provide UserSet class to extend and define user defined Set.为什么python不提供UserSet类来扩展和定义用户定义的Set。 It does provide UserDict , UserList , UserString but no UserSet .它确实提供UserDictUserListUserString但没有UserSet

I'm referring to Python Documentation我指的是Python 文档

PEP 3108 provides some insight into why no UserSet or similar implementations were added in similar fashion to UserDict and the like. PEP 3108提供了一些关于为什么没有以类似于UserDict等方式添加UserSet或类似实现的见解。

Guido pronounced that "silly old stuff" is to be deleted from the stdlib for Py3K. Guido 宣布将从 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.要删除的每个模块都需要有理由说明为什么它不应再与 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. UserDictUserListUserString都列在 PEP 3108 的“过时”部分,并注明了以下原因,并表明它们已移至collections模块。

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 .鉴于它们被认为是“过时的”,因此类似的类没有为其他类型(如set )实现也就不足为奇了。

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 .由于类型可以是超类,并且抽象基类为创建各种集合、序列、映射和其他类型的子类提供了合理的替代方案,这些类型的重大更改将难以通过扩展内置类型本身来实现,因此似乎没有必要包括除了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).以下是UserSet一个非常基本的实现,它带有一个data属性,该属性将类的内容存储在一个真实的set (类似于UserDict等采用的方法)。 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).虽然在标准库中包含这样的实现可能对您有用,但实现自己并不是非常困难(如果对某人可能“有用”的所有内容都包含在标准库中,Python 将变得非常臃肿)。

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}

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.我不确定它是否广为人知,但我设法创建并成功使用了内置set的子类,而无需使用self.data属性或类似属性手动操作底层数据。 Working with the data is mediated using the self attribute itself.使用数据本身是通过self属性进行处理的。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM