special_methods2.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. class Cud():
  2. def __init__(self):
  3. #print("__init__ called")
  4. pass
  5. def __repr__(self):
  6. print("__repr__ called")
  7. return ""
  8. def __lt__(self, other):
  9. print("__lt__ called")
  10. def __le__(self, other):
  11. print("__le__ called")
  12. def __eq__(self, other):
  13. print("__eq__ called")
  14. def __ne__(self, other):
  15. print("__ne__ called")
  16. def __ge__(self, other):
  17. print("__ge__ called")
  18. def __gt__(self, other):
  19. print("__gt__ called")
  20. def __abs__(self):
  21. print("__abs__ called")
  22. def __add__(self, other):
  23. print("__add__ called")
  24. def __and__(self, other):
  25. print("__and__ called")
  26. def __floordiv__(self, other):
  27. print("__floordiv__ called")
  28. def __index__(self, other):
  29. print("__index__ called")
  30. def __inv__(self):
  31. print("__inv__ called")
  32. def __invert__(self):
  33. print("__invert__ called")
  34. def __lshift__(self, val):
  35. print("__lshift__ called")
  36. def __mod__(self, val):
  37. print("__mod__ called")
  38. def __mul__(self, other):
  39. print("__mul__ called")
  40. def __matmul__(self, other):
  41. print("__matmul__ called")
  42. def __neg__(self):
  43. print("__neg__ called")
  44. def __or__(self, other):
  45. print("__or__ called")
  46. def __pos__(self):
  47. print("__pos__ called")
  48. def __pow__(self, val):
  49. print("__pow__ called")
  50. def __rshift__(self, val):
  51. print("__rshift__ called")
  52. def __sub__(self, other):
  53. print("__sub__ called")
  54. def __truediv__(self, other):
  55. print("__truediv__ called")
  56. def __div__(self, other):
  57. print("__div__ called")
  58. def __xor__(self, other):
  59. print("__xor__ called")
  60. def __iadd__(self, other):
  61. print("__iadd__ called")
  62. return self
  63. def __isub__(self, other):
  64. print("__isub__ called")
  65. return self
  66. def __dir__(self):
  67. return ['a', 'b', 'c']
  68. cud1 = Cud()
  69. cud2 = Cud()
  70. try:
  71. +cud1
  72. except TypeError:
  73. print("SKIP")
  74. raise SystemExit
  75. # the following require MICROPY_PY_ALL_SPECIAL_METHODS
  76. +cud1
  77. -cud1
  78. ~cud1
  79. cud1 * cud2
  80. cud1 / cud2
  81. cud2 // cud1
  82. cud1 += cud2
  83. cud1 -= cud2
  84. cud1 % 2
  85. cud1 ** 2
  86. cud1 | cud2
  87. cud1 & cud2
  88. cud1 ^ cud2
  89. cud1 << 1
  90. cud1 >> 1
  91. # test that dir() delegates to __dir__ special method
  92. print(dir(cud1))
  93. # test that dir() does not delegate to __dir__ for the type
  94. print('a' in dir(Cud))
  95. # TODO: the following operations are not supported on every ports
  96. #
  97. # ne is not supported, !(eq) is called instead
  98. #cud1 != cud2
  99. #
  100. # in the followin test, cpython still calls __eq__
  101. # cud3=cud1
  102. # cud3==cud1