[Python-Dev] New syntax for 'dynamic' attribute access

Tristan Seligmann mithrandi at mithrandi.za.net
Mon Feb 12 11:41:05 CET 2007


* Ben North <ben at redfrontdoor.org> [2007-02-11 23:45:05 +0000]:

>     Dynamic attribute access is currently possible using the "getattr"
>     and "setattr" builtins.  The present PEP suggests a new syntax to
>     make such access easier, allowing the coder for example to write
> 
>         x.('foo_%d' % n) += 1
> 
>         z = y.('foo_%d' % n).('bar_%s' % s)
> 
>     instead of
> 
>         attr_name = 'foo_%d' % n
>         setattr(x, attr_name, getattr(x, attr_name) + 1)
> 
>         z = getattr(getattr(y, 'foo_%d' % n), 'bar_%s' % s)

Clipper (and other members of the "xBase" family) have something vaguely
similar, and from working with that syntax, my feeling is that this is
too "subtle" given the mixing of levels going on here (a "literal" name
vs. an expression evaluating to a name). That is, it's too easy to not
properly notice the presence / absence of the () and become confused
between foo.bar and foo.(bar) or similar.

(For the curious, the Clipper syntax is generally used in "commands"
which are a kind of statement that is macro-evaluated; so, for example,
something like this:

    USE clients

will be transformed into something like the following expression:

    dbUseArea("clients")

during the preprocessor pass, care of a "builtin" definition of USE;
this function has the effect of opening a database file on disk for use.

Now, in order to use the command syntax with a database name stored in a
variable, one does something like the following:

    USE (dbName)

which is transformed into something like:

    dbUseArea(dbname)

with similar effects as before.)
-- 
mithrandi, i Ainil en-Balandor, a faer Ambar
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
Url : http://mail.python.org/pipermail/python-dev/attachments/20070212/439fd0c8/attachment.pgp 


More information about the Python-Dev mailing list