Thu 30 Nov 2006
Viewing Python function documentation with __doc__
Posted by dkaz under Python
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.”"”

November 30th, 2006 at 11:16 pm
The help() function reads a symbol’s __doc__ string and does formatting for you.
December 3rd, 2006 at 12:59 pm
nice…that’s even better
December 3rd, 2006 at 1:05 pm
>>> help(string.split) Help on function split in module string: split(s, sep=None, maxsplit=-1) split(s [,sep [,maxsplit]]) -> list of strings Return a list of the words in the string s, using sep as the delimiter string. If maxsplit is given, splits at no more than maxsplit places (resulting in at most maxsplit+1 words). If sep is not specified or is None, any whitespace string is a separator. (split and splitfields are synonymous)December 3rd, 2006 at 1:09 pm
I do find the “append” calling-style a little more convenient; it just feels more object-oriented.
I already hate pre-pending “len(” in front of strings and appending “)” at the end of them…especially in the interpreter…Ruby definitly shows superiority in these instances