Using metaclasses to make super more beatiful

Just just at xs4all.nl
Thu Jun 5 14:01:18 EDT 2003


In article <mailman.1054831871.3625.python-list at python.org>,
 Gerrit Holl <gerrit at nl.linux.org> wrote:

> as demonstrated by Guido's paper[0], a relatively simple metaclass
> can make the use of super a lot more beatiful:
> 
>  97 >>> class autosuper(type):
>  97 ...  def __init__(cls, name, bases, dict):
>  97 ...   super(autosuper, cls).__init__(name, bases, dict)
>  97 ...   setattr(cls, "super", super(cls))
>  97 ...
>  98 >>> class A:
>  98 ...  __metaclass__ = autosuper
>  98 ...  def f(self): return "A"
>  98 ...
> 100 >>> class B(A):
> 100 ...  def f(self):
> 100 ...   return self.super.f()
> 100 ...
> 101 >>> B()
> <__main__.B object at 0x403b3c6c>
> 102 >>> B().f()
> 'A'
> 
> Why doesn't type include the behaviour of autosuper by default,
> so that each class can use self.super or self.__super__ so that
> using super is easier?

Tip: create a class C, derived from B, then call f() on an instance of 
C, and see what happens <wink>.

Just




More information about the Python-list mailing list