metaclass.py

Go to the documentation of this file.
00001 from vyperlogix.misc import ObjectTypeName
00002 
00003 ##
00004 # Metaclass implementing cooperative methods. Works
00005 #     well for methods returning None, such as __init__
00006 class CooperativeAbstract(type):
00007     def __init__(cls,name,bases,dic):
00008         for meth in getattr(cls,'__cooperative__',[]): 
00009             setattr(cls,meth,cls.coop_method(meth,dic.get(meth)))
00010     ##
00011     # Calls both the superclass method and the class method (if the 
00012     #         class has an explicit method). Implemented via a closure
00013     def coop_method(cls,name,method): # method can be None
00014         def _(self,*args,**kw):
00015             getattr(super(cls,self),name)(*args,**kw) # call the supermethod
00016             if method: method(self,*args,**kw) # call the method
00017         return _
00018 
00019 class CooperativeBase(object):
00020     __metaclass__=CooperativeAbstract
00021     __cooperative__=['__init__']
00022     
00023 if (__name__ == '__main__'):
00024     from vyperlogix.misc import ObjectTypeName
00025     
00026     class _A(object):
00027         def __init__(self, *args):
00028             print '_A.__init__',args
00029     
00030     class A(CooperativeBase):
00031         def __init__(self, *args):
00032             print 'A.__init__',args
00033     
00034     class B1(CooperativeBase):
00035         def __init__(self, *args):
00036             print 'B1.__init__',args
00037 
00038         def test(self, *args):
00039             print 'B1.%s.__init__' % (ObjectTypeName.objectSignature(self)),args
00040             
00041     class B2(CooperativeBase):
00042         def __init__(self, *args):
00043             print 'B2.__init__',args
00044     
00045     class B(A, B1, B2, _A):
00046         def __init__(self, *args):
00047             print 'B.__init__',args
00048     
00049     class C(B):
00050         def __init__(self, *args):
00051             print 'C.__init__',args
00052             super(C,self).test(1,2,3)
00053     
00054     C()
00055 
00056 

© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved; If you reference this document or any part of this document you must use the citation verbatim (including the link) "© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved."

Notice: This source code contained in this document is NOT open source and is NOT being distributed as open source.

122,241 lines of code and growing...