The biggest gotcha is that namedtuples compare exactly like regular tuples. Thus, two namedtuple types with identical elements will compare equal even if the type and field names differ. Usually that isn't what you want.
I've taken to using attrs everywhere for exactly this reason: http://attrs.readthedocs.io/en/stable/why.html#namedtuples
Re:Customizing class creation in Python
by TTPrograms on Thursday March 30, 2017 @04:25PM
(#13998586)
I don't really see how it's a problem that it's "leaking" a tuple API - it's not like there's private methods in Python. Everything can see everything. If anything having the state of an object available explicitly as a tuple seems like a good idea to me, as opposed to trying to figure out what's a stateful variable and what's a method etc.
Re:Customizing class creation in Python
by joshuamorton on Thursday March 30, 2017 @04:47PM
(#13998730)
You'll get surprises such as your subclass being indexable when you (probably) don't want that.
What problem would that cause? Wouldn't it only be a problem if you attempted to index into it, which you wouldn't do (on purpose) if you didn't think it was indexable? And if you did try to index into it thinking that it wasn't indexable, it would be a bug, which is the same as when it's indexable...
Re:Customizing class creation in Python
by joshuamorton on Friday March 31, 2017 @12:27PM
(#14005383)
Exactly. You probably don't want this thing that is essentially a namespace to be indexable, but you index into it, which is a bug, but it quacks like a tuple, so your mistake propagates silently, which is a bad thing.
Its more permissive than one would expect. Although the bigger issue is what dragonne mentioned.
I haven't heard this before. I subclass namedtuples regularly—whenever I need a simple immutable object. I haven't had any problems personally. (Except when pickling them. I would not recommend that.)
Can any experienced Pythonistas weigh in? Is this bad practice?