Metaclass discussons

Michele Simionato mis6 at pitt.edu
Sun Aug 24 13:46:26 CEST 2003


Gustavo Niemeyer <niemeyer at conectiva.com> wrote in message news:<mailman.1061644806.1203.python-list at python.org>...
> Hi everyone!
> 
> I was just part of a small discussion concerning metaclass features on
> Python, and one limitation that was appointed was the inability to
> redefine metaclasses on existent code. Than I thought it was a good
> way to explore some more the metaclass concepts.. 
> 
> Basically, people were arguing that there are some uses for that, based
> on plugging new knowledge on existent classes and instances. Without
> complaining too much about usage issues, I started looking for ways to
> dismiss that limitation.
> 
> Without yet considering interpreter hacks, that's what I had as a first
> solution (a very limited one, I must admit):
> 
> class M(type): pass
> __metaclass__ = M
> class object:
>     __metaclass__ = M
> execfile("somemodule.py", globals(), locals())
> 
> Can you imagine any alternative which is not based on the same concept
> (executing the module code on a hacked context)?

Oh, what a wonderfully evil hack! Thanks Gustavo, I never come to my
mind that I could redefine the object class!
I was using the hack of modifing the source code by adding a __metaclass__
atribute and re-exec-uting it, but I didn't like the trick. Then I wrote
a script to regenerate an entire hierarchy with a different metaclass.
Much less hackish, but a bit non-trivial. Your idea is perfect for easy
hacks in the debugging phase (i.e. add a metaclass generating logging
capabilities or other more general debugging utilities). You can
spell it even as

class object(object):
    class __metaclass__(type):
        pass

# rest of the code

Michele Simionato, Ph. D.
MicheleSimionato at libero.it
http://www.phyast.pitt.edu/~micheles
--- Currently looking for a job ---




More information about the Python-list mailing list