by reinhardt · 14y ago · 0 comments
Compiling strings is usually a code smell but at least in the case of namedtuple I'm pretty sure it is justified for at least one reason: performance. It's certainly possible to be implemented in more idiomatic python but the result would probably be less efficient compared to, say, dicts or regular objects.
[1] http://www.python.org/dev/peps/pep-3119/#abcs-vs-duck-typing
And if it is necessary for performance reasons, Python should probably be able to do a similar thing efficiently without compiling big old blobs of text.
It is true, of course, that this can be hidden as an implementation detail with relatively little impact to end users... but the mere possibility of relatively clean and judicious uses do not change the fact that as a pattern, it is tricky and hard to read and easy to mess up in horrible ways. It is not that it is fundamentally unworkable. But if it is the only/best way, then that probably indicates a place where Python could be incrementally improved...
There are other drawbacks to exec though, some people have disabled exec for security reasons and it's opaque to things like pypy.