python ide,python ide_Python id()

 2023-11-19 阅读 36 评论 0

摘要:python idePython id() function returns the “identity” of the object. The identity of an object is an integer, which is guaranteed to be unique and constant for this object during its lifetime. Python id()函數返回對象的“身份”。 一個對象的

python ide

Python id() function returns the “identity” of the object. The identity of an object is an integer, which is guaranteed to be unique and constant for this object during its lifetime.

Python id()函數返回對象的“身份”。 一個對象的身份是一個整數,在該對象的生存期內,它保證是唯一且恒定的。

python ide?Two objects with non-overlapping lifetimes may have the same id() value. In CPython implementation, this is the address of the object in memory.

具有不重疊生存期的兩個對象可能具有相同的id()值。 在CPython實現中,這是內存中對象的地址。

Python id() (Python id())

Python cache the id() value of commonly used data types, such as string, integer, tuples etc. So you might find that multiple variables refer to the same object and have same id() value if their values are same.

Python緩存了常用數據類型的id()值,例如字符串 , 整數 , 元組等。因此,您可能會發現多個變量引用同一對象,并且如果它們的值相同,則它們具有相同的id()值。

python list,Let’s check this out with an example.

我們來看一個例子。

# integers
a = 10
b = 10
c = 11
d = 12print(id(a))
print(id(b))
print(id(c))
print(id(d))

Output:

輸出:

4317900064
4317900064
4317900096
4317900128

pythonista。Notice that id() value of ‘a’ and ‘b’ are same, they have the same integer value.

請注意,“ a”和“ b”的id()值相同,它們具有相同的整數值。

Let’s see if we get the similar behavior with string and tuples too?

讓我們看看我們是否也通過字符串和元組得到類似的行為?

# tuples
t = ('A', 'B')
print(id(t))t1 = ('A', 'B')
print(id(t1))# strings
s1 = 'ABC'
s2 = 'ABC'
print(id(s1))
print(id(s2))

python3。Output:

輸出:

4320130056
4320130056
4320080816
4320080816

From the output, it’s clear that Python cache the strings and tuple objects and use them to save memory space.

從輸出中很明顯,Python緩存了字符串和元組對象,并使用它們來節省內存空間。

Caching can work only with immutable objects, notice that integer, string, tuples are immutable. So Python implementation can use caching to save memory space and improve performance.
緩存只能用于不可變的對象,請注意整數,字符串和元組是不可變的。 因此,Python實現可以使用緩存來節省內存空間并提高性能。

python的五個特點、We know that dictionary is not immutable, let’s see if id() is different for different dictionaries even if the elements are same?

我們知道字典不是一成不變的,讓我們看看即使字典中的元素相同,不同字典的id()是否也不同?

# dict
d1 = {"A": 1, "B": 2}
d2 = {"A": 1, "B": 2}
print(id(d1))
print(id(d2))

Output:

輸出:

4519884624
4519884768

python groupby,As we thought, dict objects are returning different id() value and there seems no caching here.

正如我們所想,dict對象返回的id()值不同,并且這里似乎沒有緩存。

自定義對象的Python id() (Python id() for custom object)

Let’s see a simple example of getting id() value for a custom object.

讓我們看一個為自定義對象獲取id()值的簡單示例。

class Emp:a = 0e1 = Emp()
e2 = Emp()print(id(e1))
print(id(e2))

python divmod、Output:

輸出:

4520251744
4520251856

摘要 (Summary)

Python id() value is guaranteed to be unique and constant for an object. We can use this to make sure two objects are referring to the same object in memory or not.

Python id()值對于一個對象保證是唯一且恒定的。 我們可以使用它來確保兩個對象是否引用內存中的同一對象。

GitHub Repository.GitHub存儲庫中檢出完整的python腳本和更多Python示例。

python 在線ide,Reference: Official Documentation

參考: 官方文檔

翻譯自: https://www.journaldev.com/22925/python-id

python ide

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/183214.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息