fork download
  1. import contextlib
  2.  
  3. class Pituh(object):
  4. def __enter__(self):
  5. return "pithu"
  6.  
  7. def __exit__(self, exc_type, exc_val, exc_tb):
  8. print("exit")
  9.  
  10. def kok(self):
  11. print("kok")
  12.  
  13. class Spam(contextlib.AbstractContextManager, list):
  14. def __exit__(self, exc_type, exc_value, traceback):
  15. for i in self:
  16. i.__exit__(exc_type, exc_value, traceback)
  17.  
  18.  
  19. spam = Spam()
  20. spam.append(Pituh())
  21. spam.append(Pituh())
  22.  
  23. with spam as s:
  24. kok = s[0]
  25. print(type(kok))
  26. kok.kok()
Success #stdin #stdout 0.02s 9180KB
stdin
Standard input is empty
stdout
<class '__main__.Pituh'>
kok
exit
exit