Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7304

Re: how to inherit docstrings?

Date 2011-06-09 09:10 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: how to inherit docstrings?
References <BANLkTin3FGoxwvH2VDOb4OhAWi2Ea3K-pA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.54.1307635040.11593.python-list@python.org> (permalink)

Show all headers | View raw


Eric Snow wrote:
> p.s. Am I missing something or can you really not change the docstring
> of a class?  I was thinking about the idea of inheriting class
> docstrings too.

8<--------------------------------------------------------
"""module level docstring"""

def func():
     """function level docstring"""

class Test(object):
     """class level docstring"""
     def meth(self):
         """method level docstring"""


if __name__ == '__main__':
     import sys
     import traceback
     hmmm = (
         sys.modules['__main__'],
         func,
         Test(),
         Test().meth,
         Test,
         Test.meth,
         )
     for obj in hmmm:
         try:
             obj.__doc__ = 'new docstring'
             print('successfully changed %s\n' % obj)
         except:
             traceback.print_exc()
             print()
8<--------------------------------------------------------

Tested from 2.5 - 3.2.  The first three always work, the last one works 
in 3.1+, the fourth and fifth always fail.

-----------------actual output for 2.5--------------------
successfully changed <module '__main__' from 'docstring.py'>

successfully changed <function func at 0x00A8F570>

successfully changed <__main__.Test object at 0x00A94230>

Traceback (most recent call last):
   File "docstring.py", line 25, in <module>
     obj.__doc__ = 'new docstring'
AttributeError: attribute '__doc__' of 'instancemethod' objects is not 
writable
()
Traceback (most recent call last):
   File "docstring.py", line 25, in <module>
     obj.__doc__ = 'new docstring'
AttributeError: attribute '__doc__' of 'type' objects is not writable
()
Traceback (most recent call last):
   File "docstring.py", line 25, in <module>
     obj.__doc__ = 'new docstring'
AttributeError: attribute '__doc__' of 'instancemethod' objects is not 
writable
()
-----------------actual output for 3.2--------------------
successfully changed <module '__main__' from 'docstring.py'>

successfully changed <function func at 0x00BE6F60>

successfully changed <__main__.Test object at 0x00BFE730>

Traceback (most recent call last):
   File "docstring.py", line 25, in <module>
     obj.__doc__ = 'new docstring'
AttributeError: attribute '__doc__' of 'method' objects is not writable

Traceback (most recent call last):
   File "docstring.py", line 25, in <module>
     obj.__doc__ = 'new docstring'
AttributeError: attribute '__doc__' of 'type' objects is not writable

successfully changed <function meth at 0x00BE6ED0>
-----------------actual output----------------------------

~Ethan~

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: how to inherit docstrings? Ethan Furman <ethan@stoneleaf.us> - 2011-06-09 09:10 -0700
  Re: how to inherit docstrings? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-06-10 10:27 +1200
    Re: how to inherit docstrings? Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-09 17:16 -0600
    Re: how to inherit docstrings? Ben Finney <ben+python@benfinney.id.au> - 2011-06-10 09:23 +1000
      Re: how to inherit docstrings? Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-09 17:31 -0600

csiph-web