• <legend id='tWq7a'><style id='tWq7a'><dir id='tWq7a'><q id='tWq7a'></q></dir></style></legend>
    <tfoot id='tWq7a'></tfoot>
  • <small id='tWq7a'></small><noframes id='tWq7a'>

    <i id='tWq7a'><tr id='tWq7a'><dt id='tWq7a'><q id='tWq7a'><span id='tWq7a'><b id='tWq7a'><form id='tWq7a'><ins id='tWq7a'></ins><ul id='tWq7a'></ul><sub id='tWq7a'></sub></form><legend id='tWq7a'></legend><bdo id='tWq7a'><pre id='tWq7a'><center id='tWq7a'></center></pre></bdo></b><th id='tWq7a'></th></span></q></dt></tr></i><div id='tWq7a'><tfoot id='tWq7a'></tfoot><dl id='tWq7a'><fieldset id='tWq7a'></fieldset></dl></div>

      <bdo id='tWq7a'></bdo><ul id='tWq7a'></ul>

      1. 在 Python 中重写 set 方法

        时间:2023-07-24

        • <bdo id='qLY8a'></bdo><ul id='qLY8a'></ul>

            <small id='qLY8a'></small><noframes id='qLY8a'>

            <tfoot id='qLY8a'></tfoot>

            <legend id='qLY8a'><style id='qLY8a'><dir id='qLY8a'><q id='qLY8a'></q></dir></style></legend>

                <tbody id='qLY8a'></tbody>

              <i id='qLY8a'><tr id='qLY8a'><dt id='qLY8a'><q id='qLY8a'><span id='qLY8a'><b id='qLY8a'><form id='qLY8a'><ins id='qLY8a'></ins><ul id='qLY8a'></ul><sub id='qLY8a'></sub></form><legend id='qLY8a'></legend><bdo id='qLY8a'><pre id='qLY8a'><center id='qLY8a'></center></pre></bdo></b><th id='qLY8a'></th></span></q></dt></tr></i><div id='qLY8a'><tfoot id='qLY8a'></tfoot><dl id='qLY8a'><fieldset id='qLY8a'></fieldset></dl></div>
                  本文介绍了在 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 (您必须覆盖 every 感兴趣的方法).您只需要提供您想要的其他设置方法.

                  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 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:pythononic 切片集合的方式是什么? 下一篇:对集合列表进行减法

                  相关文章

                  <i id='lMy5O'><tr id='lMy5O'><dt id='lMy5O'><q id='lMy5O'><span id='lMy5O'><b id='lMy5O'><form id='lMy5O'><ins id='lMy5O'></ins><ul id='lMy5O'></ul><sub id='lMy5O'></sub></form><legend id='lMy5O'></legend><bdo id='lMy5O'><pre id='lMy5O'><center id='lMy5O'></center></pre></bdo></b><th id='lMy5O'></th></span></q></dt></tr></i><div id='lMy5O'><tfoot id='lMy5O'></tfoot><dl id='lMy5O'><fieldset id='lMy5O'></fieldset></dl></div>

                    <bdo id='lMy5O'></bdo><ul id='lMy5O'></ul>
                1. <small id='lMy5O'></small><noframes id='lMy5O'>

                  <legend id='lMy5O'><style id='lMy5O'><dir id='lMy5O'><q id='lMy5O'></q></dir></style></legend>

                  <tfoot id='lMy5O'></tfoot>