python中的id()函数及读取list的例子
生活随笔
收集整理的這篇文章主要介紹了
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解釋器干了兩件事情:
id(a)讀取的是a的內存地址
程序范例
def addElement(_list):print(6,id(_list))_list.append(0)print(7,id(_list))return _listif __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)兩個要點:
總結
以上是生活随笔為你收集整理的python中的id()函数及读取list的例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java™ 教程(Queue接口)
- 下一篇: Scrapy基本用法