Created
April 24, 2012 22:45
-
-
Save wickman/2484405 to your computer and use it in GitHub Desktop.
class decorator to inherit documentation from abstract base class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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