deleting an object in python

class Thing(object):
    # The function below runs when this object is deleted.
    # I just put it there so you can visulise it properly.
    def __del__(self): 
        print("Object was deleted.")
    
    def func(self):
        print("Random function ran from class Thing().")

random_object = Thing()
random_object.func() # Run the function so you know the class exists.
del random_object # Delete the object. Will run the __del__ function when done this.

# Try to run the func() function now, it won't run because the object is deleted.
random_object.func() # Produces NameError.

5
1
Lionel Aguero 33605 points

                                    >>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted

5 (1 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to destroy an object in python ? how to delete an object of class in python python3 delete object how to delete a class object python del object python delete object pyrthon How do you delete an object in python remove [] in a object pyhon python delete from object deleting object in python python object delete method python object delete how to get object to delete in python python object remove element python remove element from object how to delete an object python how to make an object delete itself python delete a object in python class how do I delete an element in an object in python delete object pyth remove item from object python python remove from object how to delete the object of class in pytohn delete object python with a method how to delete an item from python object python how delete object how to delete method from object python how to delete object of a class in python python deleting objects and creating destroy an object from object in python destroy an object in python delete python object with method delete object python how to delete object in python delete objects python do we need to delete python objects how to get rid of objects in python del with object python how to remove item from object in python python how to delete an object python does something when object deletion python create and delete new object remove element from object python deleting a class object in python python erase objects del python object python delete object in function python remove object delete class object python pytohn delete object python should you delete objects destroy an object python django delete object how to delete objects in python python delete object vpython object delete how to delete an object in python how to delete a object in python delete an object python eliminate object python python del class object delete objects in python python delete object from object method eliminate python object python class delete object delete object in python python delete object from class delte an object python pythno delete object deleting object in vpython how to destroy an object in python deleting objects in python delete item in an object python how to delete a class object in python how to delete objects python delete object in class python deleting an object in python python del obj remove object python how to delete object python what if we remove object() python python delete object variable remove items from object python how to delete object in array python how to delete a python object python on delete object python delete an object after use python delete an ob delete python object destroy object python remove part of an object python delete part of an object python remove object py deleting an object in json python oop python remove object deleting objects python delete all copies of an object python python delete instance of object how to delete an instance python how to unload an object from python python delete clsass object after use remove objects from python How to clear python objects python remove a class object del an object in python how to remove an object in python object.delte in python delete in python class delete a class object python how to delete object of class in python del object in python destroy object in python python delete class object kill object python python clear a object how to remove object python object remove python drop an object in python delete an object in python python destroy object python how to delete object instance
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source