Introspection in Python

In this part of the Python tutorial, we talk about introspection.

Introspection is an act of self examination. In computer programming, introspection is the ability to determine type or properties of objects at runtime. Python programming language has a large support of introspection. Everything in Python is an object. Every object in Python may have attributes and methods. By using introspection, we can dynamically inspect Python objects.

The dir() function

The dir() function returns a sorted list of attributes and methods belonging to an object.

>>> dir(())
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', 
'__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', 
'__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', 
'__str__', '__subclasshook__', 'count', 'index']

Here we see an output of the dir() function for a tuple object.

>>> print(().__doc__)
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items

If the argument is a tuple, the return value is the same object.

Our investigation showed that there is a __doc__ attribute for a tuple object.

direx.py
#!/usr/bin/python3

# direx.py

import sys

class MyObject(object):

   def __init__(self):
      pass

   def examine(self):
      print(self)


o = MyObject()

print(dir(o))
print(dir([]))
print(dir({}))
print(dir(1))
print(dir())
print(dir(len))
print(dir(sys))
print(dir("String"))

The example examines several objects using the dir() function: a user defined object, native data types, a function, a string, or a number.

Without any argument, dir() returns names in the current scope.

>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>>> import sys
>>>import math, os
>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'math', 'sys']

We execute the dir() function before and after we include some modules.

The type() function

The type() function returns the type of an object.

typefun.py
#!/usr/bin/python3

# typefun.py

import sys

def function(): 
    pass

class MyObject(object):
    
   def __init__(self):
      pass

o = MyObject()

print(type(1))
print(type(""))
print(type([]))
print(type({}))
print(type(()))
print(type(object))
print(type(function))
print(type(MyObject))
print(type(o))
print(type(sys))

The example print various types of objects to the console screen.

$ ./typefun.py 
<class 'int'>
<class 'str'>
<class 'list'>
<class 'dict'>
<class 'tuple'>
<class 'type'>
<class 'function'>
<class 'type'>
<class '__main__.MyObject'>
<class 'module'>

This is the output.

The id() function

The id() returns a special id of an object.

idfun.py
#!/usr/bin/python3

# idfun.py

import sys

def fun(): pass

class MyObject(object):
    
   def __init__(self):
      pass

o = MyObject()

print(id(1))
print(id(""))
print(id({}))
print(id([]))
print(id(sys))
print(id(fun))
print(id(MyObject))
print(id(o))
print(id(object))

The code example prints ids of various objects, both built-in and custom.

$ ./idfun.py 
10914368
139696088742576
139696087935944
139696065155784
139696088325640
139696088244296
21503992
139696087910776
10738720

The sys module

The sys module provides access to system specific variables and functions used or maintained by the interpreter and to functions that interact strongly with the interpreter. The module allows us to query about the Python environment.

>>> import sys
>>> sys.version
'3.5.2 (default, Nov 17 2016, 17:05:23) \n[GCC 5.4.0 20160609]'
>>> sys.platform
'linux'
>>> sys.path
['', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', 
'/usr/lib/python3.5/lib-dynload', '/home/janbodnar/.local/lib/python3.5/site-packages', 
'/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']

In the above code we examine the Python version, platform, and search path locations.

We can also use the dir() function to get a full list of variables and functions of the sys module.

>>> sys.executable
'/usr/bin/python3'
>>> sys.argv
['']
>>> sys.byteorder
'little'

The example presents executable, argv, and byteorder attributes of the sys module.

>>> sys.executable
'/usr/bin/python3'

The executable is a string giving the name of the executable binary for the Python interpreter, on systems where this makes sense.

>>> sys.argv
['']

This gives a list of command line arguments passed to a Python script.

>>> sys.byteorder
'little'

The byteorder is an indicator of the native byte order. This will have the value 'big' on big-endian (most-significant byte first) platforms, and 'little' on little-endian (least-significant byte first) platforms.

Various

Next we show various other ways of inspecting Python objects.

attrs.py
#!/usr/bin/python3

# attr.py

def fun(): 
    pass

print(hasattr(object, '__doc__'))
print(hasattr(fun, '__doc__'))
print(hasattr(fun, '__call__'))

print(getattr(object, '__doc__'))
print(getattr(fun, '__doc__'))

The hasattr() function checks if an object has an attribute. The getattr() function returns the contents of an attribute if there are some.

$ ./attr.py 
True
True
True
The most base type
None

The isinstance function checks if an objects is an instance of a specific class.

>>> print(isinstance.__doc__)
Return whether an object is an instance of a class or of a subclass thereof.

A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
or ...`` etc.

We can get the describtion of a function interactively.

instance.py
#!/usr/bin/python3

# instance.py

class MyObject(object):
    
   def __init__(self):
      pass

o = MyObject()

print(isinstance(o, MyObject))
print(isinstance(o, object))
print(isinstance(2, int))
print(isinstance('str', str))

As we know, everything is an object in Python; even numbers and strings. The object is a base type of all objects in Python.

$ ./instance.py 
True
True
True
True

The issubclass() function checks if a specific class is a derived class of another class.

subclass.py
#!/usr/bin/python3

# subclass.py

class Object(object):
    
   def __init__(self):
      pass

class Wall(Object):
    
   def __init__(self):
      pass

print(issubclass(Object, Object))
print(issubclass(Object, Wall))
print(issubclass(Wall, Object))
print(issubclass(Wall, Wall))

In our code example, the Wall class is a subclass of the Object class. Object and Wall are also subclasses of themselves. The Object class is not a subclass of class Wall.

$ ./subclass.py 
True
False
True
True

The __doc__ attribute gives some documentation about an object and the __name__ attribute holds the name of the object.

namedoc.py
#!/usr/bin/python3

# namedoc.py

def noaction():
   '''A function, which does nothing'''
   pass

funcs = [noaction, len, str]

for i in funcs:
    
   print(i.__name__)
   print(i.__doc__)
   print("-" * 75)

In our example, we create a list of three functions: one custom and two native. We go through the list and print the __name__ and the __doc__ attributes.

$ ./namedoc.py
noaction
A function, which does nothing
---------------------------------------------------------------------------
len
Return the number of items in a container.
---------------------------------------------------------------------------
str
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.
---------------------------------------------------------------------------

This is the output.

Finally, there is also a callable() function. The function checks if an object is a callable object (a function).

callable.py
#!/usr/bin/python3

# callable.py

class Car(object):
      
    def setName(self, name):
        self.name = name    

def fun():
    pass

c = Car()    
    
print(callable(fun))
print(callable(c.setName))
print(callable([]))
print(callable(1))

In the code example we check if three objects are callables.

print(callable(fun))
print(callable(c.setName))

The fun() function and the setName() method are callables. (A method is a function bound to an object.)

$ ./callable.py
True
True
False
False

In this part of the Python tutorial, we have talked about introspection in Python. More tools for doing introspection can be found in the inspect module.