15

I have been programming mainly in PHP, and I am trying to make a switch to python. I am skilled with PHP, and I have never needed to use introspection / introspection like capabilities. What good is code introspection, and in what situations would I find it indispensable?

Here is the only way I find it useful: From the examples I saw in 'Dive into Python', introspection basically means that you can list all of the functions and attributes of an object. To me it seems that introspection is just there as a "user's manual" to an object. It lets you look at the object and its functionality from the python shell.

I just do not see why or in what situation you would take an arbitrary object, introspect upon it, and do something useful.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Chris D.
  • 675
  • 7
  • 14

4 Answers4

14

Suppose you are given a custom object and you want to know if the object has certain attributes or has as a certain method, then the introspection function such as hasattr can be used to find that out.

Also like the DiveintoPython book already illustrates, suppose you are building a GUI Editor with Auto-Completion feature, you want to get the public methods of the object which are callable at the run-time, then you can use the introspection methods like getattr for each for the methods got via dir and check if it is callable and then display it in your auto-completion list.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • Hi. Thank you for your reply. One thing that confuses me about this is maybe you are expecting (and have written code to reflect this expectation) that if an object wants to get a user's ID, this would be done with a function called "getUserID()". And in your code, you introspect and look for this function called getUserID. Well, maybe the programmer prefers to name his functions "differently" and calls his function "retrieve_the_user_identification_integer()" or something wacky like that. This is what confuses me about on the fly introspection. It seems that you are assuming a lot. – Chris D. Feb 21 '11 at 01:18
  • Actually, yours is good doubt. You normally don't introspect on the objects created by you or your class. Because, you already know that something is present! Now, consider the cases where objects are **given** to you and you don't know much information about them, introspection is for those cases. Good example. You write a GUI Editor with Autocompletion, I use the editor and I create a custom module and object. When I try to auto-complete an object which I wrote, your editor should give me the list of possible functions. How will your editor know? By introspecting my objects. HTH. – Senthil Kumaran Feb 21 '11 at 01:24
  • 2
    The concept of "duck typing" is relevant here. If an object has a `write()` method, Python will be happy to let you use it anywhere you are trying to write a file -- regardless of whether it is derived from the `file` class. (This is very different from some other languages!) So if you are about to write to a file, you can either give it a whirl and see if you get an exception, or you can check to see if it has a `write()` method using `hasattr()` first. The usual Python idiom is in fact to try it and catch exceptions, but there are situations where "looking before you leap" is also useful. – kindall Feb 25 '11 at 22:55
  • To second what @kindall said: in Python, ["method-call type checking"](https://stackoverflow.com/a/66502857/4561887), AKA: "try the methods, don't check the type" typing is frequently used in place of introspection. Note that some crazy quacks :) call this "duck typing", which is a pretty ridiculous and opaque term in my opinion. See the link to read my description of what the heck that means. – Gabriel Staples Mar 09 '22 at 04:21
4

One example where I have used introspection on a real project:

We had a service that was managing background tasks called TaskService. Each task was actually implemented as a class that was implementing the Start() Stop() methods of a given interface. We had a config file, in which we were matching each task with its class. So when running TaskService, it just browsed the config file, and for each task it took the name of the class and instanciated it (during runtime) through reflection (introspection is a subpart of reflection).

Another example of where introspection can be useful is in the use of annotations in your programming language. Annotations are used to give metainformation about your classes to other third party programs (like ORMs), for instance you can use annotations to tell whether a class is an entity class (it is the case in Java, I don't know about Python sorry), or about the type of association of certain attributs etc.

Code Completion is another example of the usefulness of introspection.

And by the way, as you mentionned, introspection helps a lot to program documentation tools.

Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158
2

I wrote a documentation validator that runs tests on PDF files to check for various problems with them. The tests are methods of special classes that represent Subversion branches, products, manuals, and arbitrary groupings of various types. The validator engine uses introspection to find these special classes, instantiate them, and run their methods.

I could have written the validator so that you have to write boilerplate code to instantiate each class, call each method, etc. But that is repeating yourself and it is prone to maintenance problems (failure to update both places when adding/removing tests, in this case). By taking advantage of the fact that you want to apply the same operation to all the special classes, the computer can essentially do the boilerplate stuff for you, and it won't make mistakes. That way, you have to declare the structure of the documentation in only one place.

kindall
  • 178,883
  • 35
  • 278
  • 309
1

A little bit late on this, but another example of introspection in use is with Active Record (a Ruby library that maps objects to database tables). Active Record uses introspection to look at the name of the class, determine the associated table, and defines methods that access object attributes after inferring their names from the database schema. It reads the schema at runtime (metaprogramming & introspection in play here). So for example, if you have a class Person, you don't have to write accessor methods e.g name or email. As long as columns by the same name (in this case name and email) exist in the associated table (in this case people), Active Record defines accessor methods for these attributes of the same name at runtime.