__getitem__ method on (meta)classes

Ron Garret rNOSPAMon at flownet.com
Tue Mar 15 07:00:38 CET 2005


In article <39n1jpF60pc7pU1 at individual.net>,
 Leif K-Brooks <eurleif at ecritters.biz> wrote:

> ron at flownet.com wrote:
> > Why doesn't this work?
> > 
> > 
> >>>>def foo(lst):
> > 
> > ...   class baz(object):
> > ...     def __getitem__(cls, idx): return cls.lst[idx]
> > ...     __getitem__=classmethod(__getitem__)
> > ...   baz.lst = lst
> > ...   return baz
> > ...
> > 
> > I thought x[y] and x.__getitem__(y) were supposed to always be
> > synonymous.
> 
> No, with new-style classes, x[y] and type(x).__getitem__(y) are 
> synonymous.

Ah.

Did you mean type(x).__getitem__(x,y)?

And where is this documented?

rg



More information about the Python-list mailing list