[Python-ideas] Is there a good reason to use * for multiplication?

Yuval Greenfield ubershmekel at gmail.com
Sat Oct 13 11:15:10 CEST 2012


On Sat, Oct 13, 2012 at 10:18 AM, Steven D'Aprano <steve at pearwood.info>wrote:

> [..]
> but I think that puts the emphasis on the wrong thing. If (and that's a big
> if) we did something like this, it should be a pair of methods __op__ and
> the right-hand version __rop__ which get called on the *operands*, not the
> operator/function object:
>
> def __op__(self, other, symbol)
>
>
I thought the operator should have a say in how it operates, e.g. the
operater `dot` could call __dot__ in its operands.

class Vector:
    def _dot(self, other):
        return sum([i * j for i, j in zip(self, other)])

class dot(operator):
    def __infix__(self, left, right):
        return left._dot(left, right)

>>>Vector([1,2,3]) dot Vector([3,4,5])
26



Making the declaration and import of operators more explicit than the `def
__op__(self, other, symbol)` version. We could put [/, *, ., //, etc...] in
__builtins__

Yuval
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20121013/e2537cb0/attachment.html>


More information about the Python-ideas mailing list