Search code examples
pythonobjectdoc

I can print object.__doc__ but I can't save it to a file


As mentioned in the subject line for a given objec (module or class)

print (object.__doc__)   works
document.write (object.__doc__)
TypeError" write: () argument must be a str not None

I do get type(object.__doc__) is None

Update: Grrr...indeed the first object in the list had not doc string Now the error mutated to TypeError" must be a str not type


Solution

  • Assuming that by object you don't mean the built-in object class but rather your own class.

    For __doc__ to not be None, the class in question has to have a docstring.

    Contrast:

    >>> class X(object):
    ...    pass
    ... 
    >>> type(X.__doc__)
    <type 'NoneType'>
    

    with:

    >>> class Y(object):
    ...    "Class Y"
    ... 
    >>> type(Y.__doc__)
    <type 'str'>