Metaclass' __init__ Does not Initialize

achan anabell at sh163.net
Tue Nov 11 23:30:22 EST 2003


As I was trying to create a metaclass, I found out that __init__ defined in
it does not initializes at all:

class CMeta(type):
    def __new__(cls, ClassName, BaseClass, ClassDict):
        def __init__(self):
            for k in self.__slots__:
                k = 'something'
        NewDict = {'__slots__': [], '__init__': __init__}
        for k in ClassDict:
            if k.startswith('__') and k.endswith('__'):
                if k in NewDict:
                    warnings.warn("Can't set attr %r in bunch-class %r" %
(k, ClassName))
                else:
                    NewDict[k] = ClassDict[k]
            else:
                    NewDict['__slots__'].append(k)
        return type.__new__(cls, ClassName, BaseClass, NewDict)

class CNewType(object):
    __metaclass__   = CMeta
    a = 'nothing'
    b = 'nothing'
    c = 'nothing'



>>> x = CNewType()
something
something
something
>>> x.a
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: a          ------------->>>  Non-existent!






class CMeta(type):
	def __new__(klass, name, base, dict):
	    	def __init__(self):
			for s in seq:
				s = 123
				print s
		newDict = {'__init__': __init__, seq = []}
		return type.__new__(klass, name, base, newDict)

class Test(object):
	__metaclass__: CMeta


>>> a = Test()
>>>







More information about the Python-list mailing list