On Tue, Mar 17, 2015 at 10:52 AM, Luciano Ramalho luciano@ramalho.org wrote:
Sometimes we need a simple class to hold some mutable attributes, provide a nice repr, support == for testing, and support iterable unpacking, so you can write:
p = Point(3, 4) x, y = p
That's very much like the classes built by namedtuple, but mutable.
I propose we add to the collections module another class factory. I am calling it plainclass, but perhaps we can think of a better name. Here is how it would be used:
import collections Point = collections.plainclass('Point', 'x y')
The signature of the plainclass function would be exactly the same as namedtuple, supporting the same alternative ways of naming the attributes.
The semantics of the generated Point class would be like this code:
Should it also have all the same methods as a namedtuple class (e.g. the tuple methods, _make, _replace)? What about the other namedtuple attrs (_fields, etc.)?
What's the motivation for parity with namedtuple (particularly iteration)? I suppose I see the desire for a fixed set of mutable attributes specific to the generated type. However, there have been numerous discussions on this list about alternative approaches to namedtuple which apply here. Simply adapting namedtuple may not be the right thing.
Regardless, this is the sort of thing that should bake outside the stdlib for a while to prove it's approach and its worth, much as namedtuple did. It would also help if there were a concrete use case in the stdlib that this new class (factory) would satisfy.
PS. I am aware that there are "Namespace" classes in the standard library (e.g. [2]). They solve a different problem.
[2] https://docs.python.org/3/library/argparse.html#argparse.Namespace
Don't forget types.SimpleNamespace. :)
-eric