docstrings

Mark Jackson mjackson at wc.eso.mc.xerox.com
Fri Apr 21 20:53:38 CEST 2000


Ben Wolfson <rumjuggler at home.com> writes:
> On 20 Apr 2000 16:22:49 GMT, mjackson at wc.eso.mc.xerox.com (Mark
> Jackson) wrote:
> 
> >Ben Wolfson <rumjuggler at home.com> writes:
> >> is there a way to use %c and %s within a docstring, or to add strings
> >> together in a docstring, so that if some strings are used in all
> >> docstrings, but might be changed, I can simply change one or two
> >> variables?
> >
> >This might do what you want, although it's ugly (in the sense that it
> >serves the code-documentation - as opposed to user-documentation -
> >function less well):
> >
> >    cat > spam.py
> >    """I am a docstring"""
> >    
> >    sharedoc = """ and I am a widely-reused docstring clause"""
> >    
> >    __doc__ = __doc__ + sharedoc
> >    
> >    del sharedoc
> >    ^D
> >    yngvi> python
> >    Python 1.5.2 (#7, May  6 1999, 14:39:45)  [GCC 2.8.1] on sunos5
> >    Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >    >>> import spam
> >    >>> print spam.__doc__
> >    I am a docstring and I am a widely-reused docstring clause
> >    >>> 
> 
> This approach doesn't work for function docstrings, unless I tried it
> wrong.

Did I say my previous method was ugly?  On reflection I misspoke;
*this* is ugly:

<spam.py>

"""I am the main module docstring"""

sharedoc = """ but I am a widely-reused docstring clause"""

__doc__ = __doc__ + sharedoc

def eggs():
	"""I am eggs"""

	return 'yolk'

def bacon():
	"""I am bacon"""

	return 'grease'

def and_spam():
	"""I am and_spam"""

	return 'various parts'

for i in eggs, bacon, and_spam:
		i.__doc__ = i.__doc__ + sharedoc

<example>

>>> import spam
>>> spam.__doc__
'I am the main module docstring but I am a widely-reused docstring clause'
>>> spam.eggs.__doc__
'I am eggs but I am a widely-reused docstring clause'
>>> spam.bacon.__doc__
'I am bacon but I am a widely-reused docstring clause'
>>> spam.and_spam.__doc__
'I am and_spam but I am a widely-reused docstring clause'

-- 
Mark Jackson - http://www.alumni.caltech.edu/~mjackson
	An advertiser will happily make you feel bad about
	yourself if that will make you buy, say, a Bic pen.
				- George Meyer





More information about the Python-list mailing list