2

Let me have a string object a="ABC". Now I want to create a different object b="ABC" having both id(a) and id(b) separate. Is it possible in python?

7
  • 3
    you should never have a reason to mess with this. Why are you worried about the identity of the two string objects? Commented Feb 27, 2020 at 9:09
  • 1
    b = a[0] + a[1:] if you really want to. Not sure what purpose it serves. Commented Feb 27, 2020 at 9:10
  • 3
    ps. yes, it is possible though, but id of immutable objects should never be your concern anyways. I'm wondering what you're trying to achieve with this. I see the Y of your XY Problem, what's your X? Commented Feb 27, 2020 at 9:11
  • @khelwood leads to same ID Commented Feb 27, 2020 at 9:11
  • @Adelin Not when I try it. Commented Feb 27, 2020 at 9:11

3 Answers 3

4

This is an implementation detail. If two variables refer to the same object (a is b is true) then they have to be equal (a == b shall be true). But at least for immutable objects, Python does not specify what it does.

The standard CPython implementation uses common ids for small integers:

a = 1
b = 1
a is b   # True

The rationale is that they are likely to be used in many places in the same program, and sharing a common variable saves some memory.

It is done too for strings, when the interpreter can easily guess that they will share the same value. Example on a Python 3.6

a = "abc"
b = 'ab' + 'c'
c = ''.join(chr(i) for i in range(97, 100))
a is b      # gives True
a is c      # gives False

TL/DR: whether two equal strings share same id is an implementation detail and should not be relied on.

Sign up to request clarification or add additional context in comments.

Comments

1

This is a great source for id of strings:

>>> a = 'python'
>>> id(a)
4400931648
>>> b = a.encode().decode()
>>> b
'python'
>>> id(b)
4400931984
>>> print('Eureka!')
Eureka!

or:

>>> a = 'python'
>>> b = (a + '.')[:-1]
>>> id(a)
4400931648
>>> id(b)
4400931760
>>> print('Eureka!')
Eureka!

But the general conclusion is that you should not use this even if it is possible.

Comments

0

Python id() returns the integer which acts as identity of that object.

To answer the question in short:

Try to create string "ABC" by slicing.

>>> a = "ABC"
>>> id(a)
4427474416
>>> b = "ABC"
>>> id(b)
4427474416
>>> b = a[:2]+a[2:]
>>> b
'ABC'
>>> id(b)
4429072688

For detailed explanation

Python 3.7

>>> a = 5
>>> id(a) 
4425034672
>>> b = 5
>>> id(b)
4425034672

Actually in python immutable objects (strings, integers, tuples) are stored in cache and it returns their location when we called id() function.

So, it returns same id for immutable objects as most the time they are cached in python as above.

For the same mutable objects id() returns different values.

Consider this example:

>>> d = { 'a' : 1 }
>>> id(d)
4426697136
>>> p = { 'a' : 1 }
>>> id(p)
4427492000

Note:

It may not return the same identity values for same immutable objects as they may/ may not be cached.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.