Metaclass' __init__ Does not Initialize

Discussion in 'Python' started by achan, Nov 12, 2003.

  1. achan

    achan Guest

    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()
    >>>
    achan, Nov 12, 2003
    #1
    1. Advertising

  2. 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

    achan wrote:

    > 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()
    >>>>
    Stephan Diehl, Nov 12, 2003
    #2
    1. Advertising

  3. achan wrote:

    > 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
    Alex Martelli, Nov 12, 2003
    #3
  4. "achan" <> wrote in message news:<>...
    > 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
    Michele Simionato, Nov 12, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. achan
    Replies:
    2
    Views:
    289
    Alex Martelli
    Nov 13, 2003
  2. Steven Bethard
    Replies:
    2
    Views:
    455
    Steven Bethard
    Feb 16, 2005
  3. ironfroggy
    Replies:
    16
    Views:
    438
    Michele Simionato
    Jun 3, 2005
  4. Matthew Wilson
    Replies:
    1
    Views:
    618
  5. Steven D'Aprano

    Metaclass of a metaclass

    Steven D'Aprano, Jun 5, 2012, in forum: Python
    Replies:
    1
    Views:
    305
Loading...

Share This Page