[Python-3000] auto-super()

Aahz aahz at pythoncraft.com
Tue Apr 18 19:27:03 CEST 2006


On Tue, Apr 18, 2006, Ian Bicking wrote:
> Aahz wrote:
>>On Tue, Apr 18, 2006, Thomas Wouters wrote:
>>
>>>- Compiler hackery involving a magical variable name, say '__class__'
>>>or '__CLASS__'. The compiler would treat this specially, probably
>>>stored in the class dict, and type() (which is, after all, called to
>>>actually create the class) would stuff the actual class object in
>>>there. It causes a reference cycle, but all newstyle classes already
>>>do that ;P The main issue is that __CLASS__ would be new magic. It
>>>wouldn't exist when the class body is executed, and it would be a
>>>special form of enclosed variable afterwards (it should be extracted
>>>from the class namespace, using a similar mechanism as closures.)
>>
>>It's not clear to me that this requires compiler hackery, just metaclass
>>hackery.  Am I missing something?
>>
>>Also, my idea is that self.super is a bound method/closure that already
>>contains a reference to the class.  This makes dynamic classes more
>>difficult in some ways, but anyone who wants to play those sorts of games
>>should expect to do some magic mangling.  If it's reasonably
>>well-documented, it wouldn't even be that difficult.
> 
> Well:
> 
> class A(object):
>     def whoami(self):
>         return 'A'
>     def dostuff(self):
>         print 'I (%s) am doing A stuff...' % (self.whoami())
> 
> class B(A):
>     def whoami(self):
>         return 'B-ish %s' % (self.super.whoami())
>     def dostuff(self):
>         print 'Some B stuff...'
>         self.super.dostuff()
> 
> class C(B):
>     def whoami(self):
>         return 'C'
>     def dostuff(self):
>         print 'Some C stuff...'
>         self.super.dostuff()
> 
> Reading this statically, I think it's clear how this should work.  So, 
> when C().dostuff() is called, we want to see:
> 
>   Some C stuff...
>   Some B stuff...
>   I (C) am doing A stuff...
> 
> When B().dostuff() is called, we want to see:
> 
>   Some B stuff...
>   I (B-ish A) am doing A stuff...
> 
> 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).
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"LL YR VWL R BLNG T S"


More information about the Python-3000 mailing list