[Python-ideas] A mutable alternative to namedtuple

Eric V. Smith eric at trueblade.com
Tue Mar 17 18:20:08 CET 2015


On 03/17/2015 12:52 PM, Luciano Ramalho 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.

https://pypi.python.org/pypi/namedlist

It also adds default values to the generated constructor, which may or
may not be desirable. But if used exactly like collections.namedtuple,
it ignores the default values.

Eric.

> 
> 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:
> 
> https://gist.github.com/ramalho/fd3d367e9d3b2a659faf
> 
> What do you think?
> 
> Cheers,
> 
> Luciano
> 
> 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
> 
> 



More information about the Python-ideas mailing list