Metaclass' __init__ Does not Initialize

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • achan

    Metaclass' __init__ Does not Initialize

    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__(cl s, ClassName, BaseClass, NewDict)

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


    [color=blue][color=green][color=darkred]
    >>> x = CNewType()[/color][/color][/color]
    something
    something
    something[color=blue][color=green][color=darkred]
    >>> x.a[/color][/color][/color]
    Traceback (most recent call last):
    File "<interacti ve 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__(kl ass, name, base, newDict)

    class Test(object):
    __metaclass__: CMeta

    [color=blue][color=green][color=darkred]
    >>> a = Test()
    >>>[/color][/color][/color]



  • Stephan Diehl

    #2
    Re: Metaclass' __init__ Does not Initialize

    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:
    [color=blue]
    > 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__(cl s, ClassName, BaseClass, NewDict)
    >
    > class CNewType(object ):
    > __metaclass__ = CMeta
    > a = 'nothing'
    > b = 'nothing'
    > c = 'nothing'
    >
    >
    >[color=green][color=darkred]
    >>>> x = CNewType()[/color][/color]
    > something
    > something
    > something[color=green][color=darkred]
    >>>> x.a[/color][/color]
    > Traceback (most recent call last):
    > File "<interacti ve 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__(kl ass, name, base, newDict)
    >
    > class Test(object):
    > __metaclass__: CMeta
    >
    >[color=green][color=darkred]
    >>>> a = Test()
    >>>>[/color][/color][/color]

    Comment

    • Alex Martelli

      #3
      Re: Metaclass' __init__ Does not Initialize

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

      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

      Comment

      • Michele Simionato

        #4
        Re: Metaclass' __init__ Does not Initialize

        "achan" <anabell@sh163. net> wrote in message news:<mailman.6 49.1068611151.7 02.python-list@python.org >...[color=blue]
        > As I was trying to create a metaclass, I found out that __init__ defined in
        > it does not initializes at all <snip>[/color]

        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

        Comment

        Working...