t554.py 754 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from time import sleep
  2. class A:
  3. def __getitem__(self, item):
  4. if isinstance(item, str):
  5. print "item is a string, as it should be"
  6. print "Getting " + item
  7. return 42
  8. def __setitem__(self, item, value):
  9. if isinstance(item, str):
  10. print "attr is a string, as it should be"
  11. print "Intercepted attempt to set " + item + " to " + str(value)
  12. a = A()
  13. a["x"] = 0
  14. print "a[\"x\"] = " + str(a["x"])
  15. class B:
  16. def __getitem__(self, item):
  17. print "Getting " + item
  18. sleep(0.01)
  19. return 42
  20. def __setitem__(self, item, value):
  21. print "Intercepted attempt to set " + item + " to " + str(value)
  22. sleep(0.01)
  23. b = B()
  24. b["x"] = 0
  25. print "b[\"x\"] = " + str(b["x"])
  26. b["x"] += 1
  27. print "b[\"x\"] = " + str(b["x"])