Julia equivalent to Python's id function

Hi, guys
Is there a Julia build-in function existed that has the same functionality as python’s id function?

Depending on what you want to use this for, you might find the function hash useful

I guess the closest would be pointer_from_objref. But it is very rare that you need to use this.

There’s Base.objectid:

help?> Base.objectid
  objectid(x) -> UInt

  Get a hash value for x based on object identity. objectid(x)==objectid(y) if x === y.

  See also hash, IdDict.

IdDict uses objectid to check for equality, and its docs have an example to illustrate the difference between value equality and object equality:


  julia> Dict(true => "yes", 1 => "no", 1.0 => "maybe")
  Dict{Real, String} with 1 entry:
    1.0 => "maybe"
  
  julia> IdDict(true => "yes", 1 => "no", 1.0 => "maybe")
  IdDict{Any, String} with 3 entries:
    true => "yes"
    1.0  => "maybe"
    1    => "no"

The Dict keys are all isequal and therefore get hashed the same, so they get overwritten. The IdDict hashes by object-id, and thus preserves the 3 different keys.

4 Likes