[Python-Dev] Re: Hot-To Guide for Descriptors - Nits! :-)

François Pinard pinard at iro.umontreal.ca
Wed Jan 21 20:55:43 EST 2004

[Raymond Hettinger]

> Clarified.  [...] Fixed.  [...] (etc.)

Thanks, Raymond, for all those.

> This is because the machinery for descriptors is embedded in
> type.__getattribute__ and object.__getattribute__.  Override or fail
> to inherit either of these and all bets are off.

I thinks I understand better what you are saying.  Maybe part of my
problem is that I'm not sure how `__getattribute__' is itself fetched.
While I know you do not want to speak about metatypes, it might be
that whenever you write `type.__getattribute__', you really mean the
`__getattribute__' found in the dict of the metatype (or is it?);
while when you write `object.__getattribute__', you really mean the
`__getattribute__' found by scanning the base classes of the current
class, and `object' always when there is no base class for a "new-style"
class.  These are sensible hypotheses, yet I'm not sure, of course.

Maybe that failing to say the full truth, _if_ this is the case, might
raise questions more than calm them! :-)

> > * In `Descriptor Protocol', it might help a bit saying immediately a
> > word or two about the `type=None' argument.  Looking down for some
> > hint, the box holding "def __getattribute__(self, key): ..." uses
> > the statement "return v.__get__(None, self)", which I find confusing
> > respective to the `__get__' prototype, as `obj' corresponds to None,
> > and `type=None' likely corresponds to an instance instead of a type.

> I'll keep an open mind on this one but think it is probably fine as it
> stands.  Though the calling pattern may not be beautiful and could be
> confusing to some, my description accurately represents the signature
> and cases where are argument is potentially ignored.  That is just the
> way it is.

Just adding your own quote above might help the How-To.  About:

    descr.__get__(self, obj, type=None) --> value

I really think it would help readers like me saying that the pattern is
not beautiful, that `obj' may be None, and that `type' may be something
else than a type, and this is the way it is.  It would invite the reader
to swallow the snake, instead of fighting it.

> > * In `Invoking Descriptors', it might help a bit insisting on the
> > (presumed) fact that `hasattr' itself does not look for descriptors.

> I see no reason to delve into what hasattr() doesn't do.

One has to memorise a few rules about the order in which the lookups
are done for finding an attribute, and unless `hasattr' is more used as
pseudo-code than Python code, my fear is that `hasattr' does not look at
the same place, nor in the same order, as the few rules above.  Maybe
there is no danger of any confusion, maybe there is, I surely wondered.

> > Second is that the argument `objtype=None' to `__get__' is unused,
> > and once more, this does not help understanding its purpose, maybe
> > some explanation would be welcome about why it exists and is
> > unneeded in the context of `property'.

> That's the way it is.

I would not doubt it, but my suggestion was that the How-To could offer
more help for readers to understand or find some logic in the way it
is, enough to get a grasp and assimilate the notion.  If there is
logic to present, merely saying that it should be assimilated without
understanding might help readers like me at not stumbling on the matter
for too long.

I hope you read my criticism or requests as constructive...

François Pinard   http://www.iro.umontreal.ca/~pinard

More information about the Python-Dev mailing list