[Python-Dev] Classes and Metaclasses in Smalltalk
Greg Ward
gward@python.net
Wed, 2 May 2001 08:57:41 -0400
On 02 May 2001, Guido van Rossum said:
> Yes, I can see how to write super() using current tools (or 1.5.2
> even). The problem is that this makes super calls even more wordy
> than they already are! I can't think of anything that wouldn't
> require compiler support though.
I was just doing some gedanken with various ways to spell "super", and I
think my favourite is the same as Java's (as I remember it):
class MyClass (BaseClass):
def foo (self, arg1, arg2):
super.foo(arg1, arg2)
Since I don't know much about Python's guts, I can't say how
implementable this is, but I like the spelling. The semantics would be
something like this (with adjustments to the reality of Python's guts):
* 'super' is a magic object that only makes sense inside a 'def'
inside a 'class' (at least for now; perhaps it could be generalized
to work at class scope as well as method scope, but let's keep
it simple)
* super's notional __getattr__() does something like this:
- peek at the calling stack frame and fetch the calling function
(MyClass.foo) and the first argument to that function (self)
- [is this possible?] ensure that calling_function is a bound
method, and that it's bound to the self object we just plucked
from the stack; raise a "misuse of super object" exception if not
- walk the superclass tree starting at self.__class__.__bases__
(ie. skip self's class), looking for an object with the name
passed to this __getattr__() call -- 'foo'
- when found, return it
- if not found, raise AttributeError
The ability to peek at the calling stack frame is essential to this
scheme, in order to fetch the "current object" (self) without needing to
have it explicitly passed. Is this as bothersome from C as it is from
Python?
Greg
--
Greg Ward - nerd gward@python.net
http://starship.python.net/~gward/
In space, no one can hear you fart.