Movatterモバイル変換


[0]ホーム

URL:


Following system colour schemeSelected dark colour schemeSelected light colour scheme

Python Enhancement Proposals

PEP 363 – Syntax For Dynamic Attribute Access

Author:
Ben North <ben at redfrontdoor.org>
Status:
Rejected
Type:
Standards Track
Created:
29-Jan-2007
Post-History:
12-Feb-2007

Table of Contents

Warning

This PEP has been rejected.

×

SeeMailing Lists Discussion for more information.

Abstract

Dynamic attribute access is currently possible using the “getattr”and “setattr” builtins. The present PEP suggests a new syntax tomake such access easier, allowing the coder for example to write:

x.('foo_%d'%n)+=1z=y.('foo_%d'%n).('bar_%s'%s)

instead of:

attr_name='foo_%d'%nsetattr(x,attr_name,getattr(x,attr_name)+1)z=getattr(getattr(y,'foo_%d'%n),'bar_%s'%s)

Rationale

Dictionary access and indexing both have a friendly invocationsyntax: instead ofx.__getitem__(12) the coder can writex[12].This also allows the use of subscripted elements in an augmentedassignment, as in “x[12] += 1”. The present proposal brings thisease-of-use to dynamic attribute access too.

Attribute access is currently possible in two ways:

  • When the attribute name is known at code-writing time, the“.NAME” trailer can be used, as in:
    x.foo=42y.bar+=100
  • When the attribute name is computed dynamically at run-time, the“getattr” and “setattr” builtins must be used:
    x=getattr(y,'foo_%d'%n)setattr(z,'bar_%s'%s,99)

    The “getattr” builtin also allows the coder to specify a defaultvalue to be returned in the event that the object does not havean attribute of the given name:

    x=getattr(y,'foo_%d'%n,0)

This PEP describes a new syntax for dynamic attribute access —“x.(expr)” — with examples given in the Abstract above.

(The new syntax could also allow the provision of a default value inthe “get” case, as in:

x=y.('foo_%d'%n,None)

This 2-argument form of dynamic attribute access would not bepermitted as the target of an (augmented or normal) assignment. The“Discussion” section below includes opinions specifically on the2-argument extension.)

Finally, the new syntax can be used with the “del” statement, as in:

delx.(attr_name)

Impact On Existing Code

The proposed new syntax is not currently valid, so no existingwell-formed programs have their meaning altered by this proposal.

Across all “*.py” files in the 2.5 distribution, there are around600 uses of “getattr”, “setattr” or “delattr”. They break down asfollows (figures have some room for error because they werearrived at by partially-manual inspection):

c.300usesofplain"getattr(x, attr_name)",whichcouldbereplacedwiththenewsyntax;c.150usesofthe3-argumentform,i.e.,withthedefaultvalue;thesecouldbereplacedwiththe2-argumentformofthenewsyntax(thecasesbreakdownintoc.125caseswheretheattributenameisaliteralstring,andc.25whereit's only known at run-time);c.5usesofthe2-argumentformwithaliteralstringattributename,whichIthinkcouldbereplacedwiththestandard"x.attribute"syntax;c.120usesofsetattr,ofwhich15usegetattrtofindthenewvalue;allcouldbereplacedwiththenewsyntax,the15wheregetattrisalsoinvolvedwouldshowaparticularincreaseinclarity;c.5useswhichwouldhavetostayas"getattr"becausetheyarecallsofavariablenamed"getattr"whosedefaultvalueisthebuiltin"getattr";c.5usesofthe2-argumentform,insideatry/exceptblockwhichcatchesAttributeErrorandusesadefaultvalueinstead;thesecoulduse2-argumentformofthenewsyntax;c.10usesof"delattr",whichcouldusethenewsyntax.

As examples, the line:

setattr(self,attr,change_root(self.root,getattr(self,attr)))

from Lib/distutils/command/install.py could be rewritten:

self.(attr)=change_root(self.root,self.(attr))

and the line:

setattr(self,method_name,getattr(self.metadata,method_name))

from Lib/distutils/dist.py could be rewritten:

self.(method_name)=self.metadata.(method_name)

Performance Impact

Initial pystone measurements are inconclusive, but suggest there maybe a performance penalty of around 1% in the pystones score with thepatched version. One suggestion is that this is because the longermain loop in ceval.c hurts the cache behaviour, but this has notbeen confirmed.

On the other hand, measurements suggest a speed-up of around 40–45%for dynamic attribute access.

Error Cases

Only strings are permitted as attribute names, so for instance thefollowing error is produced:

>>>x.(99)=8   Traceback (most recent call last):     File "<stdin>", line 1, in <module>   TypeError: attribute name must be string, not 'int'

This is handled by the existingPyObject_GetAttr function.

Draft Implementation

A draft implementation adds a new alternative to the “trailer”clause in Grammar/Grammar; a new AST type, “DynamicAttribute” inPython.asdl, with accompanying changes to symtable.c, ast.c, andcompile.c, and three new opcodes (load/store/del) withaccompanying changes to opcode.h and ceval.c. The patch consistsof c.180 additional lines in the core code, and c.100 additionallines of tests. It is available as sourceforge patch #1657573[1].

Mailing Lists Discussion

Initial posting of this PEP in draft form was to python-ideas on20070209[2], and the response was generally positive. The PEP wasthen posted to python-dev on 20070212[3], and an interestingdiscussion ensued. A brief summary:

Initially, there was reasonable (but not unanimous) support for theidea, although the precise choice of syntax had a more mixedreception. Several people thought the “.” would be too easilyoverlooked, with the result that the syntax could be confused with amethod/function call. A few alternative syntaxes were suggested:

obj.(foo)obj.[foo]obj.{foo}obj{foo}obj.*fooobj->fooobj<-fooobj@[foo]obj.[[foo]]

with “obj.[foo]” emerging as the preferred one. In this initialdiscussion, the two-argument form was universally disliked, so itwas to be taken out of the PEP.

Discussion then took a step back to whether this particular featureprovided enough benefit to justify new syntax. As well as requiringcoders to become familiar with the new syntax, there would also bethe problem of backward compatibility — code using the new syntaxwould not run on older pythons.

Instead of new syntax, a new “wrapper class” was proposed, with thefollowing specification / conceptual implementation suggested byMartin von Löwis:

classattrs:def__init__(self,obj):self.obj=objdef__getitem__(self,name):returngetattr(self.obj,name)def__setitem__(self,name,value):returnsetattr(self.obj,name,value)def__delitem__(self,name):returndelattr(self,name)def__contains__(self,name):returnhasattr(self,name)

This was considered a cleaner and more elegant solution to theoriginal problem. (Another suggestion was a mixin class providingdictionary-style access to an object’s attributes.)

The decision was made that the present PEP did not meet the burdenof proof for the introduction of new syntax, a view which had beenput forward by some from the beginning of the discussion. Thewrapper class idea was left open as a possibility for a future PEP.

References

[1]
Sourceforge patch #1657573http://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.htmland following posts
[3]
https://mail.python.org/pipermail/python-dev/2007-February/070939.htmland following posts

Copyright

This document has been placed in the public domain.


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

Last modified:2024-04-14 20:08:31 GMT


[8]ページ先頭

©2009-2025 Movatter.jp