Text of peps.python.org › pep-0367 as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

PEP 367 – New Super | peps.python.org

PEP 367 – New Super PEP 367 – New Super Author : Calvin Spealman <ironfroggy at gmail.com>, Tim Delaney <timothy.c.delaney at gmail.com> Status : Superseded Type : Standards Track Created : 28-Apr-2007 Python-Version : 2.6 Post-History : 28-Apr-2007 , 29-Apr-2007 , 29-Apr-2007 , 14-May-2007 Table of Contents Numbering Note Abstract Rationale Specification Open Issues Determining the class object to use Should super actually become a keyword?

Closed Issues super used with __call__ attributes Reference Implementation Alternative Proposals No Changes Dynamic attribute on super type super(__this_class__, self) self.__super__.foo(*args) super(self, *args) or __super__(self, *args) super.foo(self, *args) super or super() super(*p, **kw) History References Copyright Numbering Note This PEP has been renumbered to PEP 3135 . The text below is the last version submitted under the old number.

Abstract This PEP proposes syntactic sugar for use of the super type to automatically construct instances of the super type binding to the class that a method was defined in, and the instance (or class object for classmethods) that the method is 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 ) and the current __builtin__.super be aliased to __builtin__.__super__ (with __builtin__.super to be removed in Python 3.0).

It is further proposed that assignment to super become a SyntaxError , similar to the behaviour of None . Rationale The current usage of super requires an explicit passing of both the class and instance it must operate from, requiring a breaking of the DRY (Don’t Repeat Yourself) rule. This hinders any change in class name, and is often considered a wart by many. Specification Within the specification section, some special terminology will be used to distinguish similar and closely related concepts.

“super type” will refer to the actual builtin type named “super”. A “super instance” is simply an instance of the super type, which is associated with a class and possibly with an instance of that class. Because the new super semantics are not backwards compatible with Python 2.5, the new semantics will require a __future__ import: from __future__ import new_super The current __builtin__.super will be aliased to __builtin__.__super__ .

This will occur regardless of whether the new super semantics are active. It is not possible to simply rename __builtin__.super , as that would affect modules that do not use the new super semantics. In Python 3.0 it is proposed that the name __builtin__.super will be removed. Replacing the old usage of super, calls to the next class in the MRO (method resolution order) can be made without explicitly creating a super instance (although doing so will still be supported via __super__ ).

Every function will have an implicit local named super . This name behaves identically to a normal local, including use by inner functions via a cell, with the following exceptions: Assigning to the name super will raise a SyntaxError at compile time; Calling a static method or normal function that accesses the name super will raise a TypeError at runtime. Every function that uses the name super , or has an inner function that uses the name super , will include a preamble that performs the equivalent of: super = __builtin__ . __super__ ( < class > , < instance > ) where <class> is the class that the method was defined in, and <instance> is the first parameter of the method (normally self for instance methods, and cls for class methods).

For static methods and normal functions, <class> will be None , resulting in a TypeError being raised during the preamble. Note: The relationship between super and __super__ is similar to that between import and __import__ . Much of this was discussed in the thread of the python-dev list, “Fixing super anyone?” [1] . Open Issues Determining the class object to use The exact mechanism for associating the method with the defining class is not specified in this PEP, and should be chosen for maximum performance.

For CPython, it is suggested that the class instance be held in a C-level variable on the function object which is bound to one of NULL (not part of a class), Py_None (static method) or a class object (instance or class method). Should super actually become a keyword? With this proposal, super would become a keyword to the same extent that None is a keyword. It is possible that further restricting the super name may simplify implementation, however some are against the actual keyword-ization of super.

The simplest solution is often the correct solution and the simplest solution may well not be adding additional keywords to the language when they are not needed. Still, it may solve other open issues. Closed Issues super used with __call__ attributes It was considered that it might be a problem that instantiating super instances the classic way, because calling it would lookup the __call__ attribute and thus 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 up the __call__ method directly on the object’s type. The following example shows this in action. class A ( object ): def __call__ ( self ): return '__call__' def __getattribute__ ( self , attr ): if attr == '__call__' : return lambda : '__getattribute__' a = A () assert a () == '__call__' assert a . __call__ () == '__getattribute__' In any case, with the renaming of __builtin__.super to __builtin__.__super__ this issue goes away entirely.

Reference Implementation It is impossible to implement the above specification entirely in Python. This reference implementation has the following differences to the specification: New super semantics are implemented using bytecode hacking. Assignment to super is not a SyntaxError . Also see point #4. Classes must either use the metaclass autosuper_meta or inherit from the base class autosuper to acquire the new super semantics. super is not an implicit local variable.

In particular, for inner functions to be able to use the super instance, there must be an assignment of the form super = super in the method. The reference implementation assumes that it is being run on Python 2.5+. #!/usr/bin/env python # # autosuper.py from array import array import dis import new import types import __builtin__ __builtin__ . __super__ = __builtin__ . super del __builtin__ . super # We need these for modifying bytecode from opcode import opmap , HAVE_ARGUMENT , EXTENDED_ARG LOAD_GLOBAL = opmap [ 'LOAD_GLOBAL' ] LOAD_NAME = opmap [ 'LOAD_NAME' ] LOAD_CONST = opmap [ 'LOAD_CONST' ] LOAD_FAST = opmap [ 'LOAD_FAST' ] LOAD_ATTR = opmap [ 'LOAD_ATTR' ] STORE_FAST = opmap [ 'STORE_FAST' ] LOAD_DEREF = opmap [ 'LOAD_DEREF' ] STORE_DEREF = opmap [ 'STORE_DEREF' ] CALL_FUNCTION = opmap [ 'CALL_FUNCTION' ] STORE_GLOBAL = opmap [ 'STORE_GLOBAL' ] DUP_TOP = opmap [ 'DUP_TOP' ] POP_TOP = opmap [ 'POP_TOP' ] NOP = opmap [ 'NOP' ] JUMP_FORWARD = opmap [ 'JUMP_FORWARD' ] ABSOLUTE_TARGET = dis . hasjabs def _oparg ( code , opcode_pos ): return code [ opcode_pos + 1 ] + ( code [ opcode_pos + 2 ] << 8 ) def _bind_autosuper ( func , cls ): co = func . func_code name = func . func_name newcode = array ( 'B' , co . co_code ) codelen = len ( newcode ) newconsts = list ( co . co_consts ) newvarnames = list ( co . co_varnames ) # Check if the global 'super' keyword is already present try : sn_pos = list ( co . co_names ) . index ( 'super' ) except ValueError : sn_pos = None # Check if the varname 'super' keyword is already present try : sv_pos = newvarnames . index ( 'super' ) except ValueError : sv_pos = None # Check if the cellvar 'super' keyword is already present try : sc_pos = list ( co . co_cellvars ) . index ( 'super' ) except ValueError : sc_pos = None # If 'super' isn't used anywhere in the function, we don't have anything to do if sn_p

The rest of this page is on the live site.