简体   繁体   English

我如何删除python3中的对象*

[英]How can i delete objects in python3.*

I am writing a binary tree class. 我正在写一个二叉树类。 And i would like to delete the whole tree (all nodes in it) So i want to understand how to delete objects in python. 而且我想删除整个树(其中的所有节点),所以我想了解如何删除python中的对象。

I have function del_obj(obj). 我有函数del_obj(obj)。 I pass there "a" and it doesn't work correctly. 我在那儿传递了“ a”字样,但无法正常工作。 As i suppose, it is copy of "a". 正如我想的那样,它是“ a”的副本。 But id method shows the same id. 但是id方法显示相同的id。 I am confused. 我很困惑。

def del_obj(obj):                                                               
   print(id(obj)) # 111                                                              
   del obj                                                                     

a = 3                                                                           
print(id(a)) # 111                                                                    
del_obj(a)
print(a) # 3  

In Python, del doesn't delete an object , it deletes a name . 在Python中, del不会删除对象 ,而是会删除name The object will only get deleted when it cannot be reached from any name anymore. 仅当无法再使用任何名称访问该对象时,该对象才会被删除。

Once the root of the tree cannot be reached from any name, all the other nodes in the tree which could only be reached from a name that is associated with that root object will themselves become unreachable, and all the nodes will get deleted, unless someone has another name somewhere that is pointing at one of them. 一旦无法通过任何名称访问树的根,则只能从与该根对象关联的名称访问树中的所有其他节点,它们自身将变得不可访问,并且除非有某人,否则所有节点都将被删除。在某处有另一个名称指向其中一个。

(If you have a cycle of names then that still should all be cleaned up, but the CPython implementation has a few gotchas in this case. But since you describe the data structure as a tree, this probably doesn't apply to your situation.) (如果您有一个名称循环 ,那么仍然应该清除所有名称,但是在这种情况下,CPython实现有一些陷阱。但是由于您将数据结构描述为树,因此这可能不适用于您的情况。 )

I would recommend you read the classic article Facts and myths about Python names and values . 我建议您阅读经典文章《 关于Python名称和值的事实与神话》

You need to say del a in the outer scope. 您需要在外部范围内说del a Your del_obj function doesn't work because it only deletes its own reference, which doesn't affect the code outside that function. 您的del_obj函数无效,因为它只会删除自己的引用,这不会影响该函数之外的代码。

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

相关问题 我无法在python3上导入scikit -learn。 如何导入? - I can`t import scikit -learn on python3. how can i import it? 我正在使用python3。 我如何切换到v2.7 - I am using python3. how can i switch to v2.7 我正在使用一个期待 Python3 的项目。 如何在不搞砸任何事情的情况下使用它? - I’m using a project which is expecting Python3. How can use that without messing anything up? python3中可选的yield或return。 如何? - Optional yield or return in python3. How to? 我在笔记本电脑上安装了烧瓶。 但它似乎仅在python3上运行。 如何使其适用于python2? - I installed flask on my laptop. But it seems to be running only for python3. How to make it work for python2? 蟒蛇3。 类,继承 - Python3. Classes, Inheritance 蟒蛇3。 如何检查后缀是否在列表中的某些单词中? - Python3. How to check if a suffix is in some of the words in a list? 蟒蛇3。 如何将下载的网页保存到指定的目录? - Python3. How to save downloaded webpages to a specified dir? 蟒蛇3。 八叉树实现涉及很多 memory。 如何优化? - Python3. Octree implementation engages a lot of memory. How to optimize? Python 或 Python3。 有什么区别? - Python or Python3. What is the difference?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM