Inspect live objects in Python


Functions in this module provide usefule information about live objects such as modules, classes, methods, functions, code objects etc. These functions perform type checking, retrieve source code, inspect classes and functions, and examine the interpreter stack.

getmembers()− This function returns all the members of an object in a list of name, value pairs sorted by name. If the optional predicate is supplied, only members for which the predicate returns a true value are included. getmodulename() −This function returns the name of the module named by the file path, without including the names of enclosing packages

We shall be using following script for understanding the behaviour of inspect module.

#inspect-example.py
'''This is module docstring'''
def hello():
   '''hello docstring'''
   print ('Hello world')
   return
#class definitions
class parent:
   '''parent docstring'''
   def __init__(self):
      self.var='hello'
   def hello(self):
      print (self.var)
class child(parent):
   def hello(self):
      '''hello function overridden'''
      super().hello()
      print ("How are you?")

starts with '__'

>>> import inspect, inspect_example
>>> for k,v in inspect.getmembers(inspect_example):
      if k.startswith('__')==False:print (k,v)
child
hello
parent
>>>

Predicates

Predicate is a logical condition applied to functions in inspect module. For example getmembers() function returns list of module's members for which given predicate condition is true. Following predicates are defined in inspect module

ismodule()Return true if the object is a module.
isclass()Return true if the object is a class, whether built-in or created in Python code.
ismethod()Return true if the object is a bound method written in Python.
isfunction()Return true if the object is a Python function, which includes functions created by a lambda expression.
isgenerator()Return true if the object is a generator.
iscode()Return true if the object is a code.
isbuiltin()Return true if the object is a built-in function or a bound built-in method.
isabstract()Return true if the object is an abstract base class.

Here only class members in the module will be returned.

>>> for k,v in inspect.getmembers(inspect_example, inspect.isclass):
      print (k,v)
child <class 'inspect_example.child'>
parent <class 'inspect_example.parent'>
>>>

To retrieve members of a specified class 'child' −

>>> inspect.getmembers(inspect_example.child)
>>> x=inspect_example.child()
>>> inspect.getmembers(x)

The getdoc() function retrieves docstring of a module, class or function.

>>> inspect.getdoc(inspect_example)
'This is module docstring'
>>> inspect.getdoc(inspect_example.parent)
'parent docstring'
>>> inspect.getdoc(inspect_example.hello)
'hello docstring'

The getsource() function fetches the definition code of a function −

>>> print (inspect.getsource(inspect_example.hello))
def hello():
   '''hello docstring'''
   print ('Hello world')
   return
>>> sign=inspect.signature(inspect_example.parent.hello)
>>> print (sign)

The inspect module also has a command line interface.

C:\Users\acer>python -m inspect -d inspect_example
Target: inspect_example
Origin: C:\python36\inspect_example.py
Cached: C:\python36\__pycache__\inspect_example.cpython-36.pyc
Loader: <_frozen_importlib_external.SourceFileLoader object at
0x0000029827BD0D30>

Following command returns source code of 'Hello()' function in the module.

C:\Users\acer>python -m inspect inspect_example:hello
def hello():
   '''hello docstring'''
   print ('Hello world')
   return

Updated on: 30-Jul-2019

481 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements