Basj
Basj

Reputation: 46423

Delete a Python object and make it point to None

Here is my class definition :

class Playingsound:
    def ___init___(self):
        # blah

    def fadeout_and_stop(self):
        # do somthing (fadeout during 100 ms)
        del self

Here is my problem (similar to that one : Python object deleting itself) :

>>> a = Playingsound()
>>> time.sleep (1.0)
>>> a.fadeout_and_stop()
>>> time.sleep (1.0)    # after 1 second, the playback should be finished !
>>> a
<__main__.Playingsound instance at 0x01F23170>

Instead of this, I would like a to be totally destroyed after the call of fadeout_and_stop, and its reference to be None :

>>> a
<None>

How to do this with Python ?

Upvotes: 3

Views: 6612

Answers (3)

Nasif Imtiaz Ohi
Nasif Imtiaz Ohi

Reputation: 1721

you can't destroy an object in python. It's the garbage collector's job. What you can do, is to change a reference to None or delete the reference. But if the object still has other links of reference, it will stay in the memory unharmed. Look at this example,

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
if __name__=='__main__':
    l1=ListNode(9)
    t=ListNode(8)
    l1.next=t
    l2=ListNode(1)
    s=Solution()
    r=s.addTwoNumbers(l1,l2)
    del t #not similar to t=None, this will fully destroy the existence
          #of the reference t, while t=None will only set the reference 
          #to None
    print l1.next.val #this will give the value 8
    print t #this line will give an error as reference t has no 
            #existence anymore

look the object that has a value 8 (created at the line, t=ListNode(8)) is still alive. We only destroyed the reference of t. But the actual object is at the hand of python's garbage collector. Only if we delete the other reference link (l1.next) then the object will lose all the reference links and will become an garbage and then python will destroy it completely from the memory. So while you have full control over the reference, you don't have control over the actual object.

Upvotes: 0

Vb407
Vb407

Reputation: 1707

Just try to print self after "del self" statement in finish function,by doing this you will get error because of python memory management. After creating 'a' object to class A, this object 'a' will be tagged to some memory location, When you are calling finish() you are passing reference of 'a' object. So when u use del self just memory reference will be untagged to the object, But still our object is 'a' is pointing to that memory location. you need untag all references in order to destroy tha object.

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1125298

You cannot, not without looping through all references in the garbage collector and testing each and every one if it is a reference to this object, then setting that reference to None. You don't want to go there. Remember: you can have more than one reference to your object:

a = A()
b = a
a.finish()  # what should be set to `None` now? a, b or both?

Instead of a.finish(), do del a, perhaps combined with implementing a __del__ clean-up hook.

If you need to have your object cleaned up after a timeout, add your object to a global list, and remove it from that list when done playing. The list can be on the class itself:

class Playingsound:
    playing = []

    def fadeout_and_stop(self):
        Playingsound.playing.append(self)
        # do somthing (fadeout during 100 ms)
        Playingsound.playing.remove(self)

then if there are no other references to the instance Python will take care of cleaning it up for you:

a = Playingsound()
a.fadeout_and_stop()
del a

You can always access any sounds still playing via Playingsound.playing.

Upvotes: 4

Related Questions