The function id () in Python
The built-in function id () in Python returns a single integer value that identifies an object.
The built-in function id () in Python returns a single integer value that identifies an object.
- All objects in Python have their own unique id .
- Id is assigned to the object when it is created.
Id () function syntax
id(object)
Parameters of the id function ()
Id () has a unique parameter:
object: is an object, number, string, list, class . that must return id.
Value returned from id ()
The id () function returns the identity of the object. This is a unique integer for the injected object and remains constant throughout its life cycle.
For example
1. The way id () works in Python
class Demo: b = 5 qtmDemo = Demo() print('id của qtmDemo =',id(qtmDemo))
Run the program, the result will be in the form:
id của qtmDemo = 139870602688104
2. Some other examples:
print('id của 5 =',id(5)) a = 5 print('id của a =',id(a)) b = a print('id của b =',id(b)) c = 5.0 print('id của c =',id(c))
Result:
id của 5 = 10914496 id của a = 10914496 id của b = 10914496 id của c = 139870675422424
See also: Built-in Python functions
You've just finished reading the article "The function id () in Python" edited by the TipsMake team. You can save the-function-id-in-python.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.