[Python-ideas] inheriting docstrings

Eric Snow ericsnowcurrently at gmail.com
Fri Jun 10 21:08:08 CEST 2011


On Fri, Jun 10, 2011 at 12:40 PM, Eric Snow <ericsnowcurrently at gmail.com> wrote:
> Right now you could do something like this:
>
>    def get_basedoc(mro):
>        return next(c.__doc__ for c in mro[1:] if c.__doc__) or None
>
>    class Meta(type):
>        __basedoc__ = property(lambda cls: get_basedoc(cls.__mro__))
>
> But then instances don't get __basedoc__, since the metaclass is not
> in the MRO.  To get it on instances you could do this:
>
>    class C:
>        __basedoc__ = property(lambda self: get_basedoc(self.__class__.__mro__))
>
> But then getting that attribute on the class will give you the
> property object and not the docstring.
>

> I'm not sure of a way to resolve that.

Duh, someone just pointed out that you use your own descriptor instead
of a property:

class Basedoc:
    def __get__(self, obj, cls):
        return next(c.__doc__ for c in cls.__mro__[1:] if c.__doc__) or None

class C:
    __basedoc__ = Basedoc()

Inherit from the class or add it onto the class with a class decorator
or metaclass.

-eric



More information about the Python-ideas mailing list