Tutorials  /  Python

Python id() Function

Ccentron Redaktion · February 2024 ·3 min read ·Python, Tutorial

The 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 the same id() value if their values are same. Let’s check this out with an example.

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

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

Output:

Code
4317900064
4317900064
4317900096
4317900128

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

Console
# 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:

Code
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?

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

Output:

Code
4519884624
4519884768

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

VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Python id() for Custom Objects

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

Python
class Emp:
    a = 0


e1 = Emp()
e2 = Emp()

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

Output:

Code
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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren