borg.py 720 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. def __str__(self):
  8. return self.state
  9. class YourBorg(Borg):
  10. pass
  11. if __name__ == '__main__':
  12. rm1 = Borg()
  13. rm2 = Borg()
  14. rm1.state = 'Idle'
  15. rm2.state = 'Running'
  16. print('rm1: {0}'.format(rm1))
  17. print('rm2: {0}'.format(rm2))
  18. rm2.state = 'Zombie'
  19. print('rm1: {0}'.format(rm1))
  20. print('rm2: {0}'.format(rm2))
  21. print('rm1 id: {0}'.format(id(rm1)))
  22. print('rm2 id: {0}'.format(id(rm2)))
  23. rm3 = YourBorg()
  24. print('rm1: {0}'.format(rm1))
  25. print('rm2: {0}'.format(rm2))
  26. print('rm3: {0}'.format(rm3))