I have been trying to find this answer but I could not find the proper explanation
Some say that they are the same and others say otherwise
I know for a compound object, The object will consist of multiple values
But for simple objects like int or string can we say that they are identical and values are objects themselves?
Thank you
3 Answers 3
But for simple objects like int or string can we say that they are identical and values are objects themselves?
Not exactly but there is the concept of 'value object' which "represents a simple entity whose equality is not based on identity."
In Python when you use the ==
operator you are testing the identity of the object based on its value. When you use the is
operator, you are testing object identity.
So conceptually, value objects
are used as if they are values but technically they are still objects and it's good to keep that distinction in mind.
It's also possible that the implementation of value objects might be to reuse a single instance (when possible) of a value object. For example, in Java, string literals will be 'interned' to avoid creating multiple objects with the same string value. Similar optimizations occur with Integer instances. You should not depend on this in general, though and doing so may result in bugs.
Value is a property of each Object. Values are the results of operations, e.g. 1 + 1
results in an object with the value 2
.
Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects.
Every object has an identity, a type and a value.
The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable.
E.g.
a = 1
b = "Two"
c = [a, b, 3]
There are three objects, assigned to the names a
, b
and c
respectively. Of these, only c
's value can change, but the names can be reassigned at any point.
Beware that every language have their own slightly different definition of these terms. So this is very language specific.
In Python, objects and values are not exactly the same, but the difference is so subtle the terms are often used interchangeably.
But strictly speaking an object has an identity and a value. The value is the data that the object contains. If the object is immutable (like ints and strings are) then the value cannot change - but there is still a difference in that two different objects may have the same value. E.g.:
>>> a = "Hello!"
>>> b = "Hello!"
>>> a is b
False
Given this I don't think you can say a object is the same as its value, even in the case of simple objects.
-
thank you for the answer. Yes people use it interchangeably by not taking into account the memory address of the Object and strictly referring to objects based on the valueChyanit Singh– Chyanit Singh2019年11月07日 19:52:59 +00:00Commented Nov 7, 2019 at 19:52
-
@ChyanitSingh not the memory address, the identity.juanpa.arrivillaga– juanpa.arrivillaga2019年12月03日 23:40:25 +00:00Commented Dec 3, 2019 at 23:40
-
1@juanpa.arrivillaga memory address is closely related to identityChyanit Singh– Chyanit Singh2019年12月04日 13:10:49 +00:00Commented Dec 4, 2019 at 13:10
-
@juanpa.arrivillaga they are the same thing, in fact, Memory address and Identity of an object are the same things. I am not sure why you needed to make that commentChyanit Singh– Chyanit Singh2019年12月05日 12:52:48 +00:00Commented Dec 5, 2019 at 12:52
-
@ChyanitSingh: There is a slight difference. If an object is deleted, then a new object might get the same memory address, even though it is a different object. You have to keep that in mind if you use
id()
.JacquesB– JacquesB2019年12月05日 12:58:05 +00:00Commented Dec 5, 2019 at 12:58
int
variable, its value changes to a different value, but the object stays the same object.