{"id":27181,"date":"2022-04-06T21:59:18","date_gmt":"2022-04-06T16:29:18","guid":{"rendered":"https:\/\/python-programs.com\/?p=27181"},"modified":"2022-04-06T21:59:18","modified_gmt":"2022-04-06T16:29:18","slug":"python-code-introspection","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-code-introspection\/","title":{"rendered":"Python Code Introspection"},"content":{"rendered":"
What is Code Introspection?<\/strong><\/p>\n The ability to determine the properties of objects at runtime is referred to as introspection.<\/p>\n 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.<\/p>\n Python, as a dynamic, object-oriented programming language, supports extensive introspection. Python’s support for introspection is extensive and widespread.<\/p>\n In Python, there are numerous built-in methods for introspection. They are:<\/p>\n Some of these are explained below<\/p>\n The type of object passed as a parameter is returned by this type() function.<\/p>\n Output:<\/strong><\/p>\n The help() function is used to return the documentation for an object. It contains information about what functions do in the program.<\/p>\n Output:<\/strong><\/p>\n The dir() is a built-in function of python that returns a list of attributes and methods for an object.<\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n Output:<\/strong><\/p>\n The str() function is used to convert the objects that are passed to it into strings.<\/p>\n Output:<\/strong><\/p>\n 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 …<\/p>\n\n
Code Introspection in Python<\/h2>\n
1)type()<\/strong><\/h4>\n
# Import sys module using the import keyword\r\nimport sys\r\n# Printing the type of given sys module using the type() function\r\nprint(type(sys))\r\n\r\n# Printing the type of given number using the type() function\r\ngvn_num = 80.45\r\nprint(type(gvn_num))\r\n\r\n# Printing the type of given tuple using the type() function\r\ngvn_tuple = (9, 7, 3)\r\nprint(type(gvn_tuple))<\/pre>\n
<class 'module'>\r\n<class 'float'>\r\n<class 'tuple'><\/pre>\n
2)help()<\/strong><\/h4>\n
# Get the information about the list using the help() function\r\nhelp(list)<\/pre>\n
Help on class list in module builtins:\r\n\r\nclass list(object)\r\n| list(iterable=(), \/)\r\n| \r\n| Built-in mutable sequence.\r\n| \r\n| If no argument is given, the constructor creates a new empty list.\r\n| The argument must be an iterable if specified.\r\n| \r\n| Methods defined here:\r\n| \r\n| __add__(self, value, \/)\r\n| Return self+value.\r\n| ........................................\r\n...................................................\r\n...................................\r\n\r\n| Static methods defined here:\r\n| \r\n| __new__(*args, **kwargs) from builtins.type\r\n| Create and return a new object. See help(type) for accurate signature.\r\n| \r\n| ----------------------------------------------------------------------\r\n| Data and other attributes defined here:\r\n| \r\n| __hash__ = None<\/pre>\n
3)dir()<\/h4>\n
# Printing all the attributes and methods of the given list using the dir() function\r\ngvn_lst = [8, 3, 6]\r\n\r\nprint(dir(gvn_lst))<\/pre>\n
['__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']<\/pre>\n
4)id()<\/h4>\n
gvn_num = 50\r\n# Printing the id of given number using the id() function\r\nprint(\"Id of given number = \", id(gvn_num))\r\n\r\ngvn_lst = [8, 3, 6]\r\n# Printing the id of given list using the id() function\r\nprint(\"Id of given list = \", id(gvn_lst))<\/pre>\n
Id of given number = 94902631075872\r\nId of given list = 140049922213520<\/pre>\n
5)str()<\/h4>\n
# Give the list as static input and store it in a variable.\r\ngvn_num = 50\r\n# Convert the given number into string using the str() function\r\ngvn_num = str(gvn_num)\r\n# Print the type of given number after converting into string using the type() function\r\nprint(\"Printing type of given number after converting into string = \", type(gvn_num))\r\n\r\n# Similarly do the same for tuple\r\ngvn_tuple = (5, 1, 7)\r\ngvn_tuple = str(gvn_tuple)\r\nprint(\"Printing type of given tuple after converting into string = \", type(gvn_tuple))<\/pre>\n
Printing type of given number after converting into string = <class 'str'>\r\nPrinting type of given tuple after converting into string = <class 'str'><\/pre>\n","protected":false},"excerpt":{"rendered":"