Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

What does introspection mean exactly? How is it useful for a programmer? Can somebody please explain in layman terms?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Introspection is a way of finding out what an object is and what it contains at runtime. In some languages this is more necessary than others. Python which is dynamically typed (that is to say, an object can gain attributes and methods at runtime) uses introspection to find out an object's current status.

Here is a very simplistic Python example where you start with a class and add a new attribute, and, using introspection, monitor what attributes are available on the object instance.

>>> class MyObject:
...   variable = False

>>> instance = MyObject()

>>> dir(instance)
['__doc__', '__module__', 'variable']

>>> instance.newvariable = "Kittens"

>>> dir(instance)
['__doc__', '__module__', 'newvariable', 'variable']

So in dynamic systems you can use things like Python's hasattr to tell if an object will be able to do what you're trying to do with it. Even if you don't use it at runtime, introspection is also extremely useful for debugging.


Now onto GObjects and GObject-based libraries. They're in C and in C you don't get all this lovely fluffy managed code nonsense, you get performance. This means built-in introspection isn't there and that means it's harder to maintain an external binding.

Bindings are an API between things in different languages that allow them to work together. In order for that to work you need a common API and in this case that API is GObject. But for this to work, the caller binding has to know what's available to it. Until GObject Introspection this has had to be hard-coded into the bindings and that means maintaining a hand-written list on each set of bindings.

GObject Introspection essentially provides a scanner that can hunt through the current source of the GObject you're working with and provides you with XML binding information. This is then processed by the bindings and you end up with an API that just works, all the time.

I certainly haven't covered everything here but I'm trying to give you an idea of the two topics (introspection and why GObjects need it). If you want to know more, there's plenty on the Gnome website.

share|improve this answer
    
thanks for your simple answer, it's what I needed :) –  thelaststud Feb 28 '12 at 8:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.