[Python-Dev] Summary of "dynamic attribute access" discussion

Jean-Paul Calderone exarkun at divmod.com
Tue Feb 13 21:07:15 CET 2007


On Tue, 13 Feb 2007 11:27:48 -0800, Mike Klaas <mike.klaas at gmail.com> wrote:
>On 2/13/07, Josiah Carlson <jcarlson at uci.edu> wrote:
>
>> As for people who say, "but getattr, setattr, and delattr aren't used";
>> please do some searches of the Python standard library.  In a recent
>> source checkout of the trunk Lib, there are 100+ uses of setattr, 400+
>> uses of getattr (perhaps 10-20% of which being the 3 argument form), and
>> a trivial number of delattr calls.  In terms of applications where
>> dynamic attribute access tends to happen; see httplib, urllib, smtpd,
>> the SocketServer variants, etc.
>
>Another data point:  on our six-figure loc code base, we have 123
>instances of getattr, 30 instances of setattr, and 0 instances of
>delattr.  There are 5 instances of setattr( ... getattr( ... ) ) on
>one line (and probably a few more that grep didn't pick up that span
>multiple lines).

Another data point: in our six-figure loc code base, we have 469
instances of getattr, 91 instances of setattr, and 0 instances of
delattr.  There is one instances of setattr(..., getattr(...)), and
one instance of setattr(getattr(...), ...).

>
>+1 on .[] notation and the idea in general.
>

-1 on a syntax change for this.  Somewhere between -0 and +0 for a
builtin or library function like attrview().

Jean-Paul


More information about the Python-Dev mailing list