Reflection in PythonIn this tutorial, we will learn about the reflection mechanism in Python. We will also discuss how to implement the reflection and how it is effective for programming. Let's have a brief introduction to the reflection. IntroductionThe reflection is a mechanism that allows inspecting the attributes of objects that might be passed as a parameter to the function. In other words, reflection is used to increase the dynamic description of the function. For example - When we write the type(obj) then Python will return the type of obj. The reflection helps to write the one recursive reverse function that will work for the iterables like list, string, and another sequence that supports slicing. The str() method returns the empty string the same as we write ". Likewise, writing a list() is the same thing as writing []. Let's understand the following example. Example - Output: [40, 30, 20, 10] tniopTavaJ Explanation - While working on a project, a developer writes a lot of classes, and the class has its own functions, and so on. A programmer has control over which class to be used or which function to be called. But sometimes, we need to decide which code to execute based on the user's needs. The user may enter some data, or otherwise, and the reflection passes the user's instruction to run the specific code. This process happens automatically without having to manually check whether the user instruction should execute the piece of code, but the reflection mechanism automatically searches and execute the particular block of code. Reflection Enabling FunctionsPython provides four methods to enables reflection. These functions are -
Let's understand the above methods.
Example - Output: <class 'int'> <class 'str'> <class 'list'>
Example - Output: is a float: True is an integer: True is a string: True is a tuple: True is a set: True is a list: True is a dict: True
Example - Output: x is not callable y is callable
Example - Output: ['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__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'] ['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__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']
Example - Output: "John Doe" "Unknown" Explanation - In the first line, we create an object called obj. In the second line, we use the getattr() function to get the value of the name attribute of obj. The function returns the value of the attribute, which is "John Doe". In the third line, we use the getattr() function to get the value of the age attribute of obj. The function returns the value of the attribute, which is 30. In the fourth line, we use the getattr() function to get the value of the height attribute of obj. The attribute does not exist, so the function returns the default value, which is "Unknown". ConclusionReflection in Python is the ability to inspect and alter the structure and behavior of a program at runtime. This can be used to make code more flexible and dynamic, by allowing it to adapt to different situations, inputs, and outputs. Reflection can also be used to make code more reusable, by allowing it to be used in a variety of ways. We can use reflection to create a function that can be used to operate on any object, regardless of its type. The reflection is used to create a program that can adapt to different user inputs. We can use reflection to create a program that can be used to generate different outputs, depending on the situation. Next TopicStock Span Problem in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India