if(widgetStylesPrinted != true) {document.write('');}var widgetStylesPrinted = true;var content = '
objvar_v2.py
python
  1. class Robot:
  2. '''Represents a robot, with a name.'''
  3.  
  4. population = 0
  5.  
  6. def __init__(self,name):
  7. '''Initializes the data.'''
  8. self.name = name
  9. print('(Initializing{0})'.format(self.name))
  10.  
  11. Robot.population += 1
  12.  
  13. def __del__(self):
  14. '''I am dying.'''
  15. print('{0} is being destroyed!'.format(self.name))
  16.  
  17. Robot.population -= 1
  18.  
  19. if Robot.population == 0:
  20. print('{0} was the last one.'.format(self.name))
  21. else:
  22. print('There are still {0:d} robots working'.format(Robot.population))
  23.  
  24. def sayHi(self):
  25. '''Greeting by the robot
  26. Yeah, they can do that.'''
  27. print('Greetings, my masters call me {0}'.format(self.name))
  28.  
  29. def howMany(klass):
  30. '''Prints the current population.'''
  31. print('We have {0:d} robots'.format(Robot.population))
  32. howMany = staticmethod(howMany)
  33.  
  34. droid1 = Robot('R2-D2')
  35. droid1.sayHi()
  36. Robot.howMany()
  37.  
  38. droid2 = Robot('C-3P0')
  39. droid2.sayHi()
  40. Robot.howMany()
  41.  
  42. print("\\nRobots can do some work here.\\n")
  43.  
  44. print("Robots have finished their work. So let's destory them.")
  45.  
  46. del droid1
  47. del droid2
  48.  
  49. Robot.howMany()
';document.write(content);