python函数示例
Python id()函数 (Python id() function)
id() function is a library function in Python, it is used to get a unique identity number (id) of an object, it accepts an object (like int, float, string, list, etc) and returns unique id number.
id()函数是Python中的一个库函数,用于获取对象的唯一标识号(id),它接受一个对象(如int,float,字符串,列表等)并返回唯一的id号。
What is an Id?
什么是ID?
An Id is a memory address of each object, which is assigned while an object is created, all objects have their own unique identity number, that can be changed to execute the program again.
Id是每个对象的内存地址,在创建对象时分配该地址,所有对象都有自己的唯一标识号,可以更改该标识号以再次执行程序。
Syntax:
句法:
id(object)
Parameter(s):object – an object like int, float, string, list, tuple etc.
参数: object –一个对象,例如int,float,string,list,tuple等。
Return value: float – it returns a unique identity number (which is in integer format) of given object.
返回值: float –返回给定对象的唯一标识号(整数格式)。
Example:
例:
Input:
a = 10
print("id(a): ", id(a))
Output:
id(a): 10455328
Python code to get id(s) of various type of objects
Python代码可获取各种类型的对象的ID
# python code to demonstrate example
# of id() function
a = 10 # integer variable
b = 10.23 # float variable
c = "Hello" # string variable
d = [10, 20, 30, 40, 50] # list variable
# return type of id()
print("return type of id(): ", type(id(a)))
# getting id of objects
print("id(a): ", id(a))
print("id(b): ", id(b))
print("id(c): ", id(c))
print("id(d): ", id(d))
Output
输出量
First run:
return type of id(): <class 'int'>
id(a): 10455328
id(b): 139862812754400
id(c): 139862812332136
id(d): 139862811549960
Second run:
return type of id(): <class 'int'>
id(a): 10455328
id(b): 139836840616416
id(c): 139836840194152
id(d): 139836839411976
翻译自: https://www.includehelp.com/python/id-function-with-example.aspx
python函数示例