python解释器不会删除python中的键和值吗?

2024-04-26 07:26:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我英语不好,python也不在行,但我对python地图有一个小小的疑问

它们是否分配给每个键和值? 如果我创建了这样的对象

my_map = {}
my_map['id'] = someObject()

某个对象不是悬空的指针吗? 不会被python解释器删除吗


Tags: 对象idmapmy地图解释器指针someobject
2条回答

正式:

my_map['id']是一个subscription

那么你有:

Assignment statements are used to (re)bind names to values and to modify attributes or items of mutable objects:

...

If the primary is a mapping object (such as a dictionary), the subscript must have a type compatible with the mapping’s key type, and the mapping is then asked to create a key/datum pair which maps the subscript to the assigned object. This can either replace an existing key/value pair with the same key value, or insert a new key/value pair (if no key with the same value existed).

从这里开始Assignment statements

因此,基本上您是将最近创建的对象绑定/映射到下标,当然,新创建的对象可以被my_map['id']引用,因此Python垃圾收集器不能触及它(回收它),它不是一个“悬空指针”,更确切地说,它不是一个没有任何引用的对象

旁注:adangling pointer不是一个用于引用没有引用的对象的术语(您是如何使用这个术语的),而是一个对非有效对象的引用(这在Python中是不可能的)

Python中没有指针。Python中有一个绑定到对象的名称

相关问题 更多 >