This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: First steps towards new super (PEP 3135)
Type: Stage:
Components: Interpreter Core Versions: Python 3.0
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: gvanrossum
Priority: normal Keywords: patch

Created on 2007-05-29 02:30 by gvanrossum, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
super.diff gvanrossum, 2007-05-29 02:30 First version
sup.py gvanrossum, 2007-05-29 02:31 Sample code
super.diff gvanrossum, 2007-05-29 03:22 New patch, C.f.im_class now returns B
sup.py gvanrossum, 2007-05-29 03:23 New demo
super.diff gvanrossum, 2007-05-29 06:08 New patch, includes unit test.
super.diff gvanrossum, 2007-05-31 11:24 Includes grammar and compiler modifications
super.diff gvanrossum, 2007-05-31 11:45 Corrected version (make ste_super bitfield unsigned)
super2.diff gvanrossum, 2007-06-06 18:53 Different tack, using __class__ cell variable
super2.diff gvanrossum, 2007-06-06 20:01 Support super() for super(__class__, self/cls)
super2.diff gvanrossum, 2007-06-06 20:57 Fixe test_scope.py
super2.diff gvanrossum, 2007-06-06 21:00 Add test_super.py
Messages (13)
msg52692 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-05-29 02:30
A half-assed start of an implementation for super.

This is lacking parser support; instead, for now, you have to have a keyword-only argument named super, e.g.

def foo(self, arg, *, super):
    return super.foo(arg) + 1

Note that the first version changes method lookup for bound method lookup but not yet for unbound method lookup.  E.g. if f is defined in class B, and class C derives from B, C().f returns a bound method object whose im_class attribute is set to B (as opposed to C as it used to be) but C.f returns an unbound method object with im_class set to C.  (I hope to this in a later version.)
msg52693 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-05-29 02:31
Here is sup.py, a small demo of how this is supposed to work.
File Added: sup.py
msg52694 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-05-29 03:22
Here's a new version of the patch where C.f.im_class (in the above example) correctly returns B.
File Added: super.diff
msg52695 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-05-29 03:23
And a new sup.py example demonstrating that the new patch works.
File Added: sup.py
msg52696 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-05-29 06:08
Here's a new version.  Forget about sup.py, the demos are now in Lib/test/test_super.py.

Only a few tests fail due to the semantic change (about six files have 1-2 failures each).  I think this is acceptable.

File Added: super.diff
msg52697 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-05-31 11:24
Here's a version that modifies the grammar so 'super' is a keyword. The compiler automatically inserts a 'super' keyword-only argument into a function's signature when the 'super' keyword is used in its body.  It works!

This requires the p3yk branch at -r55692 or newer.
File Added: super.diff
msg52698 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-05-31 11:45
Oops, a last-minute change didn't work out.  This version is better.
File Added: super.diff
msg52699 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-05-31 23:00
This breaks all class methods (as Phillip Eby pointed out):

class C:
   @classmethod
   def cm(cls): return cls.__name__
class D(C): pass
print(D.cm(), D().cm())

This should print "D D" but with the patch it prints "C C".
msg52700 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-06-06 18:53
I'm starting a new series of patches, super2.diff. These use a completely different tack, inserting a cell variable __class__ into the class shared by all the methods and initialized to the class after it's constructed.  Right now, this allows us to write super(__class__, self).method() -- I want to evolve it so that eventually you can write super.method() and super will do the lookup of __class__ and the first argument itself.

There's one failing test, testFreeVarInMethod in test_scope.py.  This seems significant but I want to upload a checkpoint first.
File Added: super2.diff
msg52701 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-06-06 20:01
Here's a better version that lets you use super() as a shorthand for super(__class__, <firstarg>).  test_scope.py still fails though.
File Added: super2.diff
msg52702 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-06-06 20:57
OK, here's today's final version (I hope). This fixes test_scope.py (wasn't too hard after all) and updates the docstring for super().  Question to all: is writing super() acceptable?
File Added: super2.diff
msg52703 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-06-06 21:00
Sorry, forgot to add test_super.py to the patch.
File Added: super2.diff
msg52704 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-06-11 21:20
I've checked this into the p3yk branch now.  Tim Delaney will update the PEP.  Note that the PEP number has changed; it's 3135 now.
History
Date User Action Args
2022-04-11 14:56:24adminsetgithub: 45011
2008-01-06 22:29:46adminsetkeywords: - py3k
versions: + Python 3.0
2007-05-29 02:30:08gvanrossumcreate