Pointer or unique id

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

    #1

    Pointer or unique id

    Hello,

    does python have an equivalent to Java: int Object.hashCode () ?

    TIA
    --
    Nomak
  • Bruno Desthuilliers

    #2
    Re: Pointer or unique id

    Nomak a écrit :[color=blue]
    > Hello,
    >
    > does python have an equivalent to Java: int Object.hashCode () ?
    >[/color]

    id(object) -> integer
    Return the identity of an object. This is guaranteed to be unique among
    simultaneously existing objects. (Hint: it's the object's memory address.)

    hash(obj) -> integer
    Return a hash value for the object. Two objects with the same value
    have the same hash value. The reverse is not necessarily true, but likely.

    HTH
    Bruno

    Comment

    • Michael Hoffman

      #3
      Re: Pointer or unique id

      Bruno Desthuilliers wrote:
      [color=blue]
      > hash(obj) -> integer
      > Return a hash value for the object. Two objects with the same value
      > have the same hash value. The reverse is not necessarily true, but likely.[/color]

      Of course not all Python objects are hashable:
      [color=blue][color=green][color=darkred]
      >>> hash([])[/color][/color][/color]
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      TypeError: list objects are unhashable
      --
      Michael Hoffman

      Comment

      • Terry Reedy

        #4
        Re: Pointer or unique id


        "Bruno Desthuilliers" <bdesth.quelque chose@free.quel quepart.fr> wrote in
        message news:41e94d40$0 $29202$636a15ce @news.free.fr.. .[color=blue]
        >id(object) -> integer
        >Return the identity of an object. This is guaranteed to be unique among
        >simultaneous ly existing objects.[/color]

        This is part of the language specification. Also, the identity of an
        object must remain the same for its entire lifetime.
        [color=blue]
        > (Hint: it's the object's memory address.)[/color]

        This is an implementation detail of CPython. It cannot be true if the
        garbage collector moves objects around. Or if the object lives elsewhere
        (and is accessed thru a proxy).

        Terry J. Reedy





        Comment

        Working...