OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python remove or delete object inside a function

  • Thread starter Thread starter Seung Hyeon Yu
  • Start date Start date
S

Seung Hyeon Yu

Guest
I want to make simple delete function in python. This function should remove any reference, and delete the object. This can be used to in game or removing unused simulated objects.

Here is a simple example:

Code:
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:

Code:
[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 this unit object, the reference counter 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,
<p>I want to make simple <code>delete</code> function in python. This function should remove any reference, and delete the object. This can be used to in game or removing unused simulated objects.</p>
<p>Here is a simple example:</p>
<pre class="lang-py prettyprint-override"><code>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...')
</code></pre>
<p>But this would not work:</p>
<pre class="lang-bash prettyprint-override"><code>[0 0 0 ... 0 0 0]
Press any key to exit...
</code></pre>
<p>Notice that the <code>unit</code> can be defined on the global scope, or local scope, which depends on the game environment.</p>
<blockquote>
<p><strong>Notes</strong>
Since there is no reference to this <code>unit</code> object, the <a href="https://en.wikipedia.org/wiki/Reference_counting" rel="nofollow noreferrer">reference counter</a> 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.</p>
</blockquote>
<p>It would be really appreciated if you give me any hint or any stuff.</p>
<p>Thanks,</p>
 

Latest posts

A
Replies
0
Views
1
AgencyAnalytics
A
S
Replies
0
Views
1
Stacker Media
S
C
Replies
0
Views
1
CC.Talent
C
Top