top of page

Introspection in Python | Codersarts

Updated: Jul 31, 2021

What is python Introspection ?

Introspection used to self examination of python objects Properties and type at the runtime. By using introspection, we can dynamically inspect Python objects.


List of important Introspection


  • dir()

  • type

  • id()

  • sys

  • hasattr()

  • issubclass()

  • callable()


dir()


It returns a sorted list of attributes and methods belonging to an object.


syntax:


>>> dir(())


Output:


['__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 use dir() with different ways:


obj = classname()


print(dir(obj))

print(dir([]))

print(dir({}))

print(dir(1))

print(dir())

print(dir(len))

print(dir(sys))

print(dir("String"))

Add modules in dir()


In dir() add modules using import


>>> import math, os

>>> dir()


It import two module, math and os in dir()



type()


It returns the type of an object.


Example:


#!/usr/bin/python3


# typefun.py


import sys


def function():

pass


class MyClass(object):

def __init__(self):

pass


o = MyClass()


print(type(1))

print(type(""))

print(type([]))

print(type({}))


Output:


<class 'int'>

<class 'str'>

<class 'list'>

<class 'dict'>


id() function


It returns a special id of an object.


Example:


#!/usr/bin/python3


# idfun.py


import sys


def function():

pass


class MyClass(object):

def __init__(self):

pass


o = MyClass()


print(id(1))

print(id(""))

print(id({}))

print(id([]))


Output:


10914368

139696088742576

139696087935944

139696065155784


sys module


It provides access to system specific variables and functions used or maintained by the interpreter and to functions that interact strongly with the interpreter.


First import the sys module:


>>> import sys


If you want to check which platform is running currently then use this


>>> sys.platform


It return the system python where python script folder exists:


>>> sys.path



hasattr() function


It checks if an object has an attribute


callable() function


It checks if an object is a callable object (a function).


Now this blog is completed if you want to read more about programming language with Codersarts then go to the codersarts blogs


If you like Codersarts blog and looking for Programming Assignment Help Service,Database Development Service,Web development service,Mobile App Development, Project help, Hire Software Developer,Programming tutors help and suggestion  you can send mail at contact@codersarts.com.

Please write your suggestion in comment section below if you find anything incorrect in this blog post


bottom of page