instance comparison

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • King

    instance comparison


    I am facing a problem where I am really confused about it.

    I am trying to compare to instances using:

    if inst1 == inst2

    These instances have a overridden method __str__ which returns same
    string. The condition result is true although they are different
    instances.

    If I use:

    if id(inst1) == id(inst2)

    The results is false. What's happening here?

    If I am trying to store this pair in a set where other instances are
    already stored, but it's not storing it. Although I am sure that there
    is no pair already stored of same but even if Set is comparing by
    using string return by __str__ method then it won't.

    How do I solve this?

    Is this mean when you have overridden __str__ method then it comapre
    with results of __str__ or else it will comapre whether they are the
    same instances?
  • John Machin

    #2
    Re: instance comparison

    On Jul 24, 6:50 pm, King <animator...@gm ail.comwrote:
    I am facing a problem where I am really confused about it.
    >
    I am trying to compare to instances using:
    >
    if inst1 == inst2
    >
    These instances have a overridden method __str__ which returns same
    string. The condition result is true although they are different
    instances.
    >
    If I use:
    >
    if id(inst1) == id(inst2)
    >
    The results is false. What's happening here?
    >
    If I am trying to store this pair in a set where other instances are
    already stored, but it's not storing it. Although I am sure that there
    is no pair already stored of same but even if Set is comparing by
    using string return by __str__ method then it won't.
    >
    How do I solve this?
    >
    Is this mean when you have overridden __str__ method then it comapre
    with results of __str__
    No. Do you have a __cmp__ method, or an __eq__ method? Any other
    __twounderscore s__ methods?

    It is impossible to tell what is going on from your description.
    Please supply the source of a *short* class that demonstrates the
    problem, and demonstrate it.

    Comment

    • Fredrik Lundh

      #3
      Re: instance comparison

      King wrote:
      Is this mean when you have overridden __str__ method then it comapre
      with results of __str__ or else it will comapre whether they are the
      same instances?
      Comparisons uses __cmp__ or the rich comparison set; see



      for details.

      Sets and dictionaries, also relies on hashing (the __hash__ method, also
      described on the linked page).

      The __str__ method only affects printing and conversion to string.

      </F>

      Comment

      • King

        #4
        Re: instance comparison


        The only methods I do have in class is __init__ and __str__.
        How ever inst1 and inst2 is coming from a dictionary where I stored
        them with a unique id.

        inst1 = stored[id]
        inst2 = stored[id]

        Is this makes a difference? I will rip down the piece of code giving
        me problem and post.

        Comment

        • Fredrik Lundh

          #5
          Re: instance comparison

          King wrote:
          The only methods I do have in class is __init__ and __str__.
          How ever inst1 and inst2 is coming from a dictionary where I stored
          them with a unique id.
          >
          inst1 = stored[id]
          inst2 = stored[id]
          >
          Is this makes a difference?
          unlikely (well, if that's the literal code, both variables will point to
          the same variable, but I guess that's not what you meant).
          I will rip down the piece of code giving me problem and post.
          please do. and don't be surprised if you find the problem while doing
          so ;-)

          </F>

          Comment

          • King

            #6
            Re: instance comparison

            No,

            The the class is not subclass of another one. Problem still persist.
            The code is pretty huge and I am trying to post the information as
            clear as possible.

            Comment

            • Fredrik Lundh

              #7
              Re: instance comparison

              King skrev:
              The the class is not subclass of another one. Problem still persist.
              The code is pretty huge and I am trying to post the information as
              clear as possible.
              feel free to *add* stuff to the following example until it breaks, if
              that's easier:
              >>class Spam:
              .... def __init__(self, label):
              .... self.label = label
              .... def __str__(self):
              .... return self.label
              ....
              >>a = Spam("an object")
              >>a
              <__main__.Spa m instance at 0xb7e8faac>
              >>print a
              an object
              >>b = Spam("an object")
              >>b
              <__main__.Spa m instance at 0xb7e8fb6c>
              >>print b
              an object
              >>a == b
              False
              >>a is b
              False
              >>str(a) == str(b)
              True

              </F>

              Comment

              • Petite Abeille

                #8
                Re: instance comparison


                On Jul 24, 2008, at 7:53 PM, King wrote:
                The the class is not subclass of another one. Problem still persist.
                The code is pretty huge and I am trying to post the information as
                clear as possible.
                Mark V. Shaney, from Dissociated Press, I presume?

                --
                PA.

                Comment

                Working...