Python Code Introspection

What is Code Introspection?

The ability to determine the properties of objects at runtime is referred to as introspection.

In Python, every object can have attributes and methods. We can dynamically examine Python objects using introspection. Code Introspection is used to examine classes, methods, objects, modules, and keywords in order to obtain information about them so that we can use it. Introspection reveals useful information about the objects in your program.

Python, as a dynamic, object-oriented programming language, supports extensive introspection. Python’s support for introspection is extensive and widespread.

In Python, there are numerous built-in methods for introspection. They are:

  • type()
  • help()
  • dir()
  • id()
  • str()
  • hasattr()
  • repr()
  • callable()
  • issubclass()
  • isinstance()
  • __doc__
  • __name__

Some of these are explained below

Code Introspection in Python

1)type()

The type of object passed as a parameter is returned by this type() function.

# Import sys module using the import keyword
import sys
# Printing the type of given sys module using the type() function
print(type(sys))

# Printing the type of given number using the type() function
gvn_num = 80.45
print(type(gvn_num))

# Printing the type of given tuple using the type() function
gvn_tuple = (9, 7, 3)
print(type(gvn_tuple))

Output:

<class 'module'>
<class 'float'>
<class 'tuple'>

2)help()

The help() function is used to return the documentation for an object. It contains information about what functions do in the program.

# Get the information about the list using the help() function
help(list)

Output:

Help on class list in module builtins:

class list(object)
| list(iterable=(), /)
| 
| Built-in mutable sequence.
| 
| If no argument is given, the constructor creates a new empty list.
| The argument must be an iterable if specified.
| 
| Methods defined here:
| 
| __add__(self, value, /)
| Return self+value.
|   ........................................
...................................................
...................................

| Static methods defined here:
| 
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
| 
| ----------------------------------------------------------------------
| Data and other attributes defined here:
| 
| __hash__ = None

3)dir()

The dir() is a built-in function of python that returns a list of attributes and methods for an object.

# Printing all the attributes and methods of the given list using the dir() function
gvn_lst = [8, 3, 6]

print(dir(gvn_lst))

Output:

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

4)id()

The id() is a built-in function of python that returns the id of an object. During the lifetime of a Program, this id is unique for each object.

gvn_num = 50
# Printing the id of given number using the id() function
print("Id of given number = ", id(gvn_num))

gvn_lst = [8, 3, 6]
# Printing the id of given list using the id() function
print("Id of given list = ", id(gvn_lst))

Output:

Id of given number = 94902631075872
Id of given list = 140049922213520

5)str()

The str() function is used to convert the objects that are passed to it into strings.

# Give the list as static input and store it in a variable.
gvn_num = 50
# Convert the given number into string using the str() function
gvn_num = str(gvn_num)
# Print the type of given number after converting into string using the type() function
print("Printing type of given number after converting into string = ", type(gvn_num))

# Similarly do the same for tuple
gvn_tuple  = (5, 1, 7)
gvn_tuple = str(gvn_tuple)
print("Printing type of given tuple after converting into string = ", type(gvn_tuple))

Output:

Printing type of given number after converting into string = <class 'str'>
Printing type of given tuple after converting into string = <class 'str'>