1

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
  • The difference is that if you increment an int variable, its value changes to a different value, but the object stays the same object. Nov 7, 2019 at 14:06
  • It basically depends on how you define the terms "object" and "value". Nov 7, 2019 at 15:29
  • 2
    @JörgWMittag Here's the python definition: docs.python.org/2.0/ref/objects.html
    – JimmyJames
    Nov 7, 2019 at 15:31

3 Answers 3

2

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.

1

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.

From the docs:

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.

1

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.

5
  • 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 value Nov 7, 2019 at 19:52
  • @ChyanitSingh not the memory address, the identity. Dec 3, 2019 at 23:40
  • 1
    @juanpa.arrivillaga memory address is closely related to identity 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 comment 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
    Dec 5, 2019 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.