8000 overload operator __rmul__ does not work · Issue #2328 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content
overload operator __rmul__ does not work #2328
Closed
@bolorkhuu

Description

@bolorkhuu

i'm trying to test the overload operator __rmul__ on micropython.

class foo:
    def __init__(self, data):
        self.data = data
    def __mul__(self, other):
        print("mul")
        if type(other) in (int, float):
            return foo(self.data * other)
        else:
            return foo(self.data * other.data)
    def __rmul__(self, other):
        print("rmul")
        if type(other) in (int, float):
            return foo(self.data * other)
        else:
            return foo(self.data * other.data)

if __name__ == '__main__':
    f1 = foo(10)
    f2 = foo(20)

on python


In [1]: (f1*f2).data
mul
Out[1]: 200

In [2]: (f1*50).data
mul
Out[2]: 500

In [3]: (50*f1).data
rmul
Out[3]: 500


Unfortunately it fails on micropython

>>> (f1*f2).data                                                                
mul                                                                             
200                                                                             
>>> (f1*50).data                                                                
mul                                                                             
500                                                                             
>>> (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?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0