Tutorial

Python id()

Published on August 4, 2022
author

Pankaj

Python id()

Python id() function returns the “identity” of the object. The identity of an object is an integer, which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value. In CPython implementation, this is the address of the object in memory.

Python id()

Python cache the id() value of commonly used data types, such as string, integer, tuples etc. So you might find that multiple variables refer to the same object and have same id() value if their values are same. Let’s check this out with an example.

# integers
a = 10
b = 10
c = 11
d = 12

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

Output:

4317900064
4317900064
4317900096
4317900128

Notice that id() value of ‘a’ and ‘b’ are same, they have the same integer value. Let’s see if we get the similar behavior with string and tuples too?

# tuples
t = ('A', 'B')
print(id(t))

t1 = ('A', 'B')
print(id(t1))

# strings
s1 = 'ABC'
s2 = 'ABC'
print(id(s1))
print(id(s2))

Output:

4320130056
4320130056
4320080816
4320080816

From the output, it’s clear that Python cache the strings and tuple objects and use them to save memory space.

Caching can work only with immutable objects, notice that integer, string, tuples are immutable. So Python implementation can use caching to save memory space and improve performance.

We know that dictionary is not immutable, let’s see if id() is different for different dictionaries even if the elements are same?

# dict
d1 = {"A": 1, "B": 2}
d2 = {"A": 1, "B": 2}
print(id(d1))
print(id(d2))

Output:

4519884624
4519884768

As we thought, dict objects are returning different id() value and there seems no caching here.

Python id() for custom object

Let’s see a simple example of getting id() value for a custom object.

class Emp:
    a = 0


e1 = Emp()
e2 = Emp()

print(id(e1))
print(id(e2))

Output:

4520251744
4520251856

Summary

Python id() value is guaranteed to be unique and constant for an object. We can use this to make sure two objects are referring to the same object in memory or not.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
April 27, 2021

why id() gives different values for a=999 and b=999

- Suneel Kumar

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    March 3, 2020

    id of two same tuple are different. t = (1, 2,3) t1 = (1, 2,3) print(t is t1) Output: False

    - Ajay Dayma

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Become a contributor for community

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      DigitalOcean Documentation

      Full documentation for every DigitalOcean product.

      Resources for startups and SMBs

      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

      Get our newsletter

      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

      New accounts only. By submitting your email you agree to our Privacy Policy

      The developer cloud

      Scale up as you grow — whether you're running one virtual machine or ten thousand.

      Get started for free

      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

      *This promotional offer applies to new accounts only.