本篇文章给人人带来的内容是关于python中的id()函数及读取list的要领引见(代码示例),有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。
id(object)
Return the “identity” of an object. This is an integer (or long 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.
CPython implementation detail: This is the address of the object in memory.
提及这个函数就须要先相识pyhton的变量存储机制了:
变量:是动态变量,不必提早声明类型。
当我们写:a = 'ABC'时,Python诠释器干了两件事变:
在内存中创建了一个'ABC'的字符串;
在内存中创建了一个名为a的变量,并把它指向'ABC'。
id(a)读取的是a的内存地点
顺序类型
def addElement(_list): print(6,id(_list)) _list.append(0) print(7,id(_list)) return _list if __name__=="__main__": list1=[1,2,3] print(1,id(list1)) list2 = addElement(list1) print(2,list1) print(3,id(list1)) print(4,list2) print(5,id(list2))
实行效果:
(1, 48757192L) (6, 48757192L) (7, 48757192L) (2, [1, 2, 3, 0]) (3, 48757192L) (4, [1, 2, 3, 0]) (5, 48757192L)
两个要点:
return语句返回后list1就已变成其返回值而不是本来的值
自从定义后list1这个变量的实质就是一个内存盒子,传到函数内里的一直是这个变量自身,所以地点没变,末了返回的照样他,只是背面加了一个新值,而用a=b这类赋值要领后ab的内存地点是一致的。因而从头至尾list1,list2,_list内存地点都没变过
以上就是python中的id()函数及读取list的要领引见(代码示例)的细致内容,更多请关注ki4网别的相干文章!