Code Introspection in Python

Tram Ho

This keyword “code introspection” I accidentally found in an article on Medium. After a while of research, I know that “code introspection” is the exploitation of information about classes, methods, modules … right at runtime. Python provides us with a number of built-in methods to assist with this, so that we can better understand the program, understand what objects we are working on (what it is, it can do. what, …) through which it is possible to properly handle logic or debugging in a more efficient way. Perhaps that is also the reason for appearing the word “introspection” (inner self-reflection?).

During the work, I have also tried to use some of the above methods and found it very useful. Therefore, I want to sum it up again to make myself remember longer, and hope to share with you who want to learn about Python, add some interesting new knowledge. If everyone has any suggestions, please tell me!

Here I will introduce some popular intropection functions:

type ()

To illustrate this method, I would like to retrieve the example in a Medium post (link at the end of the post):

How do I store the value 1?

We can list the following ways:

There are more ways than you think right? Now we will check the types of the variables above:

Nearly every variable is a different data type (except for the two at the end). But is the data type important ?

Have. Because it will define possible operations, and ensure the output is as expected.

Continuing the above example, when we perform addition (operator +) on different data types, we will get completely different outputs:

The same is working on variables that “store the value 1”, but in the first one we are adding arithmetic, the second is adding strings, and the last is having an error!

Personally, I often use the type() during debugging. For me, data types is really a nightmare, because each library, every working team, or each individual will sometimes have different conventions and problem approaches. In an image processing project on our team, the image matrix can be found in the form np.uint8 or np.float32 ; the point coordinate can be encountered in the Tuple form of two integers, List contains two integers, or np.ndarray contains two integers; etc … If not careful, we can encounter unintended errors like in the above example.

Thus, it is very important to check data types and agree on a common standard when working. I usually do this by using a type hint when declaring a function and using assert in combination with isinstance() to validate input inputs before doing the next logic processing. The above function can be rewritten as follows:

isinstance ()

As seen in the above example, the isintance() method checks if an object is an instance of the class . If true, isinstance() will return True , otherwise return False .

We can refer to an example below:

Thanks to the definition of the class of each object, we have actions suitable for each specific class. In the above example, animal is an instance of Dog so it will do the bark() instead of fly() . This helps to minimize risks during program execution.

hasattr ()

This method is to determine if the object has an attribute or not.

Continuing the example above, let’s try to make the dog Pluto fly like a bird:

Thus, Pluto does not have the fly attribute. But what is “sure all dogs can’t fly”? Suppose we declare an alien dog for example:

To avoid the AttributeError error as above, we can solve it by:

Since animal doesn’t have the fly() attribute, the condition becomes false, the won’t be executed. However, in alien_animal there was this way so the conditions became right, and this dog was flying.

(Unfortunately, the actual Woola in the John Carter movie can’t fly.)

dir ()

This method lists all the properties and methods of any object .

As a matter of fact, almost everything in Python is an object. We can easily verify with isinstance() :

(Just about objects in Python, I have seen a lot of interesting things. I will synthesize and share with everyone in the next sections.)

The Object will have certain properties and methods. dir() is the power function that lists all the methods and properties of that object , even the default properties.

This output helps us to have an overview of the animal object earlier, determine what properties the animal has and what methods can be executed. In addition to the bark method and the name property at the end of the array, the remaining default methods are called magic methods (I’ll discuss this in another article).

id ()

This method returns the identifier (unique) of the object at the current program life, which in C can be understood as the memory address that the variable refers to. This value will change each time the program is run.

As the results above, a and b are referencing the same device. Now let’s try to make b a shallow copy of a :

At this time, the two variables a and b no longer share the same id , or can be understood that they no longer point to the same device. We can use the keyword is to test:

Note that using == still returns True because we are currently comparing values, not ids .

help ()

When debugging, we can use this method to display documentation about a function, class, module or variable.

Other methods

There are also some other methods like: repr() , getattr() , sys() or issubclass() to check if a class is inheritance of another class , callable() used to check if Is that a function.

What if you are too lazy to execute each of the above methods for a certain object ?

Fortunately, we can write an aggregate function to use in one call:

Isn’t it convenient to just call one line to reveal how much mystery about the object?


Through the recent synthesis, I have introduced the concept of “code introspection” and the basic usage of Python’s built-in functions. Hope you find it helpful. In addition to the methods above, we can also use package inspect with the “better” customizations, helping you to become more powerful when executing code introspection. You can take time to learn more.

In the end, I believe the article is probably still lacking. Therefore, I hope to receive everyone’s suggestions for the article to be more complete. I would like to thank and see you all in the next post! ^^

Enjoy coding!

Original link

References .

Share the news now

Source : Viblo