Python id() Function
id()It is a built-in function in Python used to obtain the unique identifier of an object.
In Python, each object has a unique integer identifier, which is unique and constant throughout the object's lifetime.id()It returns this identifier, which usually corresponds to the object's memory address.
Word Definition: idYesidentifierAbbreviation for (identifier).
Basic Syntax and Parameters
Syntax Format
id(object)
Parameter Description
- Parameter object:
- Type: Any object
- Description: The object whose identifier is to be obtained.
Function Description
- Return Value: Returns an integer representing the object's unique identifier.
- Characteristics: The id remains constant throughout the object's lifetime.
Examples
Example 1: Basic Usage
Example
# View the id of an integer
a = 10
print(id(a)) # Output: an integer, e.g., 4302400000
# View the id of a string
s = "hello"
print(id(s)) # Output: an integer
# View the id of a list
lst = [1, 2, 3]
print(id(lst)) # Output: an integer
a = 10
print(id(a)) # Output: an integer, e.g., 4302400000
# View the id of a string
s = "hello"
print(id(s)) # Output: an integer
# View the id of a list
lst = [1, 2, 3]
print(id(lst)) # Output: an integer
Expected Output:
Returns the object's unique identifier (an integer).
Code Analysis:
- Every object has a unique id.
- Different objects usually have different ids.
Example 2: Verify Object Identity
Example
# Same object referenced multiple times
a = [1, 2, 3]
b = a
print(id(a) == id(b)) # Output: True (a and b point to the same object)
# Copy an object
a = [1, 2, 3]
b = a.copy()
print(id(a) == id(b)) # Output: False (a and b are different objects)
# Integer caching (small integers)
a = 256
b = 256
print(id(a) == id(b)) # Output: True (small integers are cached)
a = 257
b = 257
print(id(a) == id(b)) # Output: False (large integers are not cached)
a = [1, 2, 3]
b = a
print(id(a) == id(b)) # Output: True (a and b point to the same object)
# Copy an object
a = [1, 2, 3]
b = a.copy()
print(id(a) == id(b)) # Output: False (a and b are different objects)
# Integer caching (small integers)
a = 256
b = 256
print(id(a) == id(b)) # Output: True (small integers are cached)
a = 257
b = 257
print(id(a) == id(b)) # Output: False (large integers are not cached)
Expected Output:
True False True False
Code Analysis:
- Use
==Comparing ids can determine whether they are the same object. - Python caches small integers (-5 to 256), so variables with the same value may share the same id.
Example 3: Practical Application
Example
# Check whether it is the same object
def check_same_object(a, b):
return id(a) == id(b)
# Test
list1 = [1, 2, 3]
list2 = list1
list3 = [1, 2, 3]
print(check_same_object(list1, list2)) # Output: True
print(check_same_object(list1, list3)) # Output: False
# Use as a dictionary key (though not common)
d = {}
obj = object()
d[obj] = "value"
print(d[obj]) # Output: value
# View objects during debugging
class Person:
pass
p = Person()
print(f"Object id: {id(p)}")
print(f"Class id: {id(Person)}")
def check_same_object(a, b):
return id(a) == id(b)
# Test
list1 = [1, 2, 3]
list2 = list1
list3 = [1, 2, 3]
print(check_same_object(list1, list2)) # Output: True
print(check_same_object(list1, list3)) # Output: False
# Use as a dictionary key (though not common)
d = {}
obj = object()
d[obj] = "value"
print(d[obj]) # Output: value
# View objects during debugging
class Person:
pass
p = Person()
print(f"Object id: {id(p)}")
print(f"Class id: {id(Person)}")
Expected Output:
True False value 对象 id: ... 类 id: ...
id()Mainly used for debugging, checking object identity, understanding Python memory management, and other aspects.
Other Extensions
Python3 Built-in Functions