104

The following seems to work either way. What is the advantage (other than the nice repr) of using types.SimpleNamespace? Or is it the same thing?

>>> import types
>>> class Cls():
...     pass
... 
>>> foo = types.SimpleNamespace() # or foo = Cls()
>>> foo.bar = 42
>>> foo.bar
42
>>> del foo.bar
>>> foo.bar
AttributeError: 'types.SimpleNamespace' object has no attribute 'bar'
0

2 Answers 2

148

This is explained pretty well in the types module description. It shows you that types.SimpleNamespace is roughly equivalent to this:

class SimpleNamespace:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __repr__(self):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

This provides the following advantages over an empty class:

  1. It allows you to initialize attributes while constructing the object: sn = SimpleNamespace(a=1, b=2)
  2. It provides a readable repr(): eval(repr(sn)) == sn
  3. It overrides the default comparison. Instead of comparing by id(), it compares attribute values instead.
7
  • 3
    Any idea why repr(SimpleNamespace(a=1)) displays "namespace(a=1)"? I'd expect it to be SimpleNamespace instead of simply namespace.
    – Demi-Lune
    May 24, 2019 at 11:46
  • 2
    @Demi-Lune It's hard-coded. However, SimpleNamespace's subclasses instances get their own class name. Sep 10, 2019 at 7:56
  • 11
    To add biggest benefit to me 4: it allows me to use dot.notation on dictionaries.
    – Anssi
    Nov 8, 2019 at 9:43
  • @Anssi: I assume you mean when one is used instead of a dictionary, correct?
    – martineau
    Nov 14, 2019 at 0:12
  • 2
    @martineau i prefer to use classes directly. I am using PyCharm and it can actually show and fill the variables set to self in init or set with dot notation after the instance of class have been made. (otherwise it goes to slots not dict). Thanks for pointing out AttrDict. I will check it out.
    – Anssi
    Dec 17, 2019 at 12:10
15

A class types.SimpleNamespace provides a mechanism to instantiate an object that can hold attributes and nothing else. It is, in effect, an empty class with a fancier __init__() and a helpful __repr__():

>>> from types import SimpleNamespace
>>> sn = SimpleNamespace(x = 1, y = 2)
>>> sn
namespace(x=1, y=2)
>>> sn.z = 'foo'
>>> del(sn.x)
>>> sn
namespace(y=2, z='foo')

or

from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)

output:

namespace(x=1, y=2)
namespace(y=2, z='foo')

This answer may also be helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.