This PEP started its life asPEP 367. Since it is now targetedfor Python 3000, it has been moved into the 3xxx space.
This PEP proposes syntactic sugar for use of thesuper type to automaticallyconstruct instances of the super type binding to the class that a method wasdefined in, and the instance (or class object for classmethods) that the methodis currently acting upon.
The premise of the new super usage suggested is as follows:
super().foo(1,2)
to replace the old:
super(Foo,self).foo(1,2)
The current usage of super requires an explicit passing of both the class andinstance it must operate from, requiring a breaking of the DRY (Don’t RepeatYourself) rule. This hinders any change in class name, and is often considereda wart by many.
Within the specification section, some special terminology will be used todistinguish similar and closely related concepts. “super class” will refer tothe actual builtin class named “super”. A “super instance” is simply aninstance of the super class, which is associated with another class andpossibly with an instance of that class.
The newsuper semantics are only available in Python 3.0.
Replacing the old usage of super, calls to the next class in the MRO (methodresolution order) can be made without explicitly passing the class object(although doing so will still be supported). Every functionwill have a cell named__class__ that contains the class object that thefunction is defined in.
The new syntax:
super()
is equivalent to:
super(__class__,<firstarg>)
where__class__ is the class that the method was defined in, and<firstarg> is the first parameter of the method (normallyselffor instance methods, andcls for class methods). For functionsdefined outside a class body,__class__ is not defined, and willresult in runtimeSystemError.
Whilesuper is not a reserved word, the parser recognizes the useofsuper in a method definition and only passes in the__class__ cell when this is found. Thus, calling a global aliasofsuper without arguments will not necessarily work.
The class object is taken from a cell named__class__.
super actually become a keyword?No. It is not necessary for super to become a keyword.
It was considered that it might be a problem that instantiating super instancesthe classic way, because calling it would lookup the __call__ attribute andthus try to perform an automatic super lookup to the next class in the MRO.However, this was found to be false, because calling an object only looks upthe __call__ method directly on the object’s type. The following example showsthis in action.
classA(object):def__call__(self):return'__call__'def__getattribute__(self,attr):ifattr=='__call__':returnlambda:'__getattribute__'a=A()asserta()=='__call__'asserta.__call__()=='__getattribute__'
In any case, this issue goes away entirely because classic calls tosuper(<class>,<instance>) are still supported with the same meaning.
Although its always attractive to just keep things how they are, people havesought a change in the usage of super calling for some time, and for goodreason, all mentioned previously.
The proposal adds a dynamic attribute lookup to the super type, which willautomatically determine the proper class and instance parameters. Each superattribute lookup identifies these parameters and performs the super lookup onthe instance, as the current super implementation does with the explicitinvocation of a super instance upon a class and instance.
This proposal relies on sys._getframe(), which is not appropriate for anythingexcept a prototype implementation.
The __super__ attribute is mentioned in this PEP in several places, and couldbe a candidate for the complete solution, actually using it explicitly insteadof any super usage directly. However, double-underscore names are usually aninternal detail, and attempted to be kept out of everyday code.
This solution only solves the problem of the type indication, does not handledifferently named super methods, and is explicit about the name of theinstance. It is less flexible without being able to enacted on other methodnames, in cases where that is needed. One use case this fails is where abase-class has a factory classmethod and a subclass has two factoryclassmethods,both of which needing to properly make super calls to the onein the base-class.
This variation actually eliminates the problems with locating the properinstance, and if any of the alternatives were pushed into the spotlight, Iwould want it to be this one.
There has been the proposal that directly callingsuper(*p,**kw) wouldbe equivalent to calling the method on thesuper object with the same nameas the method currently being executed i.e. the following two methods would beequivalent:
deff(self,*p,**kw):super.f(*p,**kw)
deff(self,*p,**kw):super(*p,**kw)
There is strong sentiment for and against this, but implementation and styleconcerns are obvious. Guido has suggested that this should be excluded fromthis PEP on the principle of KISS (Keep It Simple Stupid).
[1] Fixing super anyone? (https://mail.python.org/pipermail/python-3000/2007-April/006667.html)
[2] PEP 3130: Access to Module/Class/Function Currently Being Defined (this) (https://mail.python.org/pipermail/python-ideas/2007-April/000542.html)
This document has been placed in the public domain.
Source:https://github.com/python/peps/blob/main/peps/pep-3135.rst
Last modified:2025-02-01 08:55:40 GMT