Code to add docstrings to classes

Matthew Wilson matt at tplus1.com
Tue Sep 12 10:53:28 EDT 2006


On Tue 12 Sep 2006 10:06:27 AM EDT, Neil Cerutti wrote:
> Writing a thin wrapper around the dictionary might be beneficial,
> and would also furnish a place for the docstrings. 

I wrote a function that hopefully does just that.  I'm not very savvy at
doing this class-factory stuff, so any advice would be welcome.

def vd(C):

    """

    Return a subclass of class C that has a instance-level attribute _vardoc.

    """

    class VDC(C):

        def __init__(self, *args, **kwargs):

            vardoc = kwargs.get('vardoc')
            if vardoc:
                assert isinstance(vardoc, str), "vardoc must be a
string!"
                kwargs.pop('vardoc')
            self._vardoc = vardoc

            C.__init__(self, *args, **kwargs)

        def __repr__(self):

            if self._vardoc:
                return self._vardoc + "\n" + C.__repr__(self)

            else:
                return C.__repr__(self)

    return VDC


def test_vd():

    i = vd(int)(6)
    i._vardoc = "integer!"
    assert isinstance(i, int)

    d = vd(dict)(a=1, b=2, c=i, vardoc="dict!")
    assert d['a'] == 1
    assert d['c'] == 6
    assert isinstance(d, dict)



-- 
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilsonwiki/SasAndMakefiles



More information about the Python-list mailing list