[Python-3000] Pre-pre PEP for 'super' keyword
tcdelaney at optusnet.com.au
Sun Apr 29 22:44:45 CEST 2007
I've been intending to write up a PEP for fixing super, but I haven't had
time to get to it. I've worked out the semantics I'm going to propose
though. I've attached an implementation of those semantics as far as I've
been able to get them so far.
I think this could be targeted at 2.6, with a __future__ import.
Basically, the proposals are:
1. 'super' becomes a keyword, that returns a super object for the instance
method currently being executed.
2. 'super' cannot be rebound, but the object produced can be assigned to
other names i.e.
super = None # SyntaxError, or UnrebindableError (see #7 below)
name = super # OK
This allows passing the super object to other functions.
3. 'super' is a cell variable i.e. inner functions can use it, and will get
the super object of the outer method.
4. super objects are callable, and calling them will execute the super
method with the same name as the instance method currently being executed.
Lookup of this method occurs when the instance method is entered.
super() # Calls A.f(self)
If you want name lookup to occur at the time of the call, you can explicitly
specify the method name (just like with any other super attribute):
super.f() # Calls A.f(self)
5. The repr of a super object will include the current class, the instance
type and the bound method that will be used when called (if present) or an
<super: <class 'E'>, <F object>, <bound method B.f of <__main__.F object at
<super: <class 'A'>, <F object>, <AttributeError: 'super' object has no
6. Methods will have an attribute holding the class where the unbound method
was created. This attribute is available on both unbound and bound methods.
In the attached code I've called it func_class, but it really should have an
im_prefix (can't be im_class though). This is used to make the repr of a
bound method actually reflect which class it comes from.
7. Code objects gain a tuple of unrebindable names, which can be local
variables, cell variables, etc. For instance methods and class methods, this
would initially contain two names - 'super' and the name of the first
parameter to the method (normally 'self' or 'cls). Attempting to rebind
these names would throw an UnrebindableError (maybe a subclass of
SyntaxError). It would be good if these could actually be detected at
Differences in the attached code are:
1. You need to inherit from 'autosuper' (or use the _autosuper metaclass).
2. 'super' is not a keyword.
3. Rebinding 'super' will not throw an exception, but such rebinding will be
ignored. Also see the next point.
4. It is *necessary* to rebind 'super' in the method in order to create a
cell for use in inner classes. The recommended way in the attached code is:
super = super
The 'super = super' line above would normally throw an UnboundLocalError,
but the bytecode hacking I've done causes it to work.
Comments before I start writing up the PEP? Or if anyone has a PEP partially
written, do you want to incorporate any of the above into it?
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 13032 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-3000/attachments/20070430/6e412e89/attachment-0001.obj
More information about the Python-3000