__getitem__ method on (meta)classes

Michele Simionato michele.simionato at gmail.com
Mon Mar 14 22:52:18 EST 2005


Leif Brooks:
>with new-style classes, x[y] and type(x).__getitem__(y) are
>synonymous.

Yes, but check the discussion around SF789262. The call ``x[y]`` is
converted to  ``type(x).__getitem__(x,y)``
*only if* ``__getitem__`` is explicitely defined in ``type(x)``. If
``type(x)`` does not define ``__getitem__`` directly, but only
indirectly via delegation (i.e. overriding ``__getattribute__``),
then the second form (i.e. ``type(x).__getitem__(x,y)``) works but
not the first one (i.e. ``x[y]`` raises an error).

                       Michele Simionato




More information about the Python-list mailing list