DEV Community

Python's `help()` in other languages.

In Python's interactive interpreter, I can do thiq:

>>> help(reduce)
Enter fullscreen mode Exit fullscreen mode

And it'll pop up a help page like this:

Help on built-in function reduce in module __builtin__:

reduce(...)
    reduce(function, sequence[, initial]) -> value

    Apply a function of two arguments cumulatively to the items of a sequence,
    from left to right, so as to reduce the sequence to a single value.
    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
    ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
    of the sequence in the calculation, and serves as a default when the
    sequence is empty.
Enter fullscreen mode Exit fullscreen mode

I can do this for non built-in functions as well. A triple-quoted string at the start of the function body defines a "docstring".

>>> def handshakes(n):
...     """Returns the number o handshakess that occur in a group of n people if everyone shakes everyone's hand."""
...     return (n*(n-1))/2
... 
>>> help(handshakes)
Enter fullscreen mode Exit fullscreen mode

I get this help page:

Help on function handshakes in module __main__:

handshakes(n)
    Returns the number o handshakes that occur in a group of n people if everyone shakes everyone's hand.
Enter fullscreen mode Exit fullscreen mode

This makes it very easy to interactively explore new libraries. I often find myself wishing more languages had this feature, especially JavaScript or exploring web APIs.

Thoughts?

Top comments (1)

Collapse
 
ikemkrueger profile image
Ikem Krueger

I liked that too on Python, that I can explore the language on my own. What I learned recently is, that Pythons β€ždir()β€œ is Javascripts β€žconsole.dir()β€œ.