borg.py 672 B

12345678910111213141516171819202122232425262728293031323334353637
  1. class Borg:
  2. __shared_state = {}
  3. def __init__(self):
  4. self.__dict__ = self.__shared_state
  5. def __str__(self):
  6. return self.state
  7. class YourBorg(Borg):
  8. pass
  9. if __name__ == '__main__':
  10. rm1 = Borg()
  11. rm2 = Borg()
  12. rm1.state = 'Idle'
  13. rm2.state = 'Running'
  14. print('rm1: {0}'.format(rm1))
  15. print('rm2: {0}'.format(rm2))
  16. rm2.state = 'Zombie'
  17. print('rm1: {0}'.format(rm1))
  18. print('rm2: {0}'.format(rm2))
  19. print('rm1 id: {0}'.format(id(rm1)))
  20. print('rm2 id: {0}'.format(id(rm2)))
  21. rm3 = YourBorg()
  22. print('rm1: {0}'.format(rm1))
  23. print('rm2: {0}'.format(rm2))
  24. print('rm3: {0}'.format(rm3))