using __getitem()__ correctly

Chris Angelico rosuav at gmail.com
Wed Dec 30 08:11:24 EST 2015


On Wed, Dec 30, 2015 at 11:57 PM, Charles T. Smith
<cts.private.yahoo at gmail.com> wrote:
> Hello,
>
> I thought __getitem__() was invoked when an object is postfixed with an
> expression in brackets:
>
>   - abc[n]
>
> and __getattr__() was invoked when an object is postfixed with an dot:
>
>   - abc.member

That would be normal (with the caveat that __getattr__ is called only
if the attribute isn't found; __getattribute__ is called
unconditionally).

> but my __getitem__ is being invoked at this time, where there's no
> subscript (going into a spiral recursion death):
>
>   self.mcc = self.attrs.mcc
>
> Can anybody explain to me why __getitem__()  would be invoked here?

Can you post your entire class, or at least __getattr__? Also check
superclasses, if there are any.

ChrisA



More information about the Python-list mailing list