HOW TO USING IPYTHON TAB COMPLETION FOR DISCOVERY IN PYTHON PROGRAMMING

USING IPYTHON TAB COMPLETION FOR DISCOVERY

You can view a module’s documentation in IPython interactive mode via tab completion—a discovery feature that speeds your coding and discovery processes. After you type a portion of an identifier and press Tab, IPython completes the identifier for you or provides a list of identifiers that begin with what you’ve typed so far. This may vary based on your operating system platform and what you have imported into your IPython session:
view code image

In [1]: import math
In [2]: ma<Tab>
          map %macro %%markdown
          math %magic %matplotlib
          max() %man

You can scroll through the identifiers with the up and down arrow keys. As you do, IPython highlights an identifier and shows it to the right of the In [] prompt.

Viewing Identifiers in a Module

To view a list of identifiers defined in a module, type the module’s name and a dot (.), then press Tab:
view code image

In [3]: math.<Tab>
   acos()    atan()   copysign()    e expm1()
   acosh() atan2() cos() erf()      fabs()
   asin()    atanh() cosh() erfc() factorial() >
   asinh()  ceil()     degrees()      exp() floor()

If there are more identifiers to display than are currently shown, IPython displays the > symbol (on some platforms) at the right edge, in this case to the right of factorial(). You can use the up and down arrow keys to scroll through the list. In the list of identifiers:

  • Those followed by parentheses are functions (or methods, as you’ll see later).
  • Single-­word identifiers (such as Employee) that begin with an uppercase letter and multi-word identifiers in which each word begins with an uppercase letter (such as Commission-Employee) represent class names (there are none in the preceding list). This naming convention, which the Style Guide for Python Code recommends, is known as CamelCase because the uppercase letters stand out like a camel’s humps.
  • Lowercase identifiers without parentheses, such as pi (not shown in the preceding list) and e, are variables. The identifier pi evaluates to 3.141592653589793, and the identifier e evaluates to 2.718281828459045. In the math module, pi and e represent the mathematical constants Ď€ and e, respectively.

Python does not have constants, although many objects in Python are immutable (nonmodifiable). So even though pi and e are real­world constants, you must not assign new values to them, because that would change their values. To help distinguish constants from other variables, the style guide recommends naming your custom constants with all capital letters.

Using the Currently Highlighted Function

As you navigate through the identifiers, if you wish to use a currently highlighted function, simply start typing its arguments in parentheses. IPython then hides the auto-completion list. If you need more information about the currently highlighted item, you can view its docstring by typing a question mark (?) following the name and pressing Enter to view the help documentation. The following shows the fabs function’s docstring:
view code image

In [4]: math.fabs?
Docstring:
fabs(x)
Return the absolute value of the float x.
Type: builtin_function_or_method

The builtin_function_or_method shown above indicates that fabs is part of a Python Standard Library module. Such modules are considered to be built into Python. In this case, fabs is a built­in function from the math module.

FREE HACKING COURSE LAUNCHED

*

Post a Comment (0)
Previous Post Next Post