__mul__ vs __rmul__ (and others) priority for different classes

Discussion in 'Python' started by dmitrey, Dec 11, 2009.

  1. dmitrey

    dmitrey Guest

    hi all,
    I have created a class MyClass and defined methods like __add__,
    __mul__, __pow__, __radd__, __rmul__ etc.
    Also, they are defined to work with numbers, Python lists and
    numpy.arrays.

    Both Python lists and numpy arrays have their own methods __add__,
    __mul__, __pow__, __radd__, __rmul__ etc.
    If I involve myPythonList * myClassInstance it returns just result of
    my __rmul__; but when I invoke nuumpy_arr*myClassInstance it returns
    another numpy array with elements [nuumpy_arr[0]*myClassInstance,
    nuumpy_arr[1]*myClassInstance, ...].

    Can I somehow prevent numpy array of using it __mul__ etc methods?
    (and use only my __rmul__, __radd__, etc instead)?

    Thank you in advance, D.
    dmitrey, Dec 11, 2009
    #1
    1. Advertising

  2. dmitrey

    Terry Reedy Guest

    dmitrey wrote:
    > hi all,
    > I have created a class MyClass and defined methods like __add__,
    > __mul__, __pow__, __radd__, __rmul__ etc.
    > Also, they are defined to work with numbers, Python lists and
    > numpy.arrays.
    >
    > Both Python lists and numpy arrays have their own methods __add__,
    > __mul__, __pow__, __radd__, __rmul__ etc.
    > If I involve myPythonList * myClassInstance it returns just result of
    > my __rmul__; but when I invoke nuumpy_arr*myClassInstance it returns
    > another numpy array with elements [nuumpy_arr[0]*myClassInstance,
    > nuumpy_arr[1]*myClassInstance, ...].
    >
    > Can I somehow prevent numpy array of using it __mul__ etc methods?
    > (and use only my __rmul__, __radd__, etc instead)?


    No. you have to put your class instance first to get priority.
    Given a op b, python first calls a.__op__(b) and only if that fails,
    which it will for most builtin objects when b is MyClass, b.__rop__(a).
    Numpy arrays, however, are more broadminded about what they will work with.

    If your operations are not commutative, you will either have to wrap
    numpy arrays in a class that disables the special methods or use
    explicit function calls.

    Terry Jan Reedy
    Terry Reedy, Dec 11, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Philips

    Using __mul__

    Thomas Philips, Jul 3, 2004, in forum: Python
    Replies:
    2
    Views:
    381
    Peter Otten
    Jul 3, 2004
  2. Gerard Flanagan

    Using '__mul__' within a class

    Gerard Flanagan, Sep 24, 2005, in forum: Python
    Replies:
    12
    Views:
    707
    John J. Lee
    Sep 25, 2005
  3. Marcel Müller
    Replies:
    3
    Views:
    559
    Marcel Müller
    Apr 27, 2009
  4. Muhammad Alkarouri

    Is __mul__ sufficient for operator '*'?

    Muhammad Alkarouri, Oct 20, 2009, in forum: Python
    Replies:
    7
    Views:
    422
    Mick Krippendorf
    Oct 26, 2009
  5. Travis
    Replies:
    3
    Views:
    356
Loading...

Share This Page