Getting help()

But how can we find out what other functions are available in the math module?

The REPL has a special function help() which can retrieve any embedded documentation from objects for which documentation has been provided, such as standard library modules.

To get help, simply type help at the prompt:

>>> help
Type help() for interactive help, or help(object) for help about object.

We'll leave you to explore the first form — for interactive help — in your own time. Here we'll go for the second option and pass the math module as the object for which we want help:

>>> help(math)
Help on module math:

NAME
math

MODULE REFERENCE
http://docs.python.org/3.3/library/math

The following documentation is automatically generated from the
Python
source files. It may be incomplete, incorrect or include
features that
are considered implementation detail and may vary
between Python
implementations. When in doubt, consult the module
reference at the
location listed above.

DESCRIPTION
This module is always available. It provides access to the
mathematical functions defined by the C standard.

FUNCTIONS
acos(...)
acos(x)

Return the arc cosine (measured in radians) of x.

You can use the space-bar to page through the help, and if you're on Mac or Linux use the arrow keys to scroll up and down.

Browsing through the functions, you'll can see that there's a math function, factorial, for computing factorials. Press Q to exit the help browser, and return us to the Python REPL.

Now practice using help() to request specific help on the factorial function:

>>> help(math.factorial)
Help on built-in function factorial in module math:

factorial(...)
factorial(x) -> Integral

Find x!. Raise a ValueError if x is negative or non-integral.

Press Q to return to the REPL.

Let's use factorial() a bit. The function accepts an integer argument and return an integer value:

>>> math.factorial(5)
120
>>> math.factorial(6)
720

Notice how we need to qualify the function name with the module namespace. This is generally good practice, as it makes it abundantly clear where the function is coming from. That said, it can result in code that is excessively verbose.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset