IPython - Dynamic Object Introspection



IPython has different ways of obtaining information about Python objects dynamically. In this chapter, let us learn the ways of dynamic object introspection in IPython.

Use of ? and ?? provides specific and more detailed information about the object.

Example - Getting Information of an Integer Object

In the first example discussed below, a simple integer object a is created. Its information can be procured by typing a ? in the input cell.

In [27]: a = 10

In [28]: a?
Type:        int
String form: 10
Docstring:
int([x]) -> integer
int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is a number, return x.__int__().  For floating-point
numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base.  The literal can be preceded by '+' or '-' and be surrounded
by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4

In [29]:

Example - Getting Information of a Function Object

In the second example, let us define a function and introspect this function object with ? and ??.

In [30]: def hello():
    ...:     print("Hello")
    ...:

In [31]: hello?
Signature: hello()
Docstring: <no docstring>
File:      d:\projects\python\myenv\<ipython-input-30-1dfef71f1e97>
Type:      function

Note that the magic function %psearch is equivalent to the use of ? or ?? for fetching object information.

Advertisements