[Python-3000] auto-super()
Ian Bicking
ianb at colorstudy.com
Tue Apr 18 19:41:15 CEST 2006
Aahz wrote:
>>But how can we do that without the function being bound to a class?
>>self.whoami() in A.dostuff needs to access the 'real' self. self.super
>>in B.dostuff should be super(B, self), even when self.__class__ is C.
>
>
> Hrm. I think I'm forgetting that Python doesn't have any dynamic
> scopes; that would make this reasonably straightforward (the original
> self.super() call would put something into the call stack that following
> calls to self.super() would retrieve and update).
I thought about that too -- since you can inspect stacks to get
something like dynamic scope, after all -- but super calls could also be
interleaved, or even done recursively. The class context is really
something like an argument to the function which is passed in
selectively. Recursive calls seem the trickiest in this case (after
trying a couple other things):
class A(object):
def sum(self, seq):
if not seq:
return 0
return 1 + seq[0] + self.sum(seq[1:])
class B(A):
def sum(self, seq):
return 2 * self.super.sum(seq)
b = B()
b.sum([1, 2])
That should call:
B.sum(b, [1, 2])
A.sum(b, [1, 2])
B.sum(b, [2])
A.sum(b, [2])
B.sum(b, [])
A.sum(b, [])
Achieving that through stack inspection doesn't seem possible.
--
Ian Bicking / ianb at colorstudy.com / http://blog.ianbicking.org
More information about the Python-3000
mailing list