Python Forum

Full Version: Destructor method adding in string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi to all, i've got a question that i hope someone can help with, i've bought a book with examples and one of those examples used a destructor method, which was ok.
#Define a destructor method, which is called when all object resources are deleted

class MyClass:
    def __init__(self):
        print("Constructor is called")
    def __del__(self):
        print("Destructor is called")
        print("Object deleted")
        
obj = MyClass()

del obj
what i tried to do was to add the name of the object being deleted to the print output, just to show what was getting deleted, but seem to have ran into a brick wall with what i thought would work, here's what i thought would work, could someone tell me please if this is possible, all responses are appreciated.

#Define a destructor method, which is called when all object resources are deleted

class MyClass:
    def __init__(self):
        print("Constructor is called")
    def __del__(self,obname):
        print("Destructor is called")
        print("Object deleted" + self.obname)
        
obj = MyClass()
nameit = "obj"
del obj,nameit
There is no way to pass an argument to __del__ other than the object to be deleted (self).

Calling del obj, nameit deletes obj and namit. It is the same as:
del obj
del namit
If you want to print the name of the object being deleted, give the object a name.
class A:
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name

    def __del__(self):
        print("Deleting", self)


a = A("A")
b = A("B")
c = A("C")
del a, b
print("got here")
Deleting A
Deleting B
got here
Deleting C
I should say that "del obj" deletes the object referenced by the variable obj. obj is a variable, not an object. Think of it as a convenient name used to reference the variable, like a key in a dictionary.

Calling del obj is unusual. Normally objects are deleted automatically when there are no references to the object.
class A:
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name

    def __del__(self):
        print("Deleting", self)


for name in "ABCD":
    print("Making", name)
    obj = A(name)
Output:
Making A Making B Deleting A Making C Deleting B Making D Deleting C Deleting D
A is deleted when obj is reassigned to reference B. B deleted when obj is assigned to C. C deleted when obj is assigned to D, and D is deleted when the program ends.
I also recommend reading the official documentation of the __del__() method. I think 99.99 % of the time, it is at best pointless to define a __del__() method in a class.
Thanks again deanhystad for the detailed explanation, it was just something that came into my head when i was going through this book of 300 examples i bought, i was just wondering if it was possible, thanks again for your time.

Thanks for the link Gribouillis, i'll check the documentation next time i am looking at anything else