fork download
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3.  
  4. class MyInt(int):
  5. def __add__(self, other):
  6. if isinstance(other, int):
  7. return int(self) + int(other)
  8. elif isinstance(other, str):
  9. try:
  10. return int(self) + int(other, 0)
  11. except ValueError:
  12. return str(self) + str(other)
  13. else:
  14. raise TypeError('Unhandled case: {} + {}'.format(
  15. type(self).__name__, type(other).__name__))
  16.  
  17. x = MyInt(3)
  18. print(x + 'how')
  19. print(x + '3')
  20. print('JavaScript, мааам!')
  21.  
Success #stdin #stdout 0.02s 9376KB
stdin
Standard input is empty
stdout
3how
6
JavaScript, мааам!