如何删除实例化对象Python?

use*_*997 24 python variables object

我对面向对象编程相对较新,我无法弄清楚如何在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
Run Code Online (Sandbox Code Playgroud)

那是代码,我为了删除对象而尝试的主要内容是barbox1 = None,但这似乎不起作用.

git*_*agi 39

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

>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted
Run Code Online (Sandbox Code Playgroud)

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

另外,从Python官方文档参考:

del x不直接调用x.del() - 前者将x的引用计数递减1,后者仅在x的引用计数达到零时调用

  • 我试过如果你直接设置`test = None`,`__del__`函数也会被调用。 (5认同)
  • 这不一定删除对象。 (4认同)

Eth*_*man 13

你什么意思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
Run Code Online (Sandbox Code Playgroud)

相比:

--> 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
Run Code Online (Sandbox Code Playgroud)

  • @Scott:通过足够的努力,您可能能够找到对一个对象的所有引用——但是如果这些引用中的任何一个位于您无法访问的命名空间(例如函数局部变量)中,您将无法`del `他们。 (3认同)
  • @他说得对,没有办法。幸运的是,因为如果某些具有引用的代码尝试访问该对象,您认为应该发生什么?这相当于 C、C++ 和类似语言中的悬空指针。通过不向程序员提供删除正在某处使用的对象的方法可以防止此类错误。 (2认同)

Muk*_*esh 5

Python 使用引用计数的概念来管理内存

这是什么意思?

当创建一个对象时,会创建一个用于存储该对象的专用内存,并返回该内存的地址。我们基本上将地址存储在变量中。

Python 跟踪存储了对象地址的变量数量,一旦所有变量都被删除/更改,Python 实际上会删除为该对象分配的内存。

假设我们有一个名为Treasure

class Treasure:
    def __del__(self):
        print("Treasure is gone")

print("John creates a treasure and he know the location of it (count is 1)")
john = Treasure()

print("John shares the treasure location with Stark (count is 2)")
stark = john

print("Stark shares the treasure location with Chris (count is 3)")
chris = stark

print("John dies (count is 2)")
del john

print("Stark dies (count is 1)")
del stark

print("Chris dies (count is 0)")
del chris
Run Code Online (Sandbox Code Playgroud)

输出

John creates a treasure and he know the location of it (count is 1)
John shares the treasure location with Stark (count is 2)
Stark shares the treasure location with Chris (count is 3)
John dies (count is 2)
Stark dies (count is 1)
Chris dies (count is 0)
Treasure is gone
Run Code Online (Sandbox Code Playgroud)

《宝藏消失》在克里斯死后只打印一次,那是因为当约翰死时斯塔克和克里斯知道宝藏的位置,当斯塔克死时克里斯知道宝藏的位置,但当克里斯死后就没有人知道宝藏的位置了。

__del__仅当最后一个引用被删除时才会调用该方法

getrefcount您可以使用示例获取对象的当前引用数sys

import sys

class A:
    """
    test class
    """

a = A()
b = a

print(sys.getrefcount(a))
Run Code Online (Sandbox Code Playgroud)

输出

3
Run Code Online (Sandbox Code Playgroud)

getrefcount通常由于调用中的临时引用,计数会多一