Code introspection in Python - GeeksforGeeks

07 Dec, 2018
Introspection is an ability to determine the type of an object at runtime. Everything in python is an object. Every object in Python may have attributes and methods. By using introspection, we can dynamically examine python objects. Code Introspection is used for examining the classes, methods, objects, modules, keywords and get information about them so that we can utilize it. 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.Python provides some built-in functions that are used for code introspection.They are:1.type() : This function returns the type of an object.
# Python program showing# a use of type function  import math  # print type of mathprint(type(math))   # print type of 1 print(type(1))  # print type of "1"print(type("1"))  # print type of rkrk =[1, 2, 3, 4, 5, "radha"]  print(type(rk))print(type(rk[1]))print(type(rk[5]))
Output:
<class 'module'>
<class 'int'>
<class 'str'>
<class 'list'>
<class 'int'>
<class 'str'>

 2.dir() :This function return list of methods and attributes associated with that object.
# Python program showing# a use of dir() function  import mathrk =[1, 2, 3, 4, 5]  # print methods and attributes of rkprint(dir(rk))rk =(1, 2, 3, 4, 5)  # print methods and attributes of rkprint(dir(rk))rk ={1, 2, 3, 4, 5}  print(dir(rk))print(dir(math))
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__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']
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

 3.str() :This function converts everything into a string .
# Python program showing# a use of str() function  a = 1print(type(a))  # converting integer# into stringa = str(a)print(type(a))  s =[1, 2, 3, 4, 5]print(type(s))  # converting list# into strings = str(s)print(type(s))
Output:
<class 'int'>
<class 'str'>
<class 'list'>
<class 'str'>

 4.id() :This function returns a special id of an object.
# Python program showing# a use of id() function   import matha =[1, 2, 3, 4, 5]   # print id of aprint(id(a))b =(1, 2, 3, 4, 5)   # print id of bprint(id(b))c ={1, 2, 3, 4, 5}   # print id of cprint(id(c))print(id(math))
Output:
139787756828232
139787757942656
139787757391432
139787756815768

Picked
python-basics
python-oop-concepts
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
Create a Pandas DataFrame from Lists
Reading and Writing to text files in Python