I have a question about metaclasses:
How would be the best way to "merge" different metaclasses?
Or, more precisely, what is the best way to merge metaclass functionality?
The idea is basicly the following:
One has several (metaclass) modules that implements an interesting feature.
Lets say, I have a metaclass L, that adds logging capabilities to all method
calls and another one, called P, that creates properties on the fly.
One possibility would be of course to write a new metaclass LP that merges
the previous two.
As far, as I can see (and I might be wrong here) the only intersting place
in a metaclass is its __new__ method where the classdict of the soon to be
created class can be manipulated.
One idea I had, was to wrap the interesting part (the manipulation of
classdict) in a function and create the actual metaclass on the fly (see
example).
Or would it be better, to create some elaborate inheritance scheme with
metaclasses (they are classes after all) ?
Thanks for your input
Stephan
--------------------------------------------------------------------------
def meta1(classname,bases,classdict):
classdict['__meta1__'] = 'meta1'
return classdict
def meta2(classname,bases,classdict):
classdict['__meta2__'] = 'meta2'
return classdict
def buildmeta(*metalist):
metalist = list(metalist)
class metameta(type):
def __new__(cls,classname,bases,classdict):
metalist.reverse()
for func in metalist:
classdict = func(classname,bases,classdict)
return type.__new__(cls,classname,bases,classdict)
return metameta
class c1(object):
__metaclass__ = buildmeta(meta1,meta2)
class c2(object):
__metaclass__ = buildmeta(meta2)
if __name__ == '__main__':
print [x for x,y in c1.__dict__.items() if x.startswith('__meta')]
print [x for x,y in c2.__dict__.items() if x.startswith('__meta')] 2 1891
Stephan Diehl <st***********@gmx.net> wrote in message news:<bd*************@news.t-online.com>... I have a question about metaclasses:
How would be the best way to "merge" different metaclasses? Or, more precisely, what is the best way to merge metaclass functionality?
The idea is basicly the following: One has several (metaclass) modules that implements an interesting feature. Lets say, I have a metaclass L, that adds logging capabilities to all method calls and another one, called P, that creates properties on the fly.
One possibility would be of course to write a new metaclass LP that merges the previous two.
As far, as I can see (and I might be wrong here) the only intersting place in a metaclass is its __new__ method where the classdict of the soon to be created class can be manipulated.
One idea I had, was to wrap the interesting part (the manipulation of classdict) in a function and create the actual metaclass on the fly (see example).
Or would it be better, to create some elaborate inheritance scheme with metaclasses (they are classes after all) ?
Multiple inheritance, no doubt about that.
Whereas you could create the metaclass by hand, as you do in your example,
this way seems to me rather primitive and redundand: multiple inheritance
is there just for the the purpose of merging (meta)classes, why not to
use the facility?
Notice that 99% of times you don't need "elaborate inheritance schemes"
but just a simple diamond. The caveat is that one should use
cooperative methods; here is an example: class M1(type):
.... def __new__(meta,name,bases,dic):
.... print "Called M1.__new__"
.... return super(M1,meta).__new__(meta,name,bases,dic)
class M2(type):
.... def __new__(meta,name,bases,dic):
.... print "Called M2.__new__"
.... return super(M2,meta).__new__(meta,name,bases,dic)
class M3(M1,M2):
.... pass
class C:
.... __metaclass__=M3
Called M1.__new__
Called M2.__new__
Moreover, one must be careful about metaclass conflicts: they can
be solved with this recipe: http://aspn.activestate.com/ASPN/Coo.../Recipe/204197
It is also convenient to give a look to http://www.python.org/2.3/mro.html
in order to understand the MRO.
David Mertz and myself wrote another paper on metaclasses that should
appear on IBM developerWorks in the near future (dunno when); it covers
some of the tricky points about metaclasses that were not discussed in http://www-106.ibm.com/developerwork.../l-pymeta.html
Thanks for your input
Stephan
HTH,
Michele
Michele Simionato wrote:
[...] Notice that 99% of times you don't need "elaborate inheritance schemes" but just a simple diamond. The caveat is that one should use cooperative methods; here is an example:
class M1(type): ... def __new__(meta,name,bases,dic): ... print "Called M1.__new__" ... return super(M1,meta).__new__(meta,name,bases,dic)
class M2(type): ... def __new__(meta,name,bases,dic): ... print "Called M2.__new__" ... return super(M2,meta).__new__(meta,name,bases,dic) class M3(M1,M2): ... pass class C:
... __metaclass__=M3 Called M1.__new__ Called M2.__new__
Wow. What I hadn't realized was that with the "super" builtin, both
"__new__" methods will be called on the instanciaton of C.
That is a really good one. (once in a while one really should read the
documentation :-)
Thanks
Stephan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mike C. Fletcher |
last post by:
Slides from my PyGTA presentation on Tuesday, focusing mostly on
why/where you would want to use meta-classes, are available in PDF format:
...
|
by: Santiago Aguiar |
last post by:
Im trying to use AOP techniques on python but, even if I understand
the idea behind metaclasses, I cant find a way to use AOP in a dynamic
way.
What im trying to do is to weave an aspect to a...
|
by: Michele Simionato |
last post by:
> Hello, my name is Skip and I am metaclass-unaware. I've been programming in
> Python for about ten years and I have yet to write a metaclass. At first I
> thought it was just that metaclasses...
|
by: Michael Sparks |
last post by:
Anyway...
At Europython Guido discussed with everyone the outstanding issue with
decorators and there was a clear majority in favour of having them, which
was good. From where I was sitting it...
|
by: Jean-François Doyon |
last post by:
Hello,
I'm using MetaClasses to create classes.
How do I make these new classes "globally" available?
I probably just have to assign them to something magic, but I can't seem to
figure out...
|
by: Hartmut Goebel |
last post by:
Hi,
I'm currently implementing a tool for auto-generating webforms from a
desription file. The output should become a php-script, an asp-script,
zope-formulator or such. Each if it may (or may...
|
by: Steffen Glückselig |
last post by:
Hello,
I've been experimenting with metaclasses a bit (even though I am quite
a newbie to python) and stumpled over the following problem in my code:
class Meta(type):
def __init__(cls, name,...
|
by: Alan Isaac |
last post by:
Forman's book is out of print.
Is there a good substitute?
Thanks,
Alan Isaac
|
by: Scott SA |
last post by:
On 4/23/08, Ivan Illarionov (ivan.illarionov@gmail.com) wrote:
As mentioned in another reply, my example was a poor representation of whatI was tryin to ask.
With that said, your reply is...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |