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

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

      3. <legend id='bbRrK'><style id='bbRrK'><dir id='bbRrK'><q id='bbRrK'></q></dir></style></legend>
          <bdo id='bbRrK'></bdo><ul id='bbRrK'></ul>

        Python set([]) 如何检查两个对象是否相等?一个对象需要定义哪些方法来自定义它?

        时间:2023-07-03

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

      4. <tfoot id='J6Zbv'></tfoot><legend id='J6Zbv'><style id='J6Zbv'><dir id='J6Zbv'><q id='J6Zbv'></q></dir></style></legend>

                <tbody id='J6Zbv'></tbody>
              <i id='J6Zbv'><tr id='J6Zbv'><dt id='J6Zbv'><q id='J6Zbv'><span id='J6Zbv'><b id='J6Zbv'><form id='J6Zbv'><ins id='J6Zbv'></ins><ul id='J6Zbv'></ul><sub id='J6Zbv'></sub></form><legend id='J6Zbv'></legend><bdo id='J6Zbv'><pre id='J6Zbv'><center id='J6Zbv'></center></pre></bdo></b><th id='J6Zbv'></th></span></q></dt></tr></i><div id='J6Zbv'><tfoot id='J6Zbv'></tfoot><dl id='J6Zbv'><fieldset id='J6Zbv'></fieldset></dl></div>
                <bdo id='J6Zbv'></bdo><ul id='J6Zbv'></ul>
                  本文介绍了Python set([]) 如何检查两个对象是否相等?一个对象需要定义哪些方法来自定义它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要在 Python 中创建一个容器"对象或类,它会记录我还定义的其他对象.此容器的一个要求是,如果两个对象被认为是相同的,则删除一个(其中一个).我的第一个想法是使用 set([]) 作为包含对象,来完成这个要求.

                  I need to create a 'container' object or class in Python, which keeps a record of other objects which I also define. One requirement of this container is that if two objects are deemed to be identical, one (either one) is removed. My first thought was to use a set([]) as the containing object, to complete this requirement.

                  但是,该集合不会删除两个相同的对象实例之一.我必须定义什么来创建一个?

                  However, the set does not remove one of the two identical object instances. What must I define to create one?

                  这是 Python 代码.

                  Here is the Python code.

                  class Item(object):
                    def __init__(self, foo, bar):
                      self.foo = foo
                      self.bar = bar
                    def __repr__(self):
                      return "Item(%s, %s)" % (self.foo, self.bar)
                    def __eq__(self, other):
                      if isinstance(other, Item):
                        return ((self.foo == other.foo) and (self.bar == other.bar))
                      else:
                        return False
                    def __ne__(self, other):
                      return (not self.__eq__(other))
                  

                  口译员

                  >>> set([Item(1,2), Item(1,2)])
                  set([Item(1, 2), Item(1, 2)])
                  

                  很明显,x == y调用的__eq__()并不是集合调用的方法.什么叫做?我还必须定义什么其他方法?

                  It is clear that __eq__(), which is called by x == y, is not the method called by the set. What is called? What other method must I define?

                  注意:Items 必须保持可变,并且可以更改,因此我无法提供 __hash__()代码> 方法.如果这是唯一的方法,那么我将重写以使用不可变的 Items.

                  推荐答案

                  恐怕你得提供一个 __hash__() 方法.但是你可以这样编码,它不依赖于你的 Item 的可变属性.

                  I am afraid you will have to provide a __hash__() method. But you can code it the way, that it does not depend on the mutable attributes of your Item.

                  这篇关于Python set([]) 如何检查两个对象是否相等?一个对象需要定义哪些方法来自定义它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                            <tbody id='yi4RO'></tbody>
                        1. <small id='yi4RO'></small><noframes id='yi4RO'>

                          <tfoot id='yi4RO'></tfoot>