Correct type for a simple “bag of attributes” namespace object (was: 3 Suggestions to Make Python Easier For Children)

Ben Finney ben+python at benfinney.id.au
Sat Aug 2 15:58:59 EDT 2014


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>

-- 
 \      “Nothing is more sacred than the facts.” —Sam Harris, _The End |
  `\                                                   of Faith_, 2004 |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list