Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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(barbox5.id, 413, 367)     bar = 0     time.sleep(0.2)     barbox1 = None     barbox2 = 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.

like image 518
user2992997 Avatar asked Feb 02 '14 18:02

user2992997


People also ask

How do you destroy an instance in Python?

Use the del keyword to delete class instance in Python. It's delete references to the instance, and once they are all gone, the object is reclaimed.

How do you dispose an object in Python?

You cannot manually destroy objects in Python, Python uses automatic memory management. When an object is no longer referenced, it is free to be garbage collected, in CPython, which uses reference counting, when a reference count reaches zero, an object is reclaimed immediately.

What does __ del __ do in Python?

__del__ is a destructor method which is called as soon as all references of the object are deleted i.e when an object is garbage collected. Example: Here is the simple example of destructor. By using del keyword we deleted the all references of object 'obj', therefore destructor invoked automatically.

Do I need to delete objects in Python?

1) Yes, I would recommend deleting the object. This will keep your code from getting bulky and/or slow. This is an especially good decision if you have a long run-time for your code, even though Python is pretty good about garbage collection.


2 Answers

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

like image 64
gitesh.tyagi Avatar answered Sep 29 '22 09:09

gitesh.tyagi


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 
like image 41
Ethan Furman Avatar answered Sep 29 '22 11:09

Ethan Furman