I'm learning about Classes and this thing is driving me crazy. Whenever I run this, it will use the del object even though I never used del droid1, del droid2, or del droid3. Can someone please help?
class Robots:
'''Represents a robot, with a name.'''
#A class variable, counting the number of robots
population = 0
def __init__(self, name):
'''Initializes the data'''
self.name = name
print("(Initializing {0})".format(self.name))
#When this person is created, the robot adds to the population
Robots.population += 1
def __del__(self):
'''I am dying'''
print("{0} is being destoryed!".format(self.name))
Robot.population -= 1
if Robot.population == 0:
print("{0} was the last one.".format(self.name))
else:
print("There are still {0:d} robots working".format(Robot.population))
def sayHi(self):
'''Greeting by the robot
yeah, they can do that.'''
print("Greetings, my masters call me {0}".format(self.name))
def howMany():
'''Prints the current population'''
print("We have {0:d} robots.".format(Robots.population))
howMany = staticmethod(howMany)
droid1 = Robots("r2-d2")
droid2 = Robots("c-3po")
droid3 = Robots("SAL")
droid1.sayHi()
droid2.sayHi()
droid3.sayHi()