Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7385
| References | <mailman.42.1307600576.11593.python-list@python.org> <4df24a87$0$30002$c3e8da3$5496439d@news.astraweb.com> |
|---|---|
| Date | 2011-06-10 11:01 -0600 |
| Subject | Re: how to inherit docstrings? |
| From | Eric Snow <ericsnowcurrently@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.86.1307725303.11593.python-list@python.org> (permalink) |
On Fri, Jun 10, 2011 at 10:47 AM, Steven D'Aprano
<steve+comp.lang.python@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)?
-eric
>
> def InheritableDocstring(name, bases, dict):
> mro = None
> docstring = dict.get('__doc__')
> if docstring == '':
> # Search the MRO for the first non-empty docstring. We let Python
> # do all the hard work of calculating the MRO.
> mro = type('K', bases, {}).__mro__[1:] # Exclude the class K.
> # Also exclude object.
> assert mro[-1] == object
> mro = mro[:-1]
> for cls in mro:
> if cls.__doc__:
> docstring = cls.__doc__
> break
> else:
> docstring = None
> dict['__doc__'] = docstring
> assert dict.get('__doc__') != ''
> # Create the class we want, and return it.
> cls = type(name, bases, dict)
> if mro:
> assert cls.__mro__ == (cls,) + mro + (object,)
> return cls
>
>
>
> class A(metaclass=InheritableDocstring):
> pass
>
> class B(A, metaclass=InheritableDocstring):
> ''
>
> class C(B, metaclass=InheritableDocstring):
> 'A docstring.'
>
> class D(B, metaclass=InheritableDocstring):
> pass
>
> class E(D, C, metaclass=InheritableDocstring):
> ''
>
> class F(E, metaclass=InheritableDocstring):
> ''
>
> assert all(cls.__doc__ is None for cls in (A, B, D))
> assert all(cls.__doc__ == 'A docstring.' for cls in (C, E, F))
>
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
how to inherit docstrings? Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-09 00:22 -0600
Re: how to inherit docstrings? Ben Finney <ben+python@benfinney.id.au> - 2011-06-09 16:37 +1000
Re: how to inherit docstrings? Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-09 01:13 -0600
Re: how to inherit docstrings? Ben Finney <ben+python@benfinney.id.au> - 2011-06-09 17:44 +1000
Re: how to inherit docstrings? Duncan Booth <duncan.booth@invalid.invalid> - 2011-06-09 11:23 +0000
Re: how to inherit docstrings? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-09 14:56 +0000
Re: how to inherit docstrings? Ben Finney <ben+python@benfinney.id.au> - 2011-06-10 07:33 +1000
Re: how to inherit docstrings? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-10 09:25 +0000
Re: how to inherit docstrings? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-10 16:47 +0000
Re: how to inherit docstrings? Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-10 11:01 -0600
Re: how to inherit docstrings? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-10 17:27 +0000
Re: how to inherit docstrings? Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-10 12:48 -0600
Re: how to inherit docstrings? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-10 17:19 +0000
csiph-web