[Python-3000] Fixing super anyone? (original) (raw)
Calvin Spealman ironfroggy at gmail.com
Tue Apr 24 16:11:21 CEST 2007
- Previous message: [Python-3000] Fixing super anyone?
- Next message: [Python-3000] Fixing super anyone?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
I must have miscopied then because it worked perfectly here. Yes, I meant to have the _superdesc defined inside the metaclass init, but thought I could pull it out to make it cleaner. I forgot it actually had to be there! Here is the metaclass that works.
class autosuper(type): def init(cls, name, bases, clsdict): class _superdesc(object): def get(self, obj, objcls): return super(cls, obj) cls.super = _superdesc()
On 4/24/07, Steven Bethard <steven.bethard at gmail.com> wrote:
On 4/23/07, Calvin Spealman <ironfroggy at gmail.com> wrote: > I will +1 on the self.super suggestion. Hey, its very doable and I > even whipped it up with a simple metaclass, so it would be a tiny > change to 'type' in order to actually implement it as a standard > feature. The demonstration is as follows: > > class superdesc(object): > def get(self, obj, objcls): > return super(cls, obj) > > class autosuper(type): > def init(cls, name, bases, clsdict): > cls.super = superdesc() > > class A(object): > metaclass = autosuper > x = 1 > > class B(A): > x = 2 > > assert B().super.x == 1
Does that really work? There's a typo in superdesc (I don't know where 'cls' comes from) but if you meant 'objcls', here's what I get:: >>> class superdesc(object): ... def get(self, obj, cls): ... return super(cls, obj) ... >>> class autosuper(type): ... def init(cls, name, bases, clsdict): ... cls.super = superdesc() ... >>> class A: ... metaclass = autosuper ... def f(self): ... print 'A' ... >>> class B(A): ... def f(self): ... print 'B' ... self.super.f() ... >>> class C(A): ... def f(self): ... print 'C' ... self.super.f() ... >>> class D(B, C): ... def f(self): ... print 'D' ... self.super.f() ... >>> D().f() D B B B ... Traceback (most recent call last): File "", line 1, in ... RuntimeError: maximum recursion depth exceeded STeVe -- I'm not in-sane. Indeed, I am so far out of sane that you appear a tiny blip on the distant coast of sanity. --- Bucky Katt, Get Fuzzy
-- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/
- Previous message: [Python-3000] Fixing super anyone?
- Next message: [Python-3000] Fixing super anyone?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]