Using metaclasses to make super more beatiful

Gerrit Holl gerrit at nl.linux.org
Thu Jun 5 12:50:14 EDT 2003


Hi,

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?

And, is it convention not to use Capitalization for metaclasses?
In three examples, Guido doensn't do it in his paper[0]!

[0] http://www.python.org/2.2.3/descrintro.html#metaclasses

yours,
Gerrit.

-- 
39. He may, however, assign a field, garden, or house which he has
bought, and holds as property, to his wife or daughter or give it for
debt.
        -- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list