[Python-Dev] RE: Hot-To Guide for Descriptors - Nits! :-)
Raymond Hettinger
python at rcn.com
Wed Jan 21 16:48:02 EST 2004
> > I'm with Raymond. Metaclasses that don't derive from 'type' can
> > create objects that are neither fish nor flesh.
>
> After thinking this through some more, I have to retract that. After
> all, even classic classes and their instances are derive from object:
>
> >>> class C: pass
>
> >>> isinstance(C, object)
> True
> >>>
> >>> isinstance(C(), object)
> True
Hmm, that surprises me. After all, writing class C(object): pass
makes it new-style. Also dir(C()) or dir(C) does not show any of
the methods given by dir(object) or dir(object()).
> What makes a classic class is one very specific metaclass. What makes
> a classic instance is a class using that very specific metaclass.
> Everything else is a new-style class.
While true, I think it more pragmatic to have the docs and terminology
*not* expressed in terms of meta-classes; rather, it is simpler to
focus on the distinction between class C and class C(object), noting
that
all the new gizmos only work with the latter.
The theory is simple, one shouldn't have to understand meta-classes
in order be able to use property, classmethod, staticmethod, super,
__slots__, __getattribute__, descriptors, etc.
Practically, in all code that doesn't explicitly create meta-classes,
the sole trigger between new and old is adding object, list, or
other builtin type as a base class.
IOW, I think the docs ought to continue using wording like this for
property: "Return a property attribute for new-style classes (classes
that derive from object)."
There would be much loss of clarity and understanding with wording like:
"Return a property attribute for a class whose metaclass is type or that
implements the class semantics of type.__getattribute__ and the instance
semantics of object.__getattribute__."
For purposes of my article on descriptors, I'll continue with the
current approach of ignoring meta-classes. There is enough information
presented that meta-class writers will quickly understand why all the
new-style gizmos stopped working when they didn't carry forward the
semantics of object.__getattribute__ and type.__getattribute__.
I think all of the topics are best understood in terms of three groups:
classic classes, new-style classes, and roll your own classes. Lumping
the latter two together won't help one bit.
'nuff said,
Raymond Hettinger
#################################################################
#################################################################
#################################################################
#####
#####
#####
#################################################################
#################################################################
#################################################################
More information about the Python-Dev
mailing list