In Python's interactive interpreter, I can do thiq:
>>> help(reduce)
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.
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)
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.
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)
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()β.