Mindmajix

Code Introspection in Python

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

Introspection is an act of self examination and is the greatest strength of Python. As you know, everything in Python is an object, and introspection is code looking at other modules and functions in memory as objects, getting information about them, and manipulating them. Along the way, you’ll define functions with no name, call functions with arguments out of order, and reference functions whose names you don’t even know ahead of time.

In computer programming, introspection is the ability to determine the type of an object 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.

Introspection reveals useful information about your program’s objects. Python, being a dynamic, object-oriented programming language, provides tremendous introspection support. Python’s support for introspection runs deep and wide throughout the language. In fact, it would be hard to imagine Python without its introspection features. It has the ability to determine the type of an object at runtime. Henceforth, by using introspection, we dynamically inspect the Python objects.

Code introspection is required for examining into other modules, classes, functions, keywords or objects and retrieving information about them so that manipulation can be carried out, if necessary. Python provides several functions and utilities for code introspection. Along with it, you can also define call functions and reference functions with no name .

Built- in functions mentioned in the chapter on functions are used for code introspection. Have a look at some of the following examples.

Finding out the type :

Screenshot_51

Converting to string :

Screenshot_52

You can use the dir() function to know about the functions and attributes for an object, as shown below :

Screenshot_53

In the similar fashion, you can use other functions provided in table about built-in function (refer to chapter on functions)

https://docs.python.org/2/library/inspect.html

 

Enroll for Instructor Led Live Python Training


0 Responses on Code Introspection in Python"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.