Re: Creating new types and invoking super



2008/1/23, Guilherme Polo <ggpolo@xxxxxxxxx>:
2008/1/23, Arnaud Delobelle <arnodel@xxxxxxxxxxxxxx>:
On Jan 23, 8:55 pm, "Guilherme Polo" <ggp...@xxxxxxxxx> wrote:
Hello,

Hi
[...]
First I tried this:

def init(func):
def _init(inst):
super(inst.__class__, inst).__init__()
func(inst)

return _init

class A(object):
@init
def __init__(self): pass

This kind of approach can't work, you need to call super(A, obj).
super(type(obj), obj) is pointless, it defeats the whole purpose of
the mro!


Yeh, as shown in the next examples in the previous email. Using a
decorator for that doesn't sound useful at all, was just trying
something different ;) I will stick to the no-decorator option.

The only way I can think of would be to create a metaclass, but I
don't think it's worth it. super(A, obj).__init__() isn't that bad!


Metaclass doesn't apply here because metaclass is related to
class-construction. This is related to instance initialization, and
I'm creating the types as the user asks.

Oops, actually it can be done. But I am not going to stick to it, just
did a sample here that "solves" for a not so deep structure:

def init(func):
def _init(cls):
if hasattr(cls, "myclass"):
super(cls.myclass, cls).__init__()
else:
super(cls.__class__, cls).__init__()
func(cls)

return _init

class M(type):
def __new__(cls, classname, bases, classdict):
if not len(classdict):
if 'myclass' in bases[0].__dict__:
classdict['myclass'] = bases[0].__dict__['myclass']
else:
classdict['myclass'] = bases[0]


return type.__new__(cls, classname, bases, classdict)

class Y(object):
__metaclass__ = M

class X(Y):
@init
def __init__(self):
print "X"

X()
y = type("Z", (X, ), {})
w = type("W", (y, ), {})
y()
w()


--
Arnaud

--
http://mail.python.org/mailman/listinfo/python-list



--
-- Guilherme H. Polo Goncalves



--
-- Guilherme H. Polo Goncalves
.



Relevant Pages

  • Re: Creating new types and invoking super
    ... def _init: ... return _init ... This kind of approach can't work, you need to call super(A, obj). ... Metaclass doesn't apply here because metaclass is related to ...
    (comp.lang.python)
  • Re: What about a series type?
    ... On 6/7/07, Robert Dober wrote: ... > def initialize ... > @init = init ... puts s1.get_some ...
    (comp.lang.ruby)
  • Re: What about a series type?
    ... def initialize ... @init = init ... puts s1.get_some ...
    (comp.lang.ruby)
  • Re: main window in tkinter app
    ... It might have hit me if the fault was related to someting in baseclass.__initnot taking place, but the recursion loop didn't give me a clue. ... Any idea why failing to init the base class caused the loop? ... var=1 def __init__: pass def getval: return self.var ... As you'd logically expect, if you don't define a function in a derived class but call it ), it will call the method from the base class. ...
    (comp.lang.python)
  • pyopenGL: glutInit : TypeError: not a list
    ... I am starting to write this sample program in Python to use the GLUT ... def init(): ... def display(): ...
    (comp.lang.python)