Dictionary comparison?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brainstaurm
    New Member
    • May 2007
    • 7

    #1

    Dictionary comparison?

    I would like to understand the internals of the dictionary comparison in python.

    The python reference manual says that:
    "If a class does not define a __cmp__() method it should not define a __hash__() operation either". Why does this dependency exist?

    I tried to google for it, but couldn't find proper documentation. Any pointers will be helpful.

    Thank you!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by brainstaurm
    I would like to understand the internals of the dictionary comparison in python.

    The python reference manual says that:
    "If a class does not define a __cmp__() method it should not define a __hash__() operation either". Why does this dependency exist?

    I tried to google for it, but couldn't find proper documentation. Any pointers will be helpful.

    Thank you!
    From Python Essential Reference: ...mutable objects should not define this method (__hash__()); any changes to an object will alter their hash value and make it impossible to locate an object on subsequent dictionary lookups. An object should not define a __hash__() method without also defining __cmp__().

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bvdet
      From Python Essential Reference: ...mutable objects should not define this method (__hash__()); any changes to an object will alter their hash value and make it impossible to locate an object on subsequent dictionary lookups. An object should not define a __hash__() method without also defining __cmp__().
      I take it that __cmp__() is used by __hash__() in its search.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by bartonc
        I take it that __cmp__() is used by __hash__() in its search.
        Take a look at the explanation in the General Python FAQ: LINK

        Comment

        Working...