Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@wickman
Created April 24, 2012 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wickman/2484405 to your computer and use it in GitHub Desktop.
Save wickman/2484405 to your computer and use it in GitHub Desktop.
class decorator to inherit documentation from abstract base class
from abc import ABCMeta, abstractmethod
def documented_by(documenting_abc):
def document(cls):
cls_dict = cls.__dict__.copy()
for attr in documenting_abc.__abstractmethods__:
cls_dict[attr].__doc__ = getattr(documenting_abc, attr).__doc__
return type(cls.__name__, cls.__mro__[1:], cls_dict)
return document
# Example
class StoreInterface(object):
__metaclass__ = ABCMeta
@abstractmethod
def get(self, key):
"""Gets the value associated with :key."""
@abstractmethod
def set(self, key, value):
"""Associates :value with :key."""
@documented_by(StoreInterface)
class Store(StoreInterface):
def __init__(self):
self.__values = {}
def get(self, key):
return self.__values[key]
def set(self, key, value):
self.__values[key] = value
help(Store)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment