Metaclasses presentation slides available...

Michele Simionato mis6 at pitt.edu
Fri Aug 29 05:27:07 EDT 2003


"Mike C. Fletcher" <mcfletch at rogers.com> wrote in message news:<mailman.1062101481.30815.python-list at python.org>...
> Slides from my PyGTA presentation on Tuesday, focusing mostly on 
> why/where you would want to use meta-classes, are available in PDF format:
> 
>   http://members.rogers.com/mcfletch/programming/metaclasses.pdf

I gave a look at your transparancies and they are very good, 
indeed. Still, there a couple of minor points I think
could be improved, for the sake of metaclass newbies
(I am in a nitpick mood today ;)

1) On page 26 you say:

Note: In Python 2.2.3, the meta-class object's __call__ is
not called by the metaclass hook, the interpreter calls
__new__, then __init__ directly

That's correct (and true in 2.3 too), still there is a way 
to get the metaclass hook to call __call__: it involves
meta-metaclasses ;)

class MetaMetaCall(type):
    def __call__(mcl,name,bases,dic):
        print "%s.__call__ called!" % mcl

class Meta(type):
    __metaclass__=MetaMetaCall
    
class C:
    __metaclass__=Meta # invokes Meta.__call__, not Meta.__new__!

To understand this, it is enough to remember that metaclasses are 
just classes and therefore can be modified by using meta-metaclasses.
BTW, this is the reason why metamethods do not attach to instances:
in this example MetaMeta.__call__ is a meta-metamethod, which is
accessible
to Meta, but not to its instance C; therefore you can modify the C
__call__
(and Meta.__call__ too, if you wish) method independently from
MetaMeta.__call__, avoiding name clashes.

2) On page 30 you say that you can modify the dictionary both in
__new__ and in __init__. This is true, but not obviously true.
In __new__ you can simply change "dic", in __init__ this would not
work:

class M(type):
    def __init__(cls,name,bases,dic):
        dic['spam']='egg'

class C:
    __metaclass__=M

print C.__dict__ # does not contain "spam"

You can modify the dict only indirectly, with something like
cls.spam='egg'.

For the rest, excellent presentation!

> BTW, for those not on Python-list, be sure to check out David & 
> Michele's newest developerworks article on the topic:
> 
>   http://www.ibm.com/developerworks/library/l-pymeta.html
>   http://www.ibm.com/developerworks/library/l-pymeta2/?ca=dnt-434


You forgot to mention Alex Martelli's presentation:

http://www.strakt.com/dev_talks.html

> Have fun,
> Mike
> 
> _______________________________________
>   Mike C. Fletcher
>   Designer, VR Plumber, Coder
>   http://members.rogers.com/mcfletch/

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