All functions from self and inherited classes

Christer Østergaard winterdk at hotmail.com
Wed Jun 13 07:40:22 EDT 2001


Hi again!

I've tried both of your suggestions and can conclude the following for both:
When i use inspect.getmembers([something]) or megadir([something too]) from
"outside" a class, it works great. I get all methods from a certain class
and it's ancestors.

But, if I include the functions in GeneralClass.doc(), and try calling
b.doc() i get the following:
1. the inspect only gives me methods for class B... not inherited functions
(which I want! :-).
2. megadir gives me nothing, since it relies on "__bases__" (which is emtpy
when called from WITHIN a class method...)

Because of my urge to crack this one, i hereby send you the code for
GeneralClass (called Component):

As you can see, _getDocList() only iterates over list from
dir(self.__class__) ... it is this part, that i want to reengineer to a
recurcive method, collecting all methods from self and inherited classes. Go
to #LOOK HERE :-)

Looking forward to your responses... :-)

[CODE SNIPPET BEGIN]
"""
NAME:
SYNOPSIS:
NOTES:
SEEALSO:
STORYCARD:
"""

class Component:
    """
    NAME:
    SYNOPSIS:
    NOTES:
    EXAMPLE:
    BUGS:
    SEEALSO:
    INTERNALS:
    """

    def __init__(self, attr={}, req={}, debug=0):
        self.attrList = attr
        self.reqList = req
        self.debug = debug

    def doc(self):
        """
        NAME: doc()
        SYNOPSIS:
        FUNCTION: To write docstring for a given class and all it's methods.
        INPUTS: self
        RESULT: Text output
        NOTES:
        EXAMPLE: t.doc()
        BUGS: None reported
        SEEALSO:
        INTERNALS:
        TASKCARD:
        """

        #Write header
        print "Documentation for class \"" + str(getattr(getattr(self,
"__class__"), "__name__")).upper() + "\""
        #print self

        #Write class docstring
        if self.__doc__ != None:
            print self.__doc__
        else:
            self._noDoc("class \"" + getattr(getattr(self, "__class__"),
"__name__") +"\"")
        self._getDocList()

    def _getDocList(self):
        """
        NAME: _getDocList()
        SYNOPSIS:
        FUNCTION: To write methods and their docstring
        INPUTS: self
        RESULT: Text output
        NOTES:
        EXAMPLE: self._getDocList()
        BUGS: None reported
        SEEALSO:
        INTERNALS:
        TASKCARD:
        """
        #LOOK HERE
        #Iterate over functions of current class
        for m in dir(self.__class__):
            #If function is not "private"
            if m[0]!="_":
                print "FUNCTION: " + str(m)
                #if anything in m.__doc__, print it
                if getattr(getattr(self, m), "__doc__") != None:
                    print getattr(getattr(self, m), "__doc__")
                else:
                    self._noDoc("method \"" + getattr(getattr(self, m),
"__name__") +"\"")

    def _noDoc(self, aString):
        """
        NAME: _get()
        SYNOPSIS:
        FUNCTION: To write a "no doc" string
        INPUTS:
        self
        aString: string containing class or method name
        RESULT: Text output
        NOTES:
        EXAMPLE: self._noDoc()
        BUGS: None reported
        SEEALSO:
        INTERNALS:
        TASKCARD:
        """

        #print no doc string
        print "No documentation for " + str(aString)


class tk(Component):
    def tst(self):
        pass

def test():
    o = Component()
    o.doc()

if __name__ == '__main__' :
    test()

[CODE SNIPPET END]

Christer






More information about the Python-list mailing list