Inheriting doc strings. What are the options?

Chad Netzer cnetzer at mail.arc.nasa.gov
Fri May 2 17:59:24 EDT 2003


I'd like derived classes to inherit the docstrings of their base
classes, unless overridden.  Or at least, a simple way to do it
explicitly (perhaps using metaclasses?)

This seems like the kind of thing that would have been discussed before,
and I was wondering if anyone could point me to a discussion, or provide
ideas on why this is or isn't the case in python already.  I'd consider
implementing a patch if it is deemed a widely useful feature.

It would be useful for me because I like to build Abstract classes that
define an API, and having the derived (implementation) classes and
method inherit the doc strings is very natural.  Other class hiearchies
may benefit less.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
class Abstract:
    """An Abstract class with doc strings."""

    def api_method( self ):
        """A method that is implemented in subclasses."""
        raise NotImplementedError

# This class will not inherit the Abstact doc strings
class Concrete( Abstract ):
    def api_method( self ):
        print "api_method of Concrete class called."

# I would like the subclasses and objects constructed from them to
# inherit the doc strings, unless they are overridden.
print Concrete.__doc__
print Concrete.api_method.__doc__

print

c = Concrete()
print c.__doc__
print c.api_method.__doc__


-- 

Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)






More information about the Python-list mailing list