fork download
  1. import random
  2.  
  3. random.seed(12345)
  4.  
  5. class Texs():
  6. ''' Create x objects and keep track of the collection within the class '''
  7.  
  8. all_my_xs_set = set()
  9. all_my_xs_list = []
  10. nxs = 0
  11.  
  12. @classmethod
  13. def add_x(cls):
  14. ''' Create a new x and add it to the collection '''
  15. x = cls()
  16. cls.all_my_xs_set.add(x)
  17. cls.nxs += 1
  18. cls.all_my_xs_list.append(x)
  19.  
  20. def __init__(self):
  21. self.i = Texs.nxs
  22.  
  23. for i in range(10):
  24. Texs.add_x()
  25.  
  26. x = random.sample(Texs.all_my_xs_set, 1)[0]
  27. y = random.sample(Texs.all_my_xs_list, 1)[0]
  28.  
  29. print(x.i, y.i)
Success #stdin #stdout 0.01s 36944KB
stdin
Standard input is empty
stdout
1 0