6 ms·
> Why is it needed, instead of just creating an anonymous class? I think the question should be: why do you want an anonymous class? If it's sufficiently ad-ho
by nemetroid 6y ago
> Why is it needed, instead of just creating an anonymous class?
I think the question should be: why do you want an anonymous class? If it's sufficiently ad-hoc that it doesn't warrant a name, you would typically use a dict.
> Or allowing the class factory to manipulate the current namespace?
I think that would just be considered too surprising and non-standard behaviour (other Python functions don't get to modify the scope they're called from).
- gorgoiler 6y agoNamed tuples, the example I called out, are a good improvement over dicts for adhoc structs that don’t require full class definitions. But you’re right. It’s hardly cumbersome to create classes that simply exist to contain data in a sightly more structured way than a dict.
- nemetroid 6y ago> Named tuples, the example I called out, are a good improvement over dicts for adhoc structs that don’t require full class definitions. Doesn't a lot of that improvement come from the fact that they have a name, though? I guess you could argue for an "UnnamedTuple" which only enforces tuple size and member names. But if you're able to specify the member names, it feels like the thing being represented is concrete enough that you should be able to come up with a name for it, and that having a name would be beneficial.