Operator Overloading

Sebastien Boisgerault boisgera at isia.cma.fr
Thu Nov 25 19:13:21 CET 2004


Nick Coghlan <ncoghlan at email.com> wrote in message news:<41a5e084$0$25787$5a62ac22 at per-qv1-newsreader-01.iinet.net.au>...
> Peter Maas wrote:
> > What you are wondering about is the opposite direction
> > 
> > IF [] works THEN __getitem__ in dict.
> > 
> > but this is not what the Python Reference Manual says. Im not a
> > Numeric expert but AFAIK Numeric arrays are basically C arrays
> > having [] intrinsically so there's no need no deliver it via
> > __getitem__.
> 
> This is correct, and true of any C extension - classes implemented in C only 
> need to define the appropriate function pointers in their type structures in 
> order for Python to find the relevant methods.
> 
> Classes that are being *nice* about it put in the actual magic method names as 
> well (e.g. try "list.__getitem__"), but it is by no means required.

Nick, Pierre, Peter,

Thanks for your answers. I guess that in the case of the Numeric 
package, there was at least the *intent* to support __getitem__ 
because it is provided at the class level (which is not required, 
right ?):

>> from Numeric import *
>> a = array([3.14])
>> Array = type(a)
>> a.__getitem__(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: __getitem__
>> Array.__getitem__(a,0)
3.14

I guess that everything would work as expected if __class__ was defined at
the instance level ...

Regards,

SB



More information about the Python-list mailing list