fork download
  1. def find_if(f, col):
  2. x = list(filter(f, col))
  3. return col.index(x[0])
  4.  
  5. class Symbol(object):
  6. strlist = []
  7. def __init__(self, data):
  8. try:
  9. ix = find_if(lambda x: x[0] == str(data), type(self).strlist)
  10. st, count = type(self).strlist[ix]
  11. type(self).strlist[ix][1] += 1
  12. self.__ix = ix
  13. except IndexError:
  14. self.__ix = len(type(self).strlist)
  15. type(self).strlist.append([str(data), 1])
  16. def __del__(self):
  17. st, count = type(self).strlist[self.__ix]
  18. type(self).strlist[self.__ix][1] += 1
  19. if type(self).strlist[self.__ix][1] == 0:
  20. del type(self).strlist[self.__ix]
  21. def __str__(self):
  22. return type(self).strlist[self.__ix][0]
  23. def __repr__(self):
  24. return "{0} <{1}>".format(str(self), id(str(self)))
  25. def __eq__(self, other):
  26. return str(self) == str(other)
  27. def __ne__(self, other):
  28. return str(self) != str(other)
  29. def __lt__(self, other):
  30. return str(self) < str(other)
  31. def __le__(self, other):
  32. return str(self) <= str(other)
  33.  
  34. a = Symbol('lol')
  35. b = Symbol('wut')
  36. print(a, id(str(a)))
  37. print(b, id(str(b)))
  38. print('----------------')
  39. x = Symbol('lol')
  40. y = Symbol('wut')
  41. print(x, id(str(x)))
  42. print(y, id(str(y)))
  43. print('----------------')
  44.  
Success #stdin #stdout 0.03s 5908KB
stdin
Standard input is empty
stdout
lol 3074194208
wut 3074194144
----------------
lol 3074194208
wut 3074194144
----------------