__mul__(), __div__()...

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
fma
Posts: 164
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

__mul__(), __div__()...

Post by fma » Fri Sep 05, 2014 7:08 am

Hi!

I'm trying to use a matrix implementation written in python, but it does not work; it seems that __mul__() and __div__() are not implemented.

Can someone confirm?
Frédéric

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: __mul__(), __div__()...

Post by pythoncoder » Sat Sep 06, 2014 9:57 am

Firstly __div__() isn't in Python 3.4 because of the changes made to the meaning of the division operator. See
https://docs.python.org/3.4/library/operator.html

However it does seem that __mul__ and __truediv__ aren't implemented, although __add__ and __sub__ are. I suggest you flag it up on github and hopefully these operators will be added. I tested with this class which works as expected in cPython, but multiplication and division fail in Micropython.

Code: Select all

class foo(object):
    def __init__(self, val):
        self.bar = val
    def __mul__(self, other):
        return self.bar * other.bar *2
    def __truediv__(self, other):
        return (self.bar /other.bar) *2
    def __add__(self, other):
        return (self.bar + other.bar) *2
    def __sub__(self, other):
        return (self.bar - other.bar) *2
In Micropython:

Code: Select all

>>> a = test4.foo(9.0)
>>> b = test4.foo(4.0)
>>> a+b
26
>>> a*b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand types for binary operator: 'foo', 'foo'
Regards, Pete
Peter Hinch
Index to my micropython libraries.

fma
Posts: 164
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: __mul__(), __div__()...

Post by fma » Sat Sep 06, 2014 6:56 pm

Thanks! I will report it on github...
Frédéric

Post Reply