The trouble with "dynamic attributes".

Lie Ryan lie.1296 at gmail.com
Fri Sep 17 13:08:54 EDT 2010


On 09/17/10 07:46, John Nagle wrote:
>    There's a tendency to use "dynamic attributes" in Python when
> trying to encapsulate objects from other systems.  It almost
> works.  But it's usually a headache in the end, and should be
> discouraged.  Here's why.

I personally love them, they makes XML files looks more like python objects.

<a>
    <b>foo</b>
    <c>bar</c>
</a>

being able to say:

if a.b == 'foo':
    print a.c

is very convenient.

Yes, it doesn't work if the attribute contains special characters that
python doesn't recognize; but you shouldn't use these syntax if that's
the case. And even dict-syntax is not perfect for accessing XML file, e.g.:

<a>
    <b>foo</b>
    <b>bar</b>
</a>

should a['b'] be 'foo' or 'bar'?

So, personally I think both attribute-syntax and dict-syntax should
continue; it should be up to the programmer to determine whether the
limitations imposed by these syntaxes are suitable for their need (e.g.
if the programmer knows he would only use alphabet tag name, then
attr-style syntax is fine; and if he knows that there is no duplicate,
then dict-style syntax is fine as well; and if he can't rely on both,
then and only then, he'd be forced to do it the long way)



More information about the Python-list mailing list