object identity and hashing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • castironpi@gmail.com

    object identity and hashing

    Can someone explain this?
    >>a= {}
    >>a[(3,)]= 0
    >>(3,) in a
    True
    >>(3,) is (3,)
    False
  • Jeff Schwab

    #2
    Re: object identity and hashing

    castironpi@gmai l.com wrote:
    Can someone explain this?
    >
    >>>a= {}
    Create an empty dict and bind it to the name a.
    >>>a[(3,)]= 0
    Set the key/value pair (3,):0 to the dict.
    >>>(3,) in a
    Is (3,) one of the keys in the dict?
    True
    Yes, it is.
    >>>(3,) is (3,)
    Create two separate tuples (that happen to be equivalent). Are they the
    same object?
    False
    No, they are not.

    Every time you write (3,), you are potentially creating a new object.
    These objects have equal values (and hash codes), so they are
    interchangeable for purposes of keying a dict.

    Comment

    • castironpi@gmail.com

      #3
      Re: object identity and hashing

      On Feb 24, 7:58 pm, Jeff Schwab <j...@schwabcen ter.comwrote:
      castiro...@gmai l.com wrote:
      Can someone explain this?
      >
      >>a= {}
      >
      Create an empty dict and bind it to the name a.
      >
      >>a[(3,)]= 0
      >
      Set the key/value pair (3,):0 to the dict.
      >
      >>(3,) in a
      >
      Is (3,) one of the keys in the dict?
      >
      True
      >
      Yes, it is.
      >
      >>(3,) is (3,)
      >
      Create two separate tuples (that happen to be equivalent).  Are they the
      same object?
      >
      False
      >
      No, they are not.
      >
      Every time you write (3,), you are potentially creating a new object.
      These objects have equal values (and hash codes), so they are
      interchangeable for purposes of keying a dict.
      I see. You stated,
      Is (3,) one of the keys in the dict?
      >
      True
      >
      Yes, it is.
      It isn't, but it does equal a key that's already in the dict.

      Comment

      Working...