Operator Overloading
Sebastien Boisgerault
boisgera at isia.cma.fr
Thu Nov 25 09:35:23 EST 2004
Peter Maas <peter at somewhere.com> wrote in message news:<co48s1$r1f$1 at swifty.westend.com>...
> Sebastien Boisgerault schrieb:
> > I wonder if the following quotation from the Python Reference Manual
> > (release 2.3.3) about operator overloading is true :
> >
> > "For example, if a class defines a method named __getitem__(), and x
> > is an instance of this class, then x[i] is equivalent to
> > x.__getitem__(i)"
> [...]
> >>>>from Numeric import *
> >>>>a = array([0.5])
> >>>>a[0]
> >
> > 0.5
> >
> > but
> >
> >
> >>>>a.__getitem__(0)
> >
> > Traceback (most recent call last):
> > File "<stdin>", line 1, in ?
> > AttributeError: __getitem__
>
> The quotation above is true. Short form:
>
> IF __getitem__ in dict THEN [] works.
Not exactly the same assertion:
replace "__getitem__ in dict" by "__getitem__ in the class dict"
and more importantly "[] works" by "[] and __getitem__" are *equivalent*.
Here, "__getitem__" does belongs to type(a).__dict__,
so "[]" and "__getitem__" should work exactly the same
according to the reference, but they don't.
> [...]
> 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__.
I would buy your argument if I couldn't find the "__getitem__" method.
But it does exist ! Except that it is hidden is the class __dict__ and
apparently cannot be recovered from the instance.__getitem__ call ...
Thanks for your help,
SB
More information about the Python-list
mailing list