如何删除实例化对象 Python?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21514631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:01:55  来源:igfitidea点击:

How to delete an instantiated object Python?

pythonvariablesobject

提问by user2992997

I am relatively new to object oriented programming and I cannot figure out how to delete an instantiated object in python. Any help would be much appreciated.

我对面向对象编程比较陌生,我无法弄清楚如何在 python 中删除实例化的对象。任何帮助将非常感激。

        if self.hit_paddle(pos) == True or self.hit_paddle2(pos) == True:
            bar = bar + 1
        if bar == 1:
            global barbox1
            barbox1 = barfill(canvas)
            barbox1.canvas.move(barbox1.id, 253, 367)
        if bar == 2:
            global barbox2
            barbox2 = barfill(canvas)
            barbox2.canvas.move(barbox2.id, 293, 367)
        if bar == 3:
            global barbox3
            barbox3 = barfill(canvas)
            barbox3.canvas.move(barbox3.id, 333, 367)
        if bar == 4:
            global barbox4
            barbox4 = barfill(canvas)
            barbox4.canvas.move(barbox4.id, 373, 367)
        if bar == 5:
            global barbox5
            barbox5 = barfill(canvas)
            barbox5.canvas.move(barbox5.id, 413, 367)
            bar = 0
            time.sleep(0.2)
            barbox1 = None
            barbox2 = None
            barbox3 = None
            barbox4 = None
            barbox5 = None

That is the code, the main thing I was trying in order to delete the objects was barbox1 = None, but that doesn't seem to work.

这就是代码,我试图删除对象的主要内容是 barbox1 = None,但这似乎不起作用。

回答by gitesh.tyagi

object.__del__(self)is called when the instance is about to be destroyed.

object.__del__(self)在实例即将被销毁时调用。

>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted

Object is not deleted unless all of its references are removed(As quoted by ethan)

除非删除所有引用,否则不会删除对象(如 ethan 引用)

Also, From Python official doc reference:

另外,来自 Python 官方文档参考:

del x doesn't directly call x.del() — the former decrements the reference count for x by one, and the latter is only called when x‘s reference count reaches zero

del x 不直接调用 x。del() — 前者将 x 的引用计数减一,后者仅在 x 的引用计数达到零时调用

回答by Ethan Furman

What do you mean by delete? In Python, removing a reference (or a name) can be done with the delkeyword, but if there are other names to the same object that object will not be deleted.

你是什​​么意思delete?在 Python 中,可以使用del关键字来删除引用(或名称),但如果同一对象还有其他名称,则不会删除该对象。

--> test = 3
--> print(test)
3
--> del test
--> print(test)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'test' is not defined

compared to:

相比:

--> test = 5
--> other is test  # check that both name refer to the exact same object
True
--> del test       # gets rid of test, but the object is still referenced by other
--> print(other)
5