Correct type for a simple “bag of attributes†namespace object
Roy Smith
roy at panix.com
Sun Aug 3 08:40:48 EDT 2014
In article <mailman.12580.1407068955.18130.python-list at python.org>,
Mark Lawrence <breamoreboy at yahoo.co.uk> wrote:
> On 02/08/2014 20:58, Ben Finney wrote:
> > Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
> >
> >> If you need instances which carry state, then object is the wrong
> >> class.
> >
> > Right. The ‘types’ module provides a SimpleNamespace class for the
> > common “bag of attributes†use case::
> >
> > >>> import types
> > >>> foo = types.SimpleNamespace()
> > >>> foo.x = 3
> > >>> foo
> > namespace(x=3)
> >
> > <URL:https://docs.python.org/3/library/types.html#types.SimpleNamespace>
> >
>
> What Alex Martelli called a bunch?
>
> http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-
> bunch-of-named/
Still overkill :-) I usually just do:
class Data:
pass
my_obj = Data()
That's all you really need. It's annoying that you can't just do:
my_obj = object()
which would be even simpler, because (for reasons I don't understand),
you can't create new attributes on my_obj.
More information about the Python-list
mailing list