Earlier this morning, I was looking for a way to look up Python function definitions inside of the interpreter.

My work buddy (that would be you, gfast) came to the rescue by pointing out the (very handy) __doc__ built-in attribute, which returns Docstring of the function it’s attached to.

Sweet!

It’s always annoyed me that Javadoc is not compiled into the *.class files - it’s nice to see that Guido didn’t make the same mistake with Python.

>>> dir(string)
[’__builtins__’, ‘__doc__’, ‘__file__’, ‘__name__’, ‘_float’, ‘_idmap’, ‘_idmapL’, ‘_int’, ‘_long’, ‘ascii_letters’, ‘as
cii_lowercase’, ‘ascii_uppercase’, ‘atof’, ‘atof_error’, ‘atoi’, ‘atoi_error’, ‘atol’, ‘atol_error’, ‘capitalize’, ‘capw
ords’, ‘center’, ‘count’, ‘digits’, ‘expandtabs’, ‘find’, ‘hexdigits’, ‘index’, ‘index_error’, ‘join’, ‘joinfields’, ‘le
tters’, ‘ljust’, ‘lower’, ‘lowercase’, ‘lstrip’, ‘maketrans’, ‘octdigits’, ‘printable’, ‘punctuation’, ‘replace’, ‘rfind
‘, ‘rindex’, ‘rjust’, ‘rstrip’, ’split’, ’splitfields’, ’strip’, ’swapcase’, ‘translate’, ‘upper’, ‘uppercase’, ‘whitesp
ace’, ‘zfill’]

>>> string.upper.__doc__
‘upper(s) -> string\n\n Return a copy of the string s converted to uppercase.\n\n ‘

To enable __doc__ support in custom methods, you need to surround comments with “”"triple double quotes”"” and place them at the top of the methods.

def function(a, b):
“”"Do some funky shit and return a list.”"”