using __getitem()__ correctly

Charles T. Smith cts.private.yahoo at gmail.com
Wed Dec 30 07:57:51 EST 2015


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

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?

I'm using __getitem__() AND __getattr__() to handle an array of objects 
which don't exist yet (autovivification).  My __getattr__() could handle 
the line above, but I don't know how to handle that in __getitem__():

  attrdict:av:__getitem__: entered for  mcc

That's printed by this line:

  print "attrdict:av:__getitem__: entered for ", key


I expected that "key" would always be numeric or a slice

TIA
cts



More information about the Python-list mailing list