Python Forum
Destructor method adding in string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Destructor method adding in string
#1
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
Reply
#2
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.
chizzy101010 likes this post
Reply
#3
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.
chizzy101010 and buran like this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  comtypes: how to provinde a list of string to a COM method zalanthas 0 981 Jun-26-2024, 01:27 PM
Last Post: zalanthas
  Adding string after every 3rd charater [SOLVED] AlphaInc 2 2,111 Jul-11-2022, 09:22 AM
Last Post: ibreeden
  Problem with adding arbitrary parrameters to __init__ method sebastianvdn 1 2,562 Feb-03-2020, 09:30 PM
Last Post: micseydel
  Adding markers to Folium map only adding last element. tantony 0 2,904 Oct-16-2019, 03:28 PM
Last Post: tantony
  How to run a method on an argument in a formatted string Exsul 1 2,197 Aug-30-2019, 01:57 AM
Last Post: Exsul
  How to use a string method on user input Exsul 2 3,389 Mar-17-2019, 08:12 PM
Last Post: Exsul
  String Method 'find(...)'. ClassicalSoul 3 3,161 Feb-27-2019, 12:24 PM
Last Post: buran
  adding a string and an int ninjarunner2005 3 3,102 Dec-10-2018, 04:54 PM
Last Post: buran
  "replace() method" fails to change string pw928gts 4 4,967 Nov-30-2018, 05:48 PM
Last Post: nilamo
  build a list (add_animals) using a while loop, stop adding when an empty string is en nikhilkumar 1 9,850 Jul-17-2017, 03:29 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020