Python Forum
del self does not work! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: del self does not work! (/thread-33109.html)



del self does not work! - quest - Mar-30-2021

Hello,

Here is my class:

class EO:

    #TODO : add method to display/print the object
    fragile=True #EO is «destroyed» when 1 bit is lost #TODO update this hack
    
    def __init__(self,state,n,*extraargs):
        self.state=state
        self.bits=[qubit(self,k,extraargs) for k in range(n)]
        self.extraargs=extraargs
        
    def __len__(self):
        return len(self.bits)
    
    def losebit(self, k):
        """Implements loss of bit(k). It should already be removed from all datastructures"""
        if not self.fragile : raise NotImplementedError('Only fragile losses implementend yet') 
        else : self.state=False
        for qb in self.bits[k+1:] :
            qb.pos -=1
        del self.bits[k]
        #if len(self)==0 : del self #doesn’t work. 
        ## But actually should happen automatically by garbage collection when last ref
        ## is removed
    
Here I wanted to delete self if the len(self) is equal zero but it did not work. What can I do?