Questions tagged [object-identity]

whether two variable are referring to the same object.

Identity is a property of an object that uniquely defines its abstract location.

Two objects stored in variable a and b are said to have the same identity, if a and b actually refer to the same object (same class, same abstract memory location). On can test identity of two objects using

As an implementation detail, identity hints about potential co-location of objects in physical or virtual memory, and thus may be related to performance. In some languages, one can get a value representing the object's identity, or address (although this token is only valid during the object's lifetime):

54 questions
38
votes
3 answers

If Java's garbage collector moves objects, what is Object.hashCode and System.identityHashCode?

I've often heard that these methods (Object.hashCode and System.identityHashCode) return the address of the object, or something computed quickly from the address; but I'm also pretty sure the garbage collector moves and compacts objects. Since the…
Rob N
  • 15,024
  • 17
  • 92
  • 165
32
votes
3 answers

In Ruby, why does inspect() print out some kind of object id which is different from what object_id() gives?

When the p function is used to print out an object, it may give an ID, and it is different from what object_id() gives. What is the reason for the different numbers? Update: 0x4684abc is different from 36971870, which is 0x234255E >> a =…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
31
votes
6 answers

'is' operator behaves differently when comparing strings with spaces

I've started learning Python (python 3.3) and I was trying out the is operator. I tried this: >>> b = 'is it the space?' >>> a = 'is it the space?' >>> a is b False >>> c = 'isitthespace' >>> d = 'isitthespace' >>> c is d True >>> e =…
luisdaniel
  • 907
  • 11
  • 20
24
votes
2 answers

How to access an element of a set using an equivalent object?

If I have an object that compares equal to an element of a Python set, but is not the same object, is there a reasonable way to get a reference to the object in the set? The use case would be using the set to identify and share duplicated…
Janne Karila
  • 24,266
  • 6
  • 53
  • 94
21
votes
2 answers

[[no_unique_address]] and two member values of the same type

I'm playing around with [[no_unique_address]] in c++20. In the example on cppreference we have an empty type Empty and type Z struct Empty {}; // empty class struct Z { char c; [[no_unique_address]] Empty e1, e2; }; Apparently, the size of…
tom
  • 1,520
  • 1
  • 12
  • 26
16
votes
1 answer

When does Python create new list objects for empty lists?

The following makes sense to me: >>> [] is [] False Given that lists are mutable, I would expect [] to be a new empty list object every time it appears in an expression. Using this explanation however, the following surprises me: id([]) ==…
Loax
  • 615
  • 5
  • 11
11
votes
2 answers

"ValueError: Not a location id (Invalid object id)" while creating HDF5 datasets

I have an numpy array(arr) of size (3997,29). I am using this array to create a dataset. This array has both integer and float variables. So dtype is reference. But when I execute it I get the below error. "ValueError: Not a location id (Invalid…
Rupesh N
  • 111
  • 1
  • 4
10
votes
1 answer

In Python (2.7), why is os.remove not identical to os.unlink?

>>> import sys >>> sys.version '2.7.3 (default, Mar 13 2014, 11:03:55) \n[GCC 4.7.2]' >>> import os >>> os.remove is os.unlink False >>> os.remove == os.unlink True Why is that? Isn't os.unlink supposed to be an alias of os.remove?
Kal
  • 1,707
  • 15
  • 29
8
votes
2 answers

Even though tuples are immutable, they are stored in different addresses in interactive mode. Why?

t = (1,2,3) t1 = (1,2,3) print(id(t)) print(id(t1)) The above lines of code gives the same addresses in script mode in Python, but in interactive mode it outputs different addresses. Can anyone explain the reason for this?
Harsha jk
  • 81
  • 2
8
votes
4 answers

Why does Ruby tend to assign object IDs in descending order?

I've noticed that objects have their IDs assigned in a counterintuitive fashion. The earlier an object is created, the greater its object ID. I would have thought they would have been assigned in ascending order, rather than the other way…
Matty
  • 33,203
  • 13
  • 65
  • 93
6
votes
1 answer

What does OBJECT_ID do in SQL Server?

I'm trying out the usage of OBJECT_ID and found the following entry in MSDN: "Returns the database object identification number of a schema-scoped object." What is database object identification number and what is schema scoped object. Seems like…
Sreedhar Danturthi
  • 7,119
  • 19
  • 68
  • 111
6
votes
0 answers

Inconsistency in has_unique_object_representations and empty subobjects

I've playing around with C++20's [[no_unique_address]] attribute and found some interesting behavior when using it with the has_unique_object_representations type trait: #include struct Empty {}; struct A : public Empty { int…
5
votes
2 answers

Rust references don't implement Eq/Hash? How to use them as hash map key?

I want to build a hashmap where they keys are references. I want that equality for those references means reference equality, i.e. both references borrow the same object. use std::collections::hash_map::HashMap; struct SomeKey(); struct…
Heinzi
  • 5,793
  • 4
  • 40
  • 69
5
votes
3 answers

Dart int and double being interned? Treated specially by identical()?

Dart has both: an equality operator == and a top-level function named identical(). By the choice of syntax, it feels natural to want to use Dart's == operator more frequently than identical(), and I like that. In fact, the Section on Equality of…
Patrice Chalin
  • 15,440
  • 7
  • 33
  • 44
5
votes
1 answer

DDD and MongoDB: Is it okay to let Mongo create ObjectIDs?

According to DDD (Blue book, Evans) a Factory has the responsibility to create an Aggregate Root in a valid state. Does this mean it should be able to create the technical id (objectId in mongoDB world) as well as the domain id? On the one hand,…
Geert-Jan
  • 18,623
  • 16
  • 75
  • 137
1
2 3 4