Morganna Carmem Diniz
Morganna Carmem Diniz

Reputation: 11

Complex number id in Python

a = 2
b = 2
c = 3.5
d = 3.5
e = 2.3 + 1.5j
f = 2.3 + 1.5j
print('id(a) = ', id(a), 'id(b) = ', id(b))
print('id(c) = ', id(c), 'id(d) = ', id(d))
print('id(e) = ', id(e), 'id(f) = ', id(f))

The program shows the follow answer:

id(a) =  10914528 id(b) =  10914528
id(c) =  140295268467240 id(d) =  140295268467240
id(e) =  140295244785552 id(f) =  140295244784464

So a and b are two names for the same integer object, and c and d are two names for the same float object. But e and f are two different objects in CPython. Why?

Upvotes: 1

Views: 600

Answers (2)

Morganna Carmem Diniz
Morganna Carmem Diniz

Reputation: 11

The program below helped me to understand what was discussed in this issue.

for i in range(255,259):
   print('i =', i, ', id(i) =', id(i))
print()
a = 255
print('a = ', a, 'id(a) = ', id(a))
b = 256
print('b = ', b, 'id(b) = ', id(b))
c = 257
print('c = ', c, 'id(c) = ', id(c))
d = 258
print('d = ', d, 'id(d) = ', id(d))
e = 257
print('e = ', e, 'id(e) = ', id(e))

The answer

i = 255 , id(i) = 10922624
i = 256 , id(i) = 10922656
i = 257 , id(i) = 140489683306320
i = 258 , id(i) = 140489683305296

a =  255 id(a) =  10922624
b =  256 id(b) =  10922656
c =  257 id(c) =  140489706959664
d =  258 id(d) =  140489706850896
e =  257 id(e) =  140489706959664

The integer number 255 will have always the same id (it is a short number). The variables c and e are two names for a same object, but there is a different identification for i = 257 (257 is not a short number).

There are no two names for a same complex number. They always have different identification (I believe that it is an implementation decision).

Thanks!

Upvotes: 0

alani
alani

Reputation: 13079

Because these are immutable objects, it does not matter whether the same object is reused or not for multiple occurrences of the same value, and whether it does or not will be implementation-dependent.

In fact CPython always uses the same id for small integers (-5 to 256). For other int/float hard-coded constants, it will do if both are defined in the same source file, though if you try importing from another source file you will find that it is not the same object.

Evidently for complex numbers, the IDs are not reused - this is in common with values generated at run time (except for small integers as mentioned above).

Example:

test1.py

from test2 import a, b
c = 3.5
d = 3.5

print(id(a), id(b), id(c), id(d))

test2.py

a = 3.5
b = 3.5

python test1.py gives:

140082541875392 140082541875392 140082541875800 140082541875800

but if you change them to small integers (with the same value) then all four have the same ID.

Note also that for hard-coded constants entered using the interactive python shell, it will be similar to when they are defined in different modules -- the optimisation of using the same object is not used (other than for small integers):

>>> a = 3.5
>>> b = 3.5
>>> a is b
False

Upvotes: 2

Related Questions