Skip to main content

id() function in Python

In Python, the id() function is a built-in function that returns the unique identifier of an object. The identifier is an integer, which represents the memory address of the object. The id() function is commonly used to check if two variables or objects refer to the same memory location.

Syntax: id(object)

Return: a unique integer for a given object

Example:

Python3




x = 42
y = x
z = 42
 
print(id(x)) 
print(id(y))  # (same as x)
print(id(z))  # (same as x and y)

Output :

10731304
10731304
10731304

As we can see the function accepts a single parameter and is used to return the identity of an object. This identity has to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value. If we relate this to C, then they are actually the memory address, here in Python it is the unique id. This function is generally used internally in Python. 

Python id() function Examples

Python id() for Inbuilt DataTypes

In this example, we are printing the id of multiple datatypes like strings and lists.

Python3




# This program shows various identities
str1 = "geek"
print(id(str1))
 
str2 = "geek"
print(id(str2))
 
# This will return True
print(id(str1) == id(str2))
 
# Use in Lists
list1 = ["aakash", "priya", "abdul"]
print(id(list1[0]))
print(id(list1[2]))
 
# This returns false
print(id(list1[0])==id(list1[2]))

Output :

140252505691448
140252505691448
True
140252505691840
140252505739928
False

Python id() for custom object

In this example, we are creating the Python class and we are creating two Python class objects and checking their ids.

Python3




class MyClass:
    pass
 
# Create two instances of MyClass
obj1 = MyClass()
obj2 = MyClass()
 
# Print the id of each object
print(id(obj1))
print(id(obj2))

Output :

139783388205328
139783388205392

Python id() with Sets

In this example, we are using the id function on sets in Python.

Python3




set1 = 1, 2, 3
set2 = 3, 2, 1
set3 = 1, 2, 3
 
print(id(set1))  # Output:
print(id(set2))  # Output:
print(id(set3))  # Output:

Output :

139755710961408
139755710961184
139755709277664

Python id() with Tuples

In this example, we are using the id function on tuples in Python.

Python3




tuple1 = (1, 2, 3)
tuple2 = (3, 2, 1)
tuple3 = (1, 2, 3)
 
print(id(tuple1))  # Output:
print(id(tuple2))  # Output:
print(id(tuple3))  # Output:

Output :

140102982048128
140102982084288
140102982048128

https://neveropen.tech/id-function-in-python/?feed_id=126&_unique_id=683d1a22cc926

Comments

Popular posts from this blog

Bare Metal Billing Client Portal Guide

Contents Order a Bare Metal Server My Custom / Contract Pricing View Contract Details Location Management Order History & Status View Order Details Introduction The phoenixNAP Client Portal allows you to purchase bare metal servers and other phoenixNAP products and services. Using the intuitive interface and its essential tools, you can also easily manage your infrastructure. This quick guide will show you how to use the new form to order a bare metal server and how to navigate through new bare metal features within the phoenixNAP Client Portal. Order a Bare Metal Server An order form is an accordion-based process for purchasing phoenixNAP products. Our order form allows you to view the pricing and order multiple products from the same category at the same time. Note: The prices on the form are per month . A contract is not required. However, if you want a contracted price, you may be eligible for a discount depending on the quantity and ...

Add an element in Array to make the bitwise XOR as K

Given an array arr[] containing N positive integers, the task is to add an integer such that the bitwise Xor of the new array becomes K. Examples: Input: arr[] = 1, 4, 5, 6, K = 4 Output: 2 Explanation: Bit-wise XOR of the array is 6.  And bit-wise XOR of 6 and 2 is 4. Input: arr[] = 2, 7, 9, 1, K = 5 Output: 8   Approach: The solution to the problem is based on the following idea of bitwise Xor: If for two numbers X and Y , the bitwise Xor of X and Y is Z then the bitwise Xor of X and Z is Y. Follow the steps to solve the problem: Let the bitwise XOR of the array elements be X .  Say the required value to be added is Y such that X Xor Y = K . From the above observation, it is clear that the value to be added (Y) is the same as X Xor K . Below is the implementation of the above approach: C++ // C++ code to implement the above approach   #include using namespace std;   // Function to find the required value int find_...

Mahotas – Template Matching

In this article we will see how we can do template matching in mahotas. Template is basically a part or structure of image. In this tutorial we will use “lena” image, below is the command to load it.   mahotas.demos.load('lena') Below is the lena image      In order to do this we will use mahotas.template_match method Syntax : mahotas.template_match(img, template) Argument : It takes image object and template as argument Return : It returns image object    Note : Input image should be filtered or should be loaded as grey In order to filter the image we will take the image object which is numpy.ndarray and filter it with the help of indexing, below is the command to do this   image = image[:, :, 0] Below is the implementation    Python3 # importing required libraries import mahotas import mahotas.demos from pylab import gray, imshow, show import numpy as np import matplotlib.pyplot as plt      # loading image ...