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

Josiah Carlson jcarlson at uci.edu
Tue Feb 13 19:47:39 CET 2007


"Greg Falcon" <veloso at verylowsodium.com> wrote:
> Guido van Rossum wrote:
> > Also, Nick's examples show (conceptual)
> > aliasing problems: after "x = attrview(y)", both x and y refer to the
> > same object, but use a different notation to access it.
> 
> Of course there is an aliasing here, but it's being explicitly
> constructed, and so I fail to see what is problematic about it.  You
> propose in PEP 3106 that Py3k's items/keys/values dictionary methods
> should return wrapper objects, which provide a different interface to
> the same data, and which can be used to mutate the underlying objects.
>  Aren't the set of ailasing concerns in these two cases exactly the
> same?

Only if it was spelled obj.attrview() .

Having personally suggested a variant of attrview to others who have
asked for dynamic attribute access in the past, I can't say I'm
particularly convinced, today, that attrview(obj) or an equivalent is
necessarily better than obj.[...] .  Why?  In order for it to be
effective, either you need to save it to a name (cluttering up a
namespace, adding mental overhead that values X and Y are really the
same thing), or you need to keep calling attrview over and over (in
which case you may as well stick to the getattr/setattr/delattr).

I personally like the syntax, as my non-proportional font and syntax
highlighting editor can easily be taught to recognize that particular
bit of syntax and _really_ highlight it if I find that my brain can't do
the pattern matching.

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.


 - Josiah



More information about the Python-Dev mailing list