Metaclass' __init__ Does not Initialize

A

achan

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'


something
something
something
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

 
S

Stephan Diehl

The __init__ runs just fine.
You just did a tiny little programming error
The line that says
"k = 'something'"
must be replaced by
"setattr(self,k,'something')"

Stephan
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'


something
something
something
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

Alex Martelli

achan said:
As I was trying to create a metaclass, I found out that __init__ defined
in it does not initializes at all: ...
def __init__(self):
for k in self.__slots__:
k = 'something'

Haven't looked at the rest of your code, but this part can't possibly
be right -- you're just rebinding local variable k over and over,
uselessly. Perhaps you want the loop's body to be
setattr(self, k, 'something')
....?


Alex
 
M

Michele Simionato

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

1. Please, test your attempts before posting, it would be helpful for
you as for the people who try to understand you.

2. Are you sure you want __slots__? 99.9% of times __slot__ are not needed,
I never use them, and I would rather NOT have this feature available
from Python (yes if available from C) since it is an endless cause
of confusion.

3. Here is an example of a working metaclass with an __init__ as you
want:

class CMeta(type):
def __new__(mcl, name, bases, dic):
def __init__(self):
for s in self.seq:
print s
newDict = {'__init__': __init__, 'seq' : [123]}
return super(CMeta,mcl).__new__(mcl, name, bases, newDict)

class Test(object):
__metaclass__= CMeta

a = Test() #=> 123


You may figure out for yourself your mistakes.

Michele Simionato
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top