Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > __mul__ vs __rmul__ (and others) priority for different classes

Reply
Thread Tools

__mul__ vs __rmul__ (and others) priority for different classes

 
 
dmitrey
Guest
Posts: n/a
 
      12-11-2009
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.
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      12-11-2009
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is __mul__ sufficient for operator '*'? Muhammad Alkarouri Python 7 10-26-2009 12:02 AM
Should I use shutter-priority or appurature-priority? ½ Confused Digital Photography 4 02-22-2006 09:48 AM
Using '__mul__' within a class Gerard Flanagan Python 12 09-25-2005 02:28 PM
Question about Aperture priority and Shutter Priority John Edwards Digital Photography 8 01-05-2005 04:58 PM
Using __mul__ Thomas Philips Python 2 07-03-2004 03:28 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57