question: metaclass and C - a new general style

Jens Gelhaar cuiod-tec at web.de
Mon Nov 26 06:23:17 EST 2001


Hi,


I am working serveral years with Python, but I am new to the C stuff. I need an 
hint, to change the source code to get the following behaviour:
   When I compile a module with an class in it, I would like to call a special 
function: __metainit__ after the class has been compiled. The calling parameters 
should the be the class itself and the enviroment one step up. 
   class a:
      def __metainit__(<selfclass>, <one level up>)
           ...
In the case of two classes
   class a:
        def __metainit__(...):
             pass
        class b:
           def __metainit__(...): 
              pass
there should be a call to b with
       __metainit__(b,a)


The reason for this feature is something in between to metaclass concept and 
macros. You can go one step further an implement an eiffel style like "deffered" 
and check after creating an new class, that this class must define an special 
method or attribute. For me, I would be more helpfull metaclass concept in python 
and I can implement every style I like.


For the sake of simplicity lets assume
   class meta_a:
         _collect: []
         def __metainit__(<selfclass>,<one level up>):
            "store all classes with meta_a as predessor in collect"
            for name, object in <selfclass>.__dict__.items():
                if issubclass(meta_b,<selfclass>):
                   <selfclass>._collect.append(object)
   class meta_b:
         pass
   
   implementation


   class a(meta_a):
         class b_1(meta_b): pass
         class b_2(meta_b): pass
         class b_3(meta_c): pass
               
   result
   class a:
      _collect: [b_1,b_2,b_3]


   and would be the same as


   class a(meta_a):
         class b_1(meta_b): pass
         class b_2(meta_b): pass
         class b_3(meta_c): pass
         _collect: [b_1,b_2,b_3]


   but much more readable and efficient, because I do not have to type it 
manually and forget it once. 


The <one level up> I need in the case there are more levels then two.


Thanks


Jens
-- 
__________________________________________________________
News suchen, lesen, schreiben mit http://newsgroups.web.de



More information about the Python-list mailing list