← 返回首页
PEP 232 – Function Attributes | peps.python.org Following system colour scheme Selected dark colour scheme Selected light colour scheme

Python Enhancement Proposals

Toggle light / dark / auto colour theme PEP 232 – Function Attributes

PEP 232 – Function Attributes

Author: Barry Warsaw <barry at python.org> Status: Final Type: Standards Track Created: 02-Dec-2000 Python-Version: 2.1 Post-History: 20-Feb-2001 Table of Contents

Introduction

This PEP describes an extension to Python, adding attribute dictionaries to functions and methods. This PEP tracks the status and ownership of this feature. It contains a description of the feature and outlines changes necessary to support the feature. This PEP summarizes discussions held in mailing list forums, and provides URLs for further information, where appropriate. The CVS revision history of this file contains the definitive historical record.

Background

Functions already have a number of attributes, some of which are writable, e.g. func_doc, a.k.a. func.__doc__. func_doc has the interesting property that there is special syntax in function (and method) definitions for implicitly setting the attribute. This convenience has been exploited over and over again, overloading docstrings with additional semantics.

For example, John Aycock has written a system where docstrings are used to define parsing rules. [1] Zope’s ZPublisher ORB [2] uses docstrings to signal publishable methods, i.e. methods that can be called through the web.

The problem with this approach is that the overloaded semantics may conflict with each other. For example, if we wanted to add a doctest unit test to a Zope method that should not be publishable through the web.

Proposal

This proposal adds a new dictionary to function objects, called func_dict (a.k.a. __dict__). This dictionary can be set and get using ordinary attribute set and get syntax.

Methods also gain getter syntax, and they currently access the attribute through the dictionary of the underlying function object. It is not possible to set attributes on bound or unbound methods, except by doing so explicitly on the underlying function object. See the Future Directions discussion below for approaches in subsequent versions of Python.

A function object’s __dict__ can also be set, but only to a dictionary object. Deleting a function’s __dict__, or setting it to anything other than a concrete dictionary object results in a TypeError. If no function attributes have ever been set, the function’s __dict__ will be empty.

Examples

Here are some examples of what you can do with this feature.

def a(): pass a.publish = 1 a.unittest = '''...''' if a.publish: print a() if hasattr(a, 'unittest'): testframework.execute(a.unittest) class C: def a(self): 'just a docstring' a.publish = 1 c = C() if c.a.publish: publish(c.a())

Other Uses

Paul Prescod enumerated a bunch of other uses on the python-dev thread.

Future Directions

Here are a number of future directions to consider. Any adoption of these ideas would require a new PEP, which referenced this one, and would have to be targeted at a Python version subsequent to the 2.1 release.

Dissenting Opinion

When this was discussed on the python-dev mailing list in April 2000, a number of dissenting opinions were voiced. For completeness, the discussion thread starts on python-dev.

The dissenting arguments appear to fall under the following categories:

Countering some of these arguments is the observation that with vanilla Python 2.0, __doc__ can in fact be set to any type of object, so some semblance of writable function attributes are already feasible. But that approach is yet another corruption of __doc__.

And while it is of course possible to add mappings to class objects (or in the case of function attributes, to the function’s module), it is more difficult and less obvious how to extract the attribute values for inspection.

Finally, it may be desirable to add syntactic support, much the same way that __doc__ syntactic support exists. This can be considered separately from the ability to actually set and get function attributes.

Reference Implementation

This PEP has been accepted and the implementation has been integrated into Python 2.1.

References

[1] Aycock, “Compiling Little Languages in Python”, https://legacy.python.org/workshops/1998-11/proceedings/papers/aycock-little/aycock-little.html [2] https://web.archive.org/web/20010307022153/http://classic.zope.org:8080/Documentation/Reference/ORB [3] Hudson, Michael, SourceForge patch implementing this syntax, https://web.archive.org/web/20010901050535/http://sourceforge.net/tracker/index.php?func=detail&aid=403441&group_id=5470&atid=305470

Copyright

This document has been placed in the public domain.

Contents


Page Source (GitHub)

Source: https://github.com/python/peps/blob/main/peps/pep-0232.rst

Last modified: 2025-02-01 08:59:27 UTC