how to inherit docstrings?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Fri Jun 10 13:27:31 EDT 2011
On Fri, 10 Jun 2011 11:01:41 -0600, Eric Snow wrote:
> On Fri, Jun 10, 2011 at 10:47 AM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> Here's some Python 3 code that uses a factory function as a metaclass
>> to inherit docstrings. Give the class a docstring of an empty string,
>> and it will be inherited from the first superclass found with a
>> non-empty docstring.
>>
>>
>>
> Yeah, the idea of an empty docstring to trigger docstring inheritance
> really appeals to me. Nice example. Incidently, aren't metaclasses
> always inherited, as opposed to class decorators (which are never)?
Metaclasses are inherited, but the example I give uses a factory function
as a metaclass: it manipulates the docstring inside the dict, then
returns an ordinary class. That makes it just a fancy class decorator
using metaclass syntax.
The type of each class A, B, ... F is just type, which means that when
you subclass each class you don't get any magic metaclass behaviour
unless you explicitly set the metaclass directly.
That is:
assert type(A) is type
succeeds, so class B(A) doesn't do anything special unless you explicitly
set the metaclass.
I followed up with a second example using a conventional metaclass, that
is, where the type of each class is *not* type. In that case, the magic
behaviour is inherited and there's no need to explicitly set the
metaclass except for the first time.
(Whew. Talking about metaclasses is hard work.)
--
Steven
More information about the Python-list
mailing list