Reflection and aspect programming in Python

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Jan 29 16:44:21 EST 2008


En Tue, 29 Jan 2008 14:42:55 -0200, Jean-François Houzard  
<jhouzard at gmail.com> escribi�:

> I'm a student at UCL Belgium and I have to write a paper about reflection
> and introspection in Python.
>
> It is somewhat difficult to find advanced information about reflection in
> Python, not only introspection but also the other sides of reflection.

I'm not familiar with what you mean by "the other sides of reflection",  
perhaps you could explain what you mean exactly. But I'll try to give some  
examples of what can be made in Python:

Direct introspection: hasattr(obj, "color") tells if obj has an attribute  
"color". dir(obj) returns a list of obj's attributes.
Similarly, you can obtain an attribute by its name: getattr(obj, "color")  
is a silly way of writing obj.color, but it's useful when "color" is  
replaced by a variable.
You can set an attribute: obj.width = 3, or setattr(obj, "width", 3). You  
can add any attribute to any object - the set of attributes allowed for an  
individual object isn't defined by its class (in general).
Methods are attributes too, so all the above may be applied to testing,  
retrieving and setting methods too.
As classes usually live in modules, the above technique allows you to get  
a class given its name: cls=getattr(modulename, classname). And then  
create an instance: obj=cls(some,arguments)

You can query any object its type: type(obj). In some cases (including all  
user defined classes) you can change an object's class to another  
dynamically: obj.__class__ = NewClass, and it will behave as it were an  
instance of NewClass (but any missing attributes won't be created  
magically)
You can dynamically create new types/classes: if Bar and Baz are classes,  
type("Foo", (Bar, Baz), {}) returns a new class named Foo which inherits  
 from Bar and Baz. It's equivalent to this statement:
class Foo(Bar,Baz): pass

You can evaluate expressions at runtime: eval("2+2").  
eval("Foo(some,arguments)") returns an instance of Foo.
You can compile blocks of code into a code object, and execute them, using  
`compile` and `exec`.
Classes, its instances, functions, methods, code, execution frames,  
builtin types, all of them are first order objects in Python: you can  
create them, assign them to variables, pass them as arguments, query its  
properties, modify them when allowed, etc.

> I'm using the book: "Programming Python, Thrid Edition" but the chapter  
> only
> describes briefly some useful functions. The IBM site talks somewhat  
> about
> the concept of "metaclass" but is there more about reflection than only
> metaclass programming?
> I'm also looking for a class diagram of the hierachy of the python  
> classes
> "metaclass", "class", "object" but didn't find it nor did I find lots of
> information about that subject.

Read sections 3 (Data Model) and 4 (Execution Model) in the Python  
Language Reference.
The root of the class hierarchy is `object`; all other [new-style] classes  
inherit from object direct or indirectly.
"metaclass" is a generic name. Metaclasses are to classes as classes are  
to instances. The type of an instance is its class; the type of a class is  
its metaclass. I won't even try to explain metaclasses further here; see  
the links at http://www.python.org/doc/newstyle/
You may be interested in decorators too. Decorators allow for higher order  
functions: a decorator is a function which takes a function as a parameter  
and returns a (possibly different) function as a result.
http://wiki.python.org/moin/PythonDecorators

> I looked around on the site www.python.org and some others about what  
> could
> be a killer application using reflection in Python an maybe more  
> information
> about it. Like how does it use reflection to do the trick.
>
> Is there an advanced and completed Aspect Python plugin or are they all
> experimental?
> Are there some papers about that part of reflection or more detailed  
> sites
> that talks about it.

This thread from last year discusses why Aspect Programming isn't so  
relevant in Python
http://groups.google.com/group/comp.lang.python/browse_thread/thread/af82d7fa8fdf5a28/

-- 
Gabriel Genellina




More information about the Python-list mailing list