__getitem__ method on (meta)classes

Bengt Richter bokr at oz.net
Tue Mar 15 15:14:40 EST 2005


On Tue, 15 Mar 2005 08:32:51 -0800, Ron Garret <rNOSPAMon at flownet.com> wrote:

>In article <423684d8.821657701 at news.oz.net>,
> bokr at oz.net (Bengt Richter) wrote:
>
>> >Did you mean type(x).__getitem__(x,y)?
>> >
>> Not if x is a classmethod,
D'oh. I meant "not if __getitem__ is a classmethod" ;-P

>
>Oh yeah, right.  Duh!
>
>> >And where is this documented?
>> Between the lines in my previous post ;-)
>
>I see.  I guess I wasn't asking a stupid question then :-)
>
Seriously, no ;-) It's hard to find good documentation on deep nitty-gritties.
The most reliable documentation of actual software behavior is inevitably
the code that implements it, though that often doesn't give a clue as to the
whys of the whats and hows shown.

As Raymond Hettinger says in his nice doc (which chew slowly for best nutrition ;-)
    http://users.rcn.com/python/download/Descriptor.htm
"""
For objects, the machinery is in object.__getattribute__ which transforms
b.x into type(b).__dict__['x'].__get__(b, type(b)).
The implementation works through a precedence chain that gives data descriptors
priority over instance variables, instance variables priority over non-data descriptors,
and assigns lowest priority to __getattr__ if provided.
The full C implementation can be found in PyObject_GenericGetAttr() in Objects/object.c.
"""

The code does show what really happens ;-)
(UIAM, for types the analogous stuff is in Objects/typeobject.c)


Most documentation is probably reachable via
    http://www.python.org/
and
    http://www.python.org/doc/

but sometimes specific stuff is hard to find. Descriptors are
discussed in various links of

    http://www.python.org/doc/newstyle.html

and IMO Raymond's

    http://users.rcn.com/python/download/Descriptor.htm

is the most readable.

If you google for descrintro you will find a lot of discussion ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list