简体   繁体   English

什么表示在Python中的print / repr上显示的十六进制整数?

[英]What does represent the hexadecimal integer showed on print/repr in Python?

In an interactive session like the following one: 在类似于以下的交互式会话中:

>>> f=open('test.txt','w')
>>> f
<open file 'test.txt', mode 'w' at 0x6e610>

what does 0x6e610 represent and what could I do with that hexadecimal number in Python? 0x6e610代表什么?在Python中该十六进制数怎么办?

>>> f=open('test.txt')
>>> f
<open file 'test.txt', mode 'r' at 0x10047c938>
>>> hex(id(f))
'0x10047c938'

Have a look at id in the official documentation : 看一下官方文档中的id

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. 具有不重叠生存期的两个对象可能具有相同的id()值。

It's the ID of the object, which (in standard Python) is its address in memory. 这是对象的ID,(在标准Python中)是对象在内存中的地址。

You can also get it via the id(obj) function. 您也可以通过id(obj)函数获取它。

You can use IDs to tell whether two references refer to the same object - when you say if x is y in Python, you're comparing IDs. 您可以使用ID来判断两个引用是否引用同一个对象-当您说Python中if x is y时,就是在比较ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM