[Python-3000] Fixing super anyone? (original) (raw)

Tim Delaney tcdelaney at optusnet.com.au
Wed Apr 25 03:13:05 CEST 2007


I've been off sick from work for over a week - I've been following this discussion, but now I think I've got something which matches all the criteria we've been discussing, so I've changed my subscription address temporarily to my home one.

It involves bytecode hacking for now, as well as a metaclass. The code is attached. It's based partially off my 'self.super' recipe, but doesn't need to use sys._getframe().

Recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286195

Basically, the metaclass modifies the bytecode to include some setup code to any function that uses 'super'. The setup code generates the correct 'super' object, and stores it as a local variable. All uses of LOAD_GLOBAL(super) are changed to LOAD_FAST(super).

The one thing I would like to add is something I've got in my 'self.super' recipe - I've got my own 'super' object that is callable. Calling it invokes the super method of the current method i.e. if you've got the following code:

class A(autosuper):
    def f(self):
        pass

class B(A):
    def f(self):
        super()

it would be functionally identical to the following:

class B(A):
    def f(self):
        super.f()

This could be easily implemented by having the 'super' constructor take a 'name' parameter (which would be passed the name of current method in the setup code). Then when super() is called, it would perform the name lookup.

Tim Delaney -------------- next part -------------- A non-text attachment was scrubbed... Name: autosuper.py Type: application/octet-stream Size: 5330 bytes Desc: not available Url : http://mail.python.org/pipermail/python-3000/attachments/20070425/6b9124d7/attachment-0001.obj



More information about the Python-3000 mailing list