duplicate docstrings

S

Steven Bethard

I have two classes that implement the same interface, e.g. something like:

class C(object):
def foo(self):
"""Foo things"""
...
def bar(self):
"""Bar things"""
...
def baz(self):
"""Baz things in a C manner"""
...

class D(object):
def foo(self):
"""Foo things"""
...
def bar(self):
"""Bar things"""
...
def baz(self):
"""Baz things in a D manner"""
...

It bothers me that I'm basically writing the same docstrings multiple
times. I guess what I really want to do is just write the docstrings
for the interface I'm describing, and only supply docstrings in the
classes when they need to differ from the interface docstrings.

Is there a good way to do this? If it's necessary, I can have C and D
inherit from another class...

STeVe
 
B

Brian van den Broek

Steven Bethard said unto the world upon 2005-02-18 13:58:
I have two classes that implement the same interface, e.g. something like:

class C(object):
def foo(self):
"""Foo things"""
...
def bar(self):
"""Bar things"""
...
def baz(self):
"""Baz things in a C manner"""
...

class D(object):
def foo(self):
"""Foo things"""
...
def bar(self):
"""Bar things"""
...
def baz(self):
"""Baz things in a D manner"""
...

It bothers me that I'm basically writing the same docstrings multiple
times. I guess what I really want to do is just write the docstrings
for the interface I'm describing, and only supply docstrings in the
classes when they need to differ from the interface docstrings.

Is there a good way to do this? If it's necessary, I can have C and D
inherit from another class...

STeVe

Hi, I'm new to thinking in classes, and may not have understood your
need. But does this help:

IDLE 1.1 '''I'm a very spammy docstring'''
pass
__doc__ = Spam.__doc__

Help on class Ham in module __main__:

class Ham
| I'm a very spammy docstring

best,

Brian vdB
 
F

Felix Wiemann

Steven said:
class C(object):
def foo(self):
"""Foo things"""
...

class D(object):
def foo(self):
"""Foo things"""
...

It bothers me that I'm basically writing the same docstrings multiple
times. I guess what I really want to do is just write the docstrings
for the interface I'm describing, and only supply docstrings in the
classes when they need to differ from the interface docstrings.

Is there a good way to do this? If it's necessary, I can have C and D
inherit from another class...

Use a common interface type and a metaclass which copies method
docstrings from base classes:

------------------------------------------------------------------------------
#!/usr/bin/env python

import inspect


class DocstringMetaclass(type):

"""Copy method docstrings."""

def __init__(cls, *args):
super(DocstringMetaclass, cls).__init__(*args)
for name, method in cls.__dict__.iteritems():
# method is a function, not a method, so we use isfunction().
if not inspect.isfunction(method) or method.__doc__ is not None:
continue
for c in cls.mro():
if hasattr(c, name):
m = getattr(c, name)
if inspect.ismethod(m) and m.__doc__ is not None:
method.__doc__ = m.__doc__
break


class Interface(object):

__metaclass__ = DocstringMetaclass

def foo(self):
"""Foo things"""

def bar(self):
"""Bar things"""

def baz(self):
"""Baz things in a C manner"""


class Implementation(Interface):

def foo(self):
pass

def bar(self):
pass

def baz(self):
pass


print Implementation.foo.__doc__
print Implementation.bar.__doc__
print Implementation.baz.__doc__
------------------------------------------------------------------------------

Output:

Foo things
Bar things
Baz things in a C manner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top