reSTify PEP 363 (#322)

This commit is contained in:
Huang Huang 2017-08-12 02:41:01 +08:00 committed by Brett Cannon
parent e889fa82a5
commit 7f68262609
1 changed files with 167 additions and 158 deletions

View File

@ -5,22 +5,23 @@ Last-Modified: $Date$
Author: Ben North <ben@redfrontdoor.org> Author: Ben North <ben@redfrontdoor.org>
Status: Rejected Status: Rejected
Type: Standards Track Type: Standards Track
Content-Type: text/plain Content-Type: text/x-rst
Created: 29-Jan-2007 Created: 29-Jan-2007
Post-History: 12-Feb-2007 Post-History: 12-Feb-2007
Abstract Abstract
========
Dynamic attribute access is currently possible using the "getattr" Dynamic attribute access is currently possible using the "getattr"
and "setattr" builtins. The present PEP suggests a new syntax to and "setattr" builtins. The present PEP suggests a new syntax to
make such access easier, allowing the coder for example to write make such access easier, allowing the coder for example to write::
x.('foo_%d' % n) += 1 x.('foo_%d' % n) += 1
z = y.('foo_%d' % n).('bar_%s' % s) z = y.('foo_%d' % n).('bar_%s' % s)
instead of instead of::
attr_name = 'foo_%d' % n attr_name = 'foo_%d' % n
setattr(x, attr_name, getattr(x, attr_name) + 1) setattr(x, attr_name, getattr(x, attr_name) + 1)
@ -29,9 +30,10 @@ Abstract
Rationale Rationale
=========
Dictionary access and indexing both have a friendly invocation Dictionary access and indexing both have a friendly invocation
syntax: instead of x.__getitem__(12) the coder can write x[12]. syntax: instead of ``x.__getitem__(12)`` the coder can write ``x[12]``.
This also allows the use of subscripted elements in an augmented This also allows the use of subscripted elements in an augmented
assignment, as in "x[12] += 1". The present proposal brings this assignment, as in "x[12] += 1". The present proposal brings this
ease-of-use to dynamic attribute access too. ease-of-use to dynamic attribute access too.
@ -39,20 +41,20 @@ Rationale
Attribute access is currently possible in two ways: Attribute access is currently possible in two ways:
* When the attribute name is known at code-writing time, the * When the attribute name is known at code-writing time, the
".NAME" trailer can be used, as in ".NAME" trailer can be used, as in::
x.foo = 42 x.foo = 42
y.bar += 100 y.bar += 100
* When the attribute name is computed dynamically at run-time, the * When the attribute name is computed dynamically at run-time, the
"getattr" and "setattr" builtins must be used: "getattr" and "setattr" builtins must be used::
x = getattr(y, 'foo_%d' % n) x = getattr(y, 'foo_%d' % n)
setattr(z, 'bar_%s' % s, 99) setattr(z, 'bar_%s' % s, 99)
The "getattr" builtin also allows the coder to specify a default The "getattr" builtin also allows the coder to specify a default
value to be returned in the event that the object does not have value to be returned in the event that the object does not have
an attribute of the given name: an attribute of the given name::
x = getattr(y, 'foo_%d' % n, 0) x = getattr(y, 'foo_%d' % n, 0)
@ -60,7 +62,7 @@ Rationale
"x.(expr)" --- with examples given in the Abstract above. "x.(expr)" --- with examples given in the Abstract above.
(The new syntax could also allow the provision of a default value in (The new syntax could also allow the provision of a default value in
the "get" case, as in: the "get" case, as in::
x = y.('foo_%d' % n, None) x = y.('foo_%d' % n, None)
@ -69,20 +71,21 @@ Rationale
"Discussion" section below includes opinions specifically on the "Discussion" section below includes opinions specifically on the
2-argument extension.) 2-argument extension.)
Finally, the new syntax can be used with the "del" statement, as in Finally, the new syntax can be used with the "del" statement, as in::
del x.(attr_name) del x.(attr_name)
Impact On Existing Code Impact On Existing Code
=======================
The proposed new syntax is not currently valid, so no existing The proposed new syntax is not currently valid, so no existing
well-formed programs have their meaning altered by this proposal. well-formed programs have their meaning altered by this proposal.
Across all "*.py" files in the 2.5 distribution, there are around Across all "\*.py" files in the 2.5 distribution, there are around
600 uses of "getattr", "setattr" or "delattr". They break down as 600 uses of "getattr", "setattr" or "delattr". They break down as
follows (figures have some room for error because they were follows (figures have some room for error because they were
arrived at by partially-manual inspection): arrived at by partially-manual inspection)::
c.300 uses of plain "getattr(x, attr_name)", which could be c.300 uses of plain "getattr(x, attr_name)", which could be
replaced with the new syntax; replaced with the new syntax;
@ -113,24 +116,25 @@ Impact On Existing Code
c.10 uses of "delattr", which could use the new syntax. c.10 uses of "delattr", which could use the new syntax.
As examples, the line As examples, the line::
setattr(self, attr, change_root(self.root, getattr(self, attr))) setattr(self, attr, change_root(self.root, getattr(self, attr)))
from Lib/distutils/command/install.py could be rewritten from Lib/distutils/command/install.py could be rewritten::
self.(attr) = change_root(self.root, self.(attr)) self.(attr) = change_root(self.root, self.(attr))
and the line and the line::
setattr(self, method_name, getattr(self.metadata, method_name)) setattr(self, method_name, getattr(self.metadata, method_name))
from Lib/distutils/dist.py could be rewritten from Lib/distutils/dist.py could be rewritten::
self.(method_name) = self.metadata.(method_name) self.(method_name) = self.metadata.(method_name)
Performance Impact Performance Impact
==================
Initial pystone measurements are inconclusive, but suggest there may Initial pystone measurements are inconclusive, but suggest there may
be a performance penalty of around 1% in the pystones score with the be a performance penalty of around 1% in the pystones score with the
@ -143,19 +147,21 @@ Performance Impact
Error Cases Error Cases
===========
Only strings are permitted as attribute names, so for instance the Only strings are permitted as attribute names, so for instance the
following error is produced: following error is produced::
>>> x.(99) = 8 >>> x.(99) = 8
Traceback (most recent call last): Traceback (most recent call last):
File "<stdin>", line 1, in <module> File "<stdin>", line 1, in <module>
TypeError: attribute name must be string, not 'int' TypeError: attribute name must be string, not 'int'
This is handled by the existing PyObject_GetAttr function. This is handled by the existing ``PyObject_GetAttr`` function.
Draft Implementation Draft Implementation
====================
A draft implementation adds a new alternative to the "trailer" A draft implementation adds a new alternative to the "trailer"
clause in Grammar/Grammar; a new AST type, "DynamicAttribute" in clause in Grammar/Grammar; a new AST type, "DynamicAttribute" in
@ -163,21 +169,22 @@ Draft Implementation
compile.c, and three new opcodes (load/store/del) with compile.c, and three new opcodes (load/store/del) with
accompanying changes to opcode.h and ceval.c. The patch consists accompanying changes to opcode.h and ceval.c. The patch consists
of c.180 additional lines in the core code, and c.100 additional of c.180 additional lines in the core code, and c.100 additional
lines of tests. It is available as sourceforge patch #1657573 [1]. lines of tests. It is available as sourceforge patch #1657573 [1]_.
Mailing Lists Discussion Mailing Lists Discussion
========================
Initial posting of this PEP in draft form was to python-ideas on Initial posting of this PEP in draft form was to python-ideas on
20070209 [2], and the response was generally positive. The PEP was 20070209 [2]_, and the response was generally positive. The PEP was
then posted to python-dev on 20070212 [3], and an interesting then posted to python-dev on 20070212 [3]_, and an interesting
discussion ensued. A brief summary: discussion ensued. A brief summary:
Initially, there was reasonable (but not unanimous) support for the Initially, there was reasonable (but not unanimous) support for the
idea, although the precise choice of syntax had a more mixed idea, although the precise choice of syntax had a more mixed
reception. Several people thought the "." would be too easily reception. Several people thought the "." would be too easily
overlooked, with the result that the syntax could be confused with a overlooked, with the result that the syntax could be confused with a
method/function call. A few alternative syntaxes were suggested: method/function call. A few alternative syntaxes were suggested::
obj.(foo) obj.(foo)
obj.[foo] obj.[foo]
@ -201,7 +208,7 @@ Mailing Lists Discussion
Instead of new syntax, a new "wrapper class" was proposed, with the Instead of new syntax, a new "wrapper class" was proposed, with the
following specification / conceptual implementation suggested by following specification / conceptual implementation suggested by
Martin von Loewis: Martin von Löwis::
class attrs: class attrs:
def __init__(self, obj): def __init__(self, obj):
@ -226,22 +233,24 @@ Mailing Lists Discussion
References References
==========
[1] Sourceforge patch #1657573 .. [1] Sourceforge patch #1657573
http://sourceforge.net/tracker/index.php?func=detail&aid=1657573&group_id=5470&atid=305470 http://sourceforge.net/tracker/index.php?func=detail&aid=1657573&group_id=5470&atid=305470
[2] https://mail.python.org/pipermail/python-ideas/2007-February/000210.html .. [2] https://mail.python.org/pipermail/python-ideas/2007-February/000210.html
and following posts and following posts
[3] https://mail.python.org/pipermail/python-dev/2007-February/070939.html .. [3] https://mail.python.org/pipermail/python-dev/2007-February/070939.html
and following posts and following posts
Copyright Copyright
=========
This document has been placed in the public domain. This document has been placed in the public domain.
..
Local Variables: Local Variables:
mode: indented-text mode: indented-text
indent-tabs-mode: nil indent-tabs-mode: nil