[Python-Dev] metaclass insanity (original) (raw)

Michael Hudson mwh@python.net
30 Oct 2002 13:00:28 +0000


For moderately nefarious reasons[1] I've being trying to write a metaclass whose instances have writable bases. This in itself isn't so hard, but having assigments to bases "do the right thing" has eluded me, basically because I can't seem to affect the mro.

Here's what I have so far:

def base_getter(klass): return klass.priv_bases

def base_setter(klass, v): klass.priv_bases = v

class MetaMutableBases(type): def new(self, name, bases, ns): t = type.new(self, name, bases, ns) t.priv_bases = type.dict["bases"].get(self) return t bases = property(base_getter, base_setter) def mro(self): if hasattr(self, "priv_bases"): return type("noname", self.priv_bases, {}).mro else: return type.mro(self)

I've tried variations on the same, but haven't got anything working yet.

Anyone have any ideas? I think my brain's exploded.

And I realise this is in some sense more appropriate for c.l.py, but I think my odds of a good answer are vastly higher here.

Cheers, M.

[1] in http://groups.google.com/groups?selm=m2r8e99l0b.fsf%40python.net I posted a metaclass that attempts to automatically upgrade instances of its instances on redefinition. Ideally, this would also frob its subclasses so they now inherited from redefined class. I'm aware this can go wrong in various ways, but I think it would be pretty handy in a large range of situations.

This is not to say C++ = bad, Lisp = good. It's to say C++ = bad irrespective of everything else. -- Alain Picard, comp.lang.lisp