Code: Select all
class foo:
def __init__(self, data):
self.data = data
def __mul__(self, other):
if type(other) in (int, float):
return foo(self.data * other)
else:
return foo(self.data * other.data)
def __rmul__(self, other):
if type(other) in (int, float):
return foo(self.data * other)
else:
return foo(self.data * other.data)
Code: Select all
if __name__ == '__main__':
f1 = foo(10)
f2 = foo(20)
(f1*f2).data # 200
(f1*50).data # 500
(50*f1).data # it does not work on micropython
Code: Select all
>>> (50*f1).data
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported types for __mul__: 'int', 'foo'
Does anyone know, how to solve it.