0

I was wondering if I could delete an object from inside its own function. Here's what I'm thinking:

class ball:
    def __init__(self, a):
        self.a = a

    def update(self, t):
        if t >= self.a:
            del self # Need help here

myBall = ball(120)

for i in range(0, 1000):
    myBall.update(i)
quamrana
  • 37,849
  • 12
  • 53
  • 71
Xander
  • 31
  • 5
  • 3
    http://stackoverflow.com/questions/293431/python-object-deleting-itself – Iluvatar Dec 07 '16 at 15:33
  • 1
    What do you expect to happen when the loop gets to `myBall.update(121)`? – quamrana Dec 07 '16 at 15:42
  • Why would you even want to do this? – poke Dec 07 '16 at 15:44
  • @poke wouldn't it save memory if at a certain point where an object is no longer needed, I delete it. (This code was just for example's sake) – Xander Dec 07 '16 at 16:33
  • But how can an object itself determine if it’s still needed by something else? It can only look at itself, so there’s no way to have that knowledge about other things. – poke Dec 07 '16 at 16:36

1 Answers1

0

del self is just deleting the reference, not the actual object.

self is a reference to your object.

omri_saadon
  • 10,193
  • 7
  • 33
  • 58