borg.py 910 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. class Borg:
  4. __shared_state = {}
  5. def __init__(self):
  6. self.__dict__ = self.__shared_state
  7. self.state = 'Init'
  8. def __str__(self):
  9. return self.state
  10. class YourBorg(Borg):
  11. pass
  12. if __name__ == '__main__':
  13. rm1 = Borg()
  14. rm2 = Borg()
  15. rm1.state = 'Idle'
  16. rm2.state = 'Running'
  17. print('rm1: {0}'.format(rm1))
  18. print('rm2: {0}'.format(rm2))
  19. rm2.state = 'Zombie'
  20. print('rm1: {0}'.format(rm1))
  21. print('rm2: {0}'.format(rm2))
  22. print('rm1 id: {0}'.format(id(rm1)))
  23. print('rm2 id: {0}'.format(id(rm2)))
  24. rm3 = YourBorg()
  25. print('rm1: {0}'.format(rm1))
  26. print('rm2: {0}'.format(rm2))
  27. print('rm3: {0}'.format(rm3))
  28. ### OUTPUT ###
  29. # rm1: Running
  30. # rm2: Running
  31. # rm1: Zombie
  32. # rm2: Zombie
  33. # rm1 id: 140732837899224
  34. # rm2 id: 140732837899296
  35. # rm1: Init
  36. # rm2: Init
  37. # rm3: Init