Skip to content

Instantly share code, notes, and snippets.

@pavan538
Created February 14, 2018 10:34
Show Gist options
  • Save pavan538/a9c14d66ccd45dcfade92f7a71140280 to your computer and use it in GitHub Desktop.
Save pavan538/a9c14d66ccd45dcfade92f7a71140280 to your computer and use it in GitHub Desktop.
automatically get docstring of abstract class docstring in derived class
import abc
class SuperclassMeta(type):
def __new__(mcls, classname, bases, cls_dict):
cls = super().__new__(mcls, classname, bases, cls_dict)
for name, member in cls_dict.items():
if not getattr(member, '__doc__'):
member.__doc__ = getattr(bases[-1], name).__doc__
return cls
class Superclass(object, metaclass=SuperclassMeta):
"""The one to rule them all"""
@abc.abstractmethod
def give(self, ring):
"""Give out a ring"""
pass
class Derived(Superclass):
"""Somebody has to do the work"""
def give(self, ring):
print("I pass the ring {} to you".format(ring))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment