On 11/10/2011 3:51 AM, Devin Jeanpierre wrote:
Because Python doesn't allow "--" to be an attribute name, and so
namedtuple doesn't let you try:
t = namedtuple("T", "foo -- bar")(1, 2, 3)
print(t.foo)
print(t.--)
print(t.bar)
'--' is a valid attribute name on virtually any object that supports
attribute setting (e.g. function objects).
ob.-- is not valid Python because '--' is not a name.
> Of course, you need to use setattr() and getattr().
I consider the fact that CPython's setattr accepts non-name strings to
be a bit of a bug. Or if you will, leniency for speed. (A unicode name
check in Py3 would be much more expensive than an ascii name check in
Py2.) I would consider it legitimate for another implementation to only
accept names and to use a specialized name_dict for attribute dictionaries.
So I consider it quite legitimate for namedtuple to requires real names
for the fields. The whole point is to allow ob.name access to tuple
members. Someone who plans to use set/getattr with arbitrary strings
should just use a dict instead of a tuple.
--
Terry Jan Reedy
--
http://mail.python.org/mailman/listinfo/python-list