[Tutor] Set/get doc strings of ALL attributes?

pan at uchicago.edu pan at uchicago.edu
Wed Nov 17 09:00:15 CET 2004

Hi there,

I've been wondering if there's a way to set/get doc string
for ALL elements of a class (but not just the __doc__ of

I ended writing a class BaseObject for this purpose. It stores doc 
strings of variables (that is, the <name> that is assigned to a 
class using myclass.name). When asking for all docs, doc strings 
are collected from 3 different sources:

1) property __doc__;
2) the variables mentioned above;
3) function/method __doc__;

It seems to work well. My questions:

A) Will the above approach be able to cover all elements 
   such that an automatic documentation mechinsm can be

B) Is there any other better way to do this?

C) I intend to use this class as the base class for all 
   other subclass. Will this kind of design cause any problem 
   in any class inherited from it?

Thx in advance.

Here's the code:

class BaseObject(object):
  '''An extention of class object, have the capacity of storing/retrieving
     doc string for variables. Doc strings are retrievd from 3 different
     elements: variable (name), property, and method (or function). See
     the code in getDoc() for how to retrieve them.
  def __init__(self):
     self.__docs__ ={'__docs__':'An internal dict storing doc'
     ' string of variables. Write: setattr(name, val, doc) or'
     ' setDoc(name, doc), read: getDoc(name) or getDocs().'
     ' Defined in class BaseObject.'}
  def setattr(self, name, val, doc=None):
     '''setattr(name, val, doc=None): set value and doc string 
      of a variable <name>. If doc=None, doc is not changed
      Defined in class BaseObject.''' 

     if doc!=None: self.__docs__[name] = doc
     return self
  def setDoc(self, name, doc, checkName=1):
      '''setDoc(name, doc, checkName=1): Set doc string of variable.
       Defined in class BaseObject.'''
      if checkName and (name not in self.__dict__):
         self.__docs__[name] = doc

  def getDoc(self, name):
      '''getDoc(name): get the doc string of <name>.
         Defined in BaseObject.'''
      fromProperty = lambda x: self.__class__.__dict__[x].__doc__
      fromVariable = lambda x: self.__docs__[x]
      fromMethod   = lambda x: getattr(self, x).__doc__.strip().replace('\n','')
          txt = fromProperty(name)
              txt = fromVariable(name)
                  txt = fromMethod(name)
                  txt = ''

      if txt==None or txt=='':
         return txt
        return re.sub('\s+', ' ', txt)
  def getDocs(self):
      '''getDocs(): return a dict representing the doc strings
         of all attributes(variables, properties, functions,
         methods...) of this class. Defined in BaseObject.''' 
      dic = {}
      for x in dir(self):
          if not x.startswith('_'):
             dic[x] = self.getDoc(x)
      return dic

class MyObj(BaseObject):
    def __init__(self):
        self.setattr('name', 'myobj', 'this is my obj')
    def setSize(self, size): self._size=size
    def getSize(self): return self._size
    size= property(getSize, setSize, None, 'Size of the object')

def test():
    mo = MyObj()
    x = mo.getDocs()
    print x

More information about the Tutor mailing list