[python] How to delete an instantiated object Python?

I am relatively new to object oriented programming and I cannot figure out how to delete an instantiated object in python. Any help would be much appreciated.

        if self.hit_paddle(pos) == True or self.hit_paddle2(pos) == True:
            bar = bar + 1
        if bar == 1:
            global barbox1
            barbox1 = barfill(canvas)
            barbox1.canvas.move(barbox1.id, 253, 367)
        if bar == 2:
            global barbox2
            barbox2 = barfill(canvas)
            barbox2.canvas.move(barbox2.id, 293, 367)
        if bar == 3:
            global barbox3
            barbox3 = barfill(canvas)
            barbox3.canvas.move(barbox3.id, 333, 367)
        if bar == 4:
            global barbox4
            barbox4 = barfill(canvas)
            barbox4.canvas.move(barbox4.id, 373, 367)
        if bar == 5:
            global barbox5
            barbox5 = barfill(canvas)
            barbox5.canvas.move(barbox5.id, 413, 367)
            bar = 0
            time.sleep(0.2)
            barbox1 = None
            barbox2 = None
            barbox3 = None
            barbox4 = None
            barbox5 = None

That is the code, the main thing I was trying in order to delete the objects was barbox1 = None, but that doesn't seem to work.

This question is related to python variables object

The answer is


What do you mean by delete? In Python, removing a reference (or a name) can be done with the del keyword, but if there are other names to the same object that object will not be deleted.

--> test = 3
--> print(test)
3
--> del test
--> print(test)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'test' is not defined

compared to:

--> test = 5
--> other is test  # check that both name refer to the exact same object
True
--> del test       # gets rid of test, but the object is still referenced by other
--> print(other)
5

object.__del__(self) is called when the instance is about to be destroyed.

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

Object is not deleted unless all of its references are removed(As quoted by ethan)

Also, From Python official doc reference:

del x doesn’t directly call x.del() — the former decrements the reference count for x by one, and the latter is only called when x‘s reference count reaches zero


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to variables

When to create variables (memory management) How to print a Groovy variable in Jenkins? What does ${} (dollar sign and curly braces) mean in a string in Javascript? How to access global variables How to initialize a variable of date type in java? How to define a variable in a Dockerfile? Why does foo = filter(...) return a <filter object>, not a list? How can I pass variable to ansible playbook in the command line? How do I use this JavaScript variable in HTML? Static vs class functions/variables in Swift classes?

Examples related to object

How to update an "array of objects" with Firestore? how to remove json object key and value.? Cast object to interface in TypeScript Angular 4 default radio button checked by default How to use Object.values with typescript? How to map an array of objects in React How to group an array of objects by key push object into array Add property to an array of objects access key and value of object using *ngFor