How Reflection Effect in Python Works:

Python (

In most languages, there is a reflection mechanism. In terms of function, reflection is to increase the dynamic description ability of the program. Popularity is the decision to let users participate in code execution. When the program is written, we will write a lot of classes, and the class has its own functions, objects, and so on. These classes and functions are for subsequent code services.

The programmer decides which class to use and when to call a function. But many times, we need to decide which piece of code to execute based on the user’s needs. The user may issue an instruction by clicking, entering data, or otherwise, and the reflection passes the user’s instruction to the piece of code that needs to be executed. This process is performed automatically, without having to manually check whether the user instruction should execute the piece of code, but the reflection mechanism automatically finds the block of code that is executed.

Most reflections are illustrated by the web, and the most common use case for reflection itself is to call different functions depending on the web url. Of course, here we don’t have to discuss his specific application, just to explain his meaning.

The reflection mechanism of Python is relatively simple. There are four key functions: getattr, hasattr, setattr, and delattr. The first two are the most commonly used, and the last one is rarely used. The reflection defined by Python itself refers to the operation of certain elements in the container in memory. This container not only includes classes, but also functions and objects.

The difference between these three is that when looking for objects, in addition to looking up the objects themselves. , will also go to the class of the created object to find. To illustrate the specific role of reflection in Python with practical examples, let’s look at the requirements first. In all languages, we can easily make it easy for users to enter a data and then print that data, which is the simplest human-computer interaction. The implementation in the code is to generate a variable, get the user input data, and assign it to the variable. Print variables. Similarly, we can define two functions in a class, then ask the user to input data, and decide which function to execute according to the data input by the user.

This is an artificial reflection mechanism. When there are only two functions that need to be looked up, we can use if-else to judge. But if the data is as many as hundreds, the repetitive use of if is not only inefficient, but the amount of code is also difficult to estimate. In this case, reflection is needed. The specific code is as follows:


To analyze the code, first of all, the code is only an instance, extremely imperfect, and the input data can only be the name of the defined function, but we mainly discuss the role of getattr. It’s not hard to see from the code that it takes a class name and a string as arguments, then looks for the same function name as the string in the given class and returns the entire contents of that function. So, our variable is actually a function, so it can be called directly. This is a simple complete reflection.

It is also the most important function of reflection in python. The role of hasatter is more for the getattr service. Just like in the above code, it is possible that the data entered by the user does not have a matching function name in our defined function, and it cannot be executed. If there is no error defense mechanism, the program will crash, so get the data input by the user, before going directly to find the execution, you need to first determine whether the function that the user wants to execute exists. This is the function of hasattr. The code is as follows:

Python 2 (

As you can see, the parameters of the hasattr function are the same as getattr, receiving a class and a string, and returning a boolean value. Its role is to detect whether the user input content has a corresponding function. If yes, return true, no, then false. We can prevent errors in the function from being found based on the results. Therefore, it is often used in conjunction with getattr. If the decision is made, it is fetched and executed again. This ensures that the code will not run incorrectly. The role of setattr is to create an object, the code is as follows:

Python 3 (

From the output analysis, the role of the setattr function is to create a new object, the parameters are the class name of the class to which the new object belongs, the object name of the newly created object, the value of the object, this value can be a string, or a number, of course, Is a function, the above code for the sake of simplicity, directly using an anonymous function. The last delattr is to delete the existing function, the usage rate is low, and there is no special attention.

In the process of use, it is also necessary to dynamically obtain the class name. The class names in all the above codes are all fixed input. In actual use, this will make the code extremely inflexible. The first argument of the four reflection functions accepts only the class name and cannot receive the string. The data directly input by the user is obviously a string and cannot be used directly. Of course, we can convert strings to class names, but it doesn’t have to be that much trouble. Python has a corresponding response, the code is as follows:

Python 4 (

The above is the common method of reflection in Python. Of course, in the last piece of code, we should verify whether the class exists, but there is no function for this in python. In the example, the defense mechanism is not given. In the middle, it is definitely not possible, because the class does not exist, the code can not run, so also give the corresponding error defense mechanism. In addition to finding functions in a class, reflections can naturally be used to look up objects in functions, look up properties on objects, methods themselves, and more. These operations are based on memory rather than on the code itself.