fork download
  1. def find_if(f, col):
  2. x = filter(f, col)
  3. return col.index(x[0])
  4.  
  5. class Symbol(object):
  6. strlist = {}
  7. def __init__(self, data):
  8. ix = find_if(lambda x: x[0] == str(data), strlist)
  9. if ix >= -1:
  10. st, count = strlist[ix]
  11. strlist[ix][1] += 1
  12. self.__ix = ix
  13. else:
  14. self.__ix = len(strlist)
  15. strlist.append([str(data), 1])
  16. def __del__(self):
  17. st, count = strlist[self.__ix]
  18. strlist[self.__ix][1] += 1
  19. if strlist[self.__ix][1] == 0:
  20. del strlist[self.__ix]
  21. def __str__(self):
  22. return 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(a, id(str(a)))
  42. print(b, id(str(b)))
  43. print('----------------')
  44.  
Runtime error #stdin #stdout 0.02s 5908KB
stdin
Standard input is empty
stdout
Standard output is empty