I want to make simple delete
function in python. This function should remove any reference, and delete the object.
Here is a simple example:
import numpy as np
class Unit:
def __init__(self):
# 8GB of data
self.data = np.random.randint(0, 1, 1000*1000*1000)
self.HP = 5
def attack(unit):
unit.HP -= 10
if unit.HP <= 0:
# Here we remove all the reference to the unit
"""
Some codes on removing references
"""
del unit
unit = Unit()
attack(unit)
print(unit.data) # <- this should be not accessible
input('Press any key to exit...')
But this would not work:
[0 0 0 ... 0 0 0]
Press any key to exit...
Notice that the unit
can be defined on the global scope, or local scope, which depends on the game environment.
Notes
Since there is no reference to thisunit
object, the counted reference number is zero, so the garbage collector should release this from the memory. But I don’t know how can I do this explicitly inside some function.
It would be really appreciated if you give me any hint or any stuff.
Thanks,