Text of peps.python.org › pep-0246 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 246 – Object Adaptation | peps.python.org

PEP 246 – Object Adaptation PEP 246 – Object Adaptation Author : Alex Martelli <aleaxit at gmail.com>, Clark C. Evans <cce at clarkevans.com> Status : Rejected Type : Standards Track Created : 21-Mar-2001 Python-Version : 2.5 Post-History : 29-Mar-2001, 10-Jan-2005 Table of Contents Rejection Notice Abstract Motivation Requirements Specification Intended Use Guido’s “Optional Static Typing: Stop the Flames” Blog Entry Reference Implementation and Test Cases Relationship To Microsoft’s QueryInterface Questions and Answers Backwards Compatibility Credits References and Footnotes Copyright Rejection Notice I’m rejecting this PEP.

Something much better is about to happen; it’s too early to say exactly what, but it’s not going to resemble the proposal in this PEP too closely so it’s better to start a new PEP. GvR. Abstract This proposal puts forth an extensible cooperative mechanism for the adaptation of an incoming object to a context which expects an object supporting a specific protocol (say a specific type, class, or interface). This proposal provides a built-in “adapt” function that, for any object X and any protocol Y, can be used to ask the Python environment for a version of X compliant with Y.

Behind the scenes, the mechanism asks object X: “Are you now, or do you know how to wrap yourself to provide, a supporter of protocol Y?”. And, if this request fails, the function then asks protocol Y: “Does object X support you, or do you know how to wrap it to obtain such a supporter?” This duality is important, because protocols can be developed after objects are, or vice-versa, and this PEP lets either case be supported non-invasively with regard to the pre-existing component[s].

Lastly, if neither the object nor the protocol know about each other, the mechanism may check a registry of adapter factories, where callables able to adapt certain objects to certain protocols can be registered dynamically. This part of the proposal is optional: the same effect could be obtained by ensuring that certain kinds of protocols and/or objects can accept dynamic registration of adapter factories, for example via suitable custom metaclasses.

However, this optional part allows adaptation to be made more flexible and powerful in a way that is not invasive to either protocols or other objects, thereby gaining for adaptation much the same kind of advantage that Python standard library’s “copy_reg” module offers for serialization and persistence. This proposal does not specifically constrain what a protocol is , what “compliance to a protocol” exactly means , nor what precisely a wrapper is supposed to do.

These omissions are intended to leave this proposal compatible with both existing categories of protocols, such as the existing system of type and classes, as well as the many concepts for “interfaces” as such which have been proposed or implemented for Python, such as the one in PEP 245 , the one in Zope3 [2] , or the ones discussed in the BDFL’s Artima blog in late 2004 and early 2005 [3] . However, some reflections on these subjects, intended to be suggestive and not normative, are also included.

Motivation Currently there is no standardized mechanism in Python for checking if an object supports a particular protocol. Typically, existence of certain methods, particularly special methods such as __getitem__ , is used as an indicator of support for a particular protocol. This technique works well for a few specific protocols blessed by the BDFL (Benevolent Dictator for Life). The same can be said for the alternative technique based on checking ‘isinstance’ (the built-in class “basestring” exists specifically to let you use ‘isinstance’ to check if an object “is a [built-in] string”).

Neither approach is easily and generally extensible to other protocols, defined by applications and third party frameworks, outside of the standard Python core. Even more important than checking if an object already supports a given protocol can be the task of obtaining a suitable adapter (wrapper or proxy) for the object, if the support is not already there. For example, a string does not support the file protocol, but you can wrap it into a StringIO instance to obtain an object which does support that protocol and gets its data from the string it wraps; that way, you can pass the string (suitably wrapped) to subsystems which require as their arguments objects that are readable as files.

Unfortunately, there is currently no general, standardized way to automate this extremely important kind of “adaptation by wrapping” operations. Typically, today, when you pass objects to a context expecting a particular protocol, either the object knows about the context and provides its own wrapper or the context knows about the object and wraps it appropriately. The difficulty with these approaches is that such adaptations are one-offs, are not centralized in a single place of the users code, and are not executed with a common technique, etc.

This lack of standardization increases code duplication with the same adapter occurring in more than one place or it encourages classes to be re-written instead of adapted. In either case, maintainability suffers. It would be very nice to have a standard function that can be called upon to verify an object’s compliance with a particular protocol and provide for a wrapper if one is readily available – all without having to hunt through each library’s documentation for the incantation appropriate to that particular, specific case.

Requirements When considering an object’s compliance with a protocol, there are several cases to be examined: When the protocol is a type or class, and the object has exactly that type or is an instance of exactly that class (not a subclass). In this case, compliance is automatic. When the object knows about the protocol, and either considers itself compliant, or knows how to wrap itself suitably. When the protocol knows about the object, and either the object already complies or the protocol knows how to suitably wrap the object.

When the protocol is a type or class, and the object is a member of a subclass. This is distinct from the first case (a) above, since inheritance (unfortunately) does not necessarily imply substitutability, and thus must be handled carefully. When the context knows about the object and the protocol and knows how to adapt the object so that the required protocol is satisfied. This could use an adapter registry or similar approaches.

The fourth case above is subtle. A break of substitutability can occur when a subclass changes a method’s signature, or restricts the domains accepted for a method’s argument (“co-variance” on arguments types), or extends the co-domain to include return values which the base class may never produce (“contra-variance” on return types). While compliance based on class inheritance should be automatic, this proposal allows an object to signal that it is not compliant with a base class protocol.

If Python gains some standard “official” mechanism for interfaces, however, then the “fast-path” case (a) can and should be extended to the protocol being an interface, and the object an instance of a type or class claiming compliance with that interface. For example, if the “interface” keyword discussed in [3] is adopted into Python, the “fast path” of case (a) could be used, since instantiable classes implementing an interface would not be allowed to break substitutability.

Specification This proposal introduces a new built-in function, adapt() , which is the basis for supporting these requirements. The adapt() function has three parameters: obj , the object to be adapted protocol , the protocol requested of the object alternate , an optional object to return if the object could not be adapted A successful result of the adapt() function returns either the object passed obj , if the object is al

The rest of this page is on the live site.