need something like __init__ in classes __classinit__

Jens Gelhaar cuiod-tec at web.de
Sat Mar 9 07:40:18 EST 2002


Kevin Jacobs <jacobs at darwin.epbi.cwru.edu> wrote in message news:<a6ab93$ien$1 at eeyore.INS.cwru.edu>...
Hi Kevin,

when I use the Persistent class from ZODB like 

import ZODB
from Persistence import Persistent
# and then your stuff
>   class ClassInit(type):
>     def __new__(cls, name, bases, cls_dict):
>       # Transform __classinit__ into a static method
>       class_init = cls_dict.get('__classinit__',None)
>       if class_init:
>         cls_dict['__classinit__'] = staticmethod(class_init)
> 
>       # Call super instead of type.__new__ to allow cooperative metaclasses
>       return super(ClassInit,cls).__new__(cls, name, bases, cls_dict)
> 
>     def __init__(self, name, bases, cls_dict):
>       # Call __classinit__ if it exists and is callable.
>       # This cannot be done in __new__ since the class may not be completely
>       # initialized by its metaclasses.
>       if callable(getattr(self, '__classinit__',None)):
>         self.__classinit__(self)
> 
>   class ClassInitObject(object):
>     __metaclass__ = ClassInit
> 
>   class Persistent:
>     pass
> 
>   class primary(ClassInitObject,Persistent):
>     def __classinit__(cls):
>       print "In __classinit__:",cls
>     def __init__(self):
>       print "In __init__:",self
> 
I get the following

Traceback (most recent call last):
  File "D:\PROGRA~1\Python22\Pythonwin\pywin\framework\scriptutils.py",
line 301, in RunScript
    exec codeObject in __main__.__dict__
  File "E:\daten\Python\SELBST\meinpythontest.py", line 24, in ?
    class primary(ClassInitObject,Persistent):
  File "E:\daten\Python\SELBST\meinpythontest.py", line 12, in __new__
    return super(ClassInit,cls).__new__(cls, name, bases, cls_dict)
TypeError: metatype conflict among bases

I looks like a conflict between "type" and "extensionclass". 

Beside, for me it would be natural to have a hook during "compile
time" like __classinit__. Serveral years ago I worked with FORTH and
there was no difference between compile and runtime. It was easy to do
everything what can be done during compile time.

I will try to hack a little bit in the source. Thanks

Jens



More information about the Python-list mailing list