2023-05-12 13:17:20 +00:00
|
|
|
# Test reverse operators.
|
2017-09-10 14:05:31 +00:00
|
|
|
|
2023-05-12 13:17:20 +00:00
|
|
|
# Test user type with integers.
|
|
|
|
class A:
|
2017-09-10 14:05:31 +00:00
|
|
|
def __init__(self, v):
|
|
|
|
self.v = v
|
|
|
|
|
|
|
|
def __add__(self, o):
|
|
|
|
if isinstance(o, A):
|
|
|
|
return A(self.v + o.v)
|
|
|
|
return A(self.v + o)
|
|
|
|
|
|
|
|
def __radd__(self, o):
|
|
|
|
return A(self.v + o)
|
|
|
|
|
|
|
|
def __repr__(self):
|
2019-10-18 08:18:06 +00:00
|
|
|
return "A({})".format(self.v)
|
2017-09-10 14:05:31 +00:00
|
|
|
|
2023-05-12 13:17:20 +00:00
|
|
|
|
2017-09-10 14:05:31 +00:00
|
|
|
print(A(3) + 1)
|
|
|
|
print(2 + A(5))
|
2023-05-12 13:17:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Test user type with strings.
|
|
|
|
class B:
|
|
|
|
def __init__(self, v):
|
|
|
|
self.v = v
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "B({})".format(self.v)
|
|
|
|
|
|
|
|
def __ror__(self, o):
|
|
|
|
return B(o + "|" + self.v)
|
|
|
|
|
|
|
|
def __radd__(self, o):
|
|
|
|
return B(o + "+" + self.v)
|
|
|
|
|
|
|
|
def __rmul__(self, o):
|
|
|
|
return B(o + "*" + self.v)
|
|
|
|
|
|
|
|
def __rtruediv__(self, o):
|
|
|
|
return B(o + "/" + self.v)
|
|
|
|
|
|
|
|
|
|
|
|
print("a" | B("b"))
|
|
|
|
print("a" + B("b"))
|
|
|
|
print("a" * B("b"))
|
|
|
|
print("a" / B("b"))
|
2023-05-12 13:16:37 +00:00
|
|
|
|
|
|
|
x = "a"; x |= B("b"); print(x)
|
|
|
|
x = "a"; x += B("b"); print(x)
|
|
|
|
x = "a"; x *= B("b"); print(x)
|
|
|
|
x = "a"; x /= B("b"); print(x)
|