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

Mike Klaas mike.klaas at gmail.com
Tue Feb 13 20:27:48 CET 2007


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).

As a comparison, enumerate (that I would have believed was much more
frequent a priori), is used 67 times, and zip/izip 165 times.

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

-Mike


More information about the Python-Dev mailing list