Namedtuple implementation details¶
I use namedtuple in Python all the time. The implementation is really interesting and unique, as this excellent namedtuple post demonstrates.
The most interesting part to me was the discussion of namespaces and using exec. I'm not sure if I'd write code like this, but it's an interesting way to determine if something is a valid identifier:
for name in (typename,) + field_names:
try:
exec ("%s = True" % name) in {}
except (SyntaxError, NameError):
raise ValueError('Invalid field name: %r' % name)
Interesting, now go read the article and see if it's something you would write.