覆盖 Python 中的 set 方法 [英] Overriding set methods in Python

查看:40
本文介绍了覆盖 Python 中的 set 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义集合,该集合会自动将对象转换为不同形式以存储在集合中(请参阅 使用 Python 字典作为非嵌套键) 作为背景.

I want to create a custom set that will automatically convert objects into a different form for storage in the set (see Using a Python Dictionary as a key non-nested) for background.

如果我覆盖addremove__contains____str__update__iter__,这是否足以使其他操作正常运行,还是我需要覆盖其他任何内容?

If I override add, remove, __contains__, __str__, update, __iter__, will that be sufficient to make the other operations behave properly, or do I need to override anything else?

推荐答案

collections 的抽象类工作,正如@kaizer.se 所建议的,是 2.6 中的合适解决方案(不知道为什么你想要调用 super —— 你想委托哪些功能不能通过包含而不是继承来最好地完成?!).

Working from collections's abstract classes, as @kaizer.se suggests, is the appropriate solution in 2.6 (not sure why you want to call super -- what functionality are you trying to delegate that can't best done by containment rather than inheritance?!).

确实,您没有获得 update -- 通过提供抽象方法,您确实获得了 __le__、__lt__、__eq__、__ne__、__gt__、__ge__、__and__、__or__ __sub__,__xor__ 和 isdisjoint(来自 collections.Set)加上 clear、pop、remove、__ior__、__iand__、__ixor__ 和 __isub__(来自 collections.MutableSet),这远远超过您从子类化 set(您必须覆盖 每个 感兴趣的方法)中获得的结果.你只需要提供你想要的其他 set 方法.

It's true that you don't get update -- by providing the abstract methods, you do get __le__, __lt__, __eq__, __ne__, __gt__, __ge__, __and__, __or__ __sub__, __xor__, and isdisjoint (from collections.Set) plus clear, pop, remove, __ior__, __iand__, __ixor__, and __isub__ (from collections.MutableSet), which is far more than you'd get from subclassing set (where you'd have to override every method of interest). You'll just have to provide other set methods you desire.

请注意,像 collections.Set 这样的抽象基类与具体类非常不同,包括诸如 set 之类的内置函数和(在 2.6 中)旧的 sets.Set,已弃用但仍然存在(在 Python 3 中删除).ABC 旨在继承(然后可以在您实现所有抽象方法后从您那里合成一些方法,这是您必须的),其次是注册"类,因此它们看起来好像是从它们继承的,即使它们没有(使 isinstance 更加实用和有用).

Note that the abstract base classes like collections.Set are a pretty different beast from concrete classes, including builtins such as set and (in 2.6) good old sets.Set, deprecated but still around (removed in Python 3). ABCs are meant to inherit from (and can then synthesize some methods from you once you implement all the abstract methods, as you must) and secondarily to "register" classes with so they look as if they inherited from them even when they don't (to make isinstance more useable and useful).

这是一个适用于 Python 3.1 和 2.6 的工作示例(没有充分的理由使用 3.0,因为 3.1 只比它有优势,没有劣势):

Here's a working example for Python 3.1 and 2.6 (no good reason to use 3.0, as 3.1 only has advantages over it, no disadvantage):

import collections

class LowercasingSet(collections.MutableSet):
  def __init__(self, initvalue=()):
    self._theset = set()
    for x in initvalue: self.add(x)
  def add(self, item):
    self._theset.add(item.lower())
  def discard(self, item):
    self._theset.discard(item.lower())
  def __iter__(self):
    return iter(self._theset)
  def __len__(self):
    return len(self._theset)
  def __contains__(self, item):
    try:
      return item.lower() in self._theset
    except AttributeError:
      return False

这篇关于覆盖 Python 中的 set 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆