The inspect library in Python
Todays post will be on the inspect library in python that can be used as a way to inspect live objects. Some examples of live objects are modules, classes, methods of classes, stand alone functions. There are also tracebacks, and frame objects that can eb used as a way to examining the state of the python interpreter.
The inspect library is packed with helpful methods for getting the members of a module, and also to find out if something is even a module to begin with. In addition there are many more additional such methods that have to do with extracting source code, and checking the state of frame objects.
This is a library that seems to show up in a lot of basic python examples for various things, so it makes sense to take a moment to take a deeper look into the module as a whole.
1 - Type checking with the inspect library, to find out what I am dealing with when it comes to inspecting things
I have wrote a post on built in functions a little while back, and one helpful built in function is the type function. This is a good starting point when it comes to having a tool to know what a given value is. However in order to use it to test if something is a module for example I have to not just use the type function by itself, but rather use the name property in an expression in which I am comparing the name property to the string module. So if I want to know if something is a module or not, I can do an expression with the type function, or I can use an inspect function such as the is module function.
So this section Will be a quick overview of some of these methods of the inspect library, and I guess I can also take a moment to touch base on the type built in function while I am at it.
1.1 - The inspect.ismodule method
When it comes to using the inspect module to start inspecting the contents of a module, it is great to have a way to first find out if I am even dealing with a module to begin with. For such a task I can use the is module function of the inspect library to so so. An alternative with python by itself would have to involve the use of the type built in function in a simple expression.
|
|
1.1 - The inspect.isfunction method
The inspect modules also has a method that I can use to find out if I am dealing with a function or not. There is a deference between a function and a method when it co,es to a function that is part of a class. Simply put if a function is a part of a class insatnce then it is a method, otherwise it is a stand alone function. So when it comes to working with a class the inspect.isfunction method will return false for functions that are methods of a class, and true if the function is just a function by itself.
|
|
1.1 - The inspect.isClass method
There is then a method that I can use to find out if a value is a class or not. I will not be getting into a class in detail here, as doing so might be a bit off topic. However a class is a way to go about creating not just an object, but a kind of class of an object. The class itself can be used to create an object that is an instance of that class, and this instance often has a few methods that are functions tht can be called off of a class instance. However as far as i am concern for this section at least the inspect.isclass method is used to just find out if something is a class or not.
|
|
1.2 - The inspect.ismethod method
There is also a function that can be used to find out if something is a method of a class rather than just a stand alone function. As i have mentioned in the section on the inspect.isclass method there are functions that can be called off of an instance of a class that are called methods. The inspect.ismethod method can be used to find out if a function is a method of a class, while the inspect.isfunction method is there to find out if I am dealing with a stand alone function or not.
|
|
2 - The get members method
The get members method of the inspect module can be used as a way to get all the members of an object, sorted by the name of each. In addition is is possible to pass a predicate function as a second argument that can be used as a way to filter members of the object. So when it comes to learning about a module I can use this method as a way to get all the classes of a module to know what I am dealing with when it comes to the classes that are in a module. So in this section I will be going over the get members method of inspect, and how it can be used as a way to filter over libraries.
2.1 - Get all members of the sys module
Lets start off with a simple example of the inspect.getmembers method where I am taking a look at everything there is to work with in the sys module.
|
|
2.2 - Get just the functions in the copy module
When using the get members method of the inspect module I can pass one of the type checking methods of the inspect module as a section argument. The result will be a collection of memebers that are of the type that matches the given type checking method. For example say that I just want the functions in the copy module to which there apear to be only two copy and deep copy. I can pass the copy module as the first argument, and then the inspect.isfunction method as the second argument.
|
|
2.3 - get all classes in the array library
Once again I can filter the colection of members by some kind of type by passing one of the type checking methods as a second argument, such as the inspect.isclass method.
|
|
3 - Get The current frame object
One thing I might need to do now and then in python is to get the current call stack frame. So far it seems like this is something that I need to do in order to get a path to the current script that is being called, however I am sure that there are many other examples where I might need to work with frame objects.
3.1 - Basic get current frame example
The inspect.currentframe method is one way to go about getting a frame object.
|
|
4 - Get dir of current script example using import, os, and sys
So far the main reason why I might use the inspect library is to help me with getting absolute paths that I might want to insert into the paths list of the sys library. In real examples the inspect library is often not used alone of course, the library goes hand in hand with many other built in librarays such as the os library, and the sys library just to name a rew.
|
|
5 - Conclusion
The inspect module strikes me as one of several modules that I am going to want to use when it comes to starting a real python project of some kind. The main reason why is becuase of the feature of getting and using frame objects, at least more son than the type checking features anyway.
One other core library that I think is important so far is the sys library which contains the path list that I am going to want to work with a little now and then. The sys library can also be used to access positional argumnets that might have been given at the command line when the script was called, however there is another library called argparse that is also part of a core stack of sorts when it comes to parsing command line options.
In one of the examples in this post I also made use of the os library that also contains a buch of helpful methods that have to do with working with paths.