python相同中文字符串id不同_为什么同一对象的不同方法有相同的“id”?

Python重用相同的内存位置,因为没有其他对对象的引用,一旦对id(a.f)求值,就会有更多对该对象的引用,因此Python就可以为a.g重用相同的内存位置。如果将方法指定给名称,您将看到不同的行为:# creates a reference to the method f

In [190]: f = a.f

# creates a reference to the method g

In [191]: g = a.g

# cannot reuse the memory location of f as it is still referenced

In [192]: id(f) == id(g)

Out[192]: False

实际上,您只需要存储一个对f的引用就可以看到与上述相同的行为。在

^{pr2}$

您可以看到带有^{}或^{}的引用计数:In [2]: import gc

In [3]: f = a.f

In [4]: len(gc.get_referrers(a.g)),len(gc.get_referrers(f))

Out[4]: (0, 1)

In [5]: sys.getrefcount(a.g),sys.getrefcount(f)

Out[5]: (1, 2)

您看到a.g为1的唯一原因是返回的计数通常比您预期的高一个,因为它包含(临时)引用作为getrefcount()的参数。

这与您自己的示例类似,在对方法求值之后,您仍然会引用f,使用a.g,refcount将为0,因此它将立即被垃圾回收,python可以自由地将内存位置用于任何其他操作。在

值得注意的是,行为不仅限于方法,它只是cpython实现的细节,而不是您应该依赖的东西:In [67]: id([]), id([])

Out[67]: (139746946179848, 139746946179848)

In [73]: id(tuple()),id([]),id([])

Out[73]: (139747414818888, 139746946217544, 139746946217544)

In [74]: id([]),id([]),id([])

Out[74]: (139746946182024, 139746946182024, 139746946182024)

In [75]: id([]),id(tuple()),id([])

Out[75]: (139746946186888, 139747414818888, 139746946186888)

In [76]: id(tuple()),id([]),id(tuple())

Out[76]: (139747414818888, 139746946217736, 139747414818888)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值