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.
Answer from Matthias Schreiber on Stack Overflow
🌐
Python
docs.python.org › 3 › library › types.html
types — Dynamic type creation and names for built-in types
February 23, 2026 - SimpleNamespace objects may be initialized in the same way as dict: either with keyword arguments, with a single positional argument, or with both. When initialized with keyword arguments, those are directly added to the underlying namespace.
🌐
GitHub
gist.github.com › kmatt › a75afd051d538190d65db5243d553688
Python JSON to SimpleNamespace Object · GitHub
Python JSON to SimpleNamespace Object. GitHub Gist: instantly share code, notes, and snippets.
🌐
Python.org
discuss.python.org › typing
Typing rules for SimpleNamespace - Typing - Discussions on Python.org
August 15, 2024 - What would be needed to be able to express SimpleNamespace using python typings? from types import SimpleNamespace my_obj = SimpleNamespace(A=1, B='abc') print(my_obj.B) # mypy and pyright can't infer the type of B righ…
🌐
Python
bugs.python.org › issue40284
Issue 40284: Add mapping methods to types.SimpleNamespace - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/84465
🌐
DEV Community
dev.to › taqkarim › extending-simplenamespace-for-nested-dictionaries-58e8
Extending SimpleNamespace for Nested Dictionaries - DEV Community
April 13, 2020 - I'm a huge fan of Python3's types.SimpleNamespace. Basically, it allows us to take a dict, like so:... Tagged with python, tutorial.
Find elsewhere
🌐
GitHub
github.com › python › cpython › issues › 83256
types.SimpleNamespace should preserve attribute ordering (?) · Issue #83256 · python/cpython
December 17, 2019 - 3.9 (EOL)end of lifeend of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement · ericsnowcurrently · opened · on Dec 17, 2019 · Issue body actions · BPO · 39075 · Nosy · @rhettinger, @ericsnowcurrently, @serhiy-storchaka, @1st1, @ZackerySpytz, @miss-islington, @tirkarthi, @shihai1991 · PRs · bpo-39075: types.SimpleNamespace no longer sorts attributes in its repr #19430 ·
Author   ericsnowcurrently
🌐
O'Reilly
oreilly.com › library › view › mastering-object-oriented-python › 9781789531367 › 8422372e-7b76-412b-8e16-9a5e380534b3.xhtml
Configuration via SimpleNamespace - Mastering Object-Oriented Python - Second Edition [Book]
June 14, 2019 - >>> import types >>> config = types.SimpleNamespace( ... param1="some value", ... param2=3.14, ...
Author   Steven F. Lott
Published   2019
Pages   770
🌐
Python
bugs.python.org › issue39076
Issue 39076: Use types.SimpleNamespace for argparse.Namespace - Python tracker
December 17, 2019 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/83257
Top answer
1 of 3
46

SimpleNamespace is basically just a nice facade on top of a dictionary. It allows you to use properties instead of index keys. This is nice as it is super flexible and easy to manipulate.

The downside of that flexibility is that it doesn't provide any structure. There is nothing to stop someone from calling SimpleNamespace(x=ax, y=ay) (and del a.z at some point later). If this instance gets passed to your function, the exception occurs when you try to access the field.

In contrast, namedtuple lets you create a structured type. The type will have a name and it will know what fields it is supposed to have. You won't be able to make an instance without each of those field and they can't be removed later. Additionally, the instance is immutable, so you will know that the value in a.x will always be the same.

It's up to you to decide if you need the flexibility that SimpleNamespace gives you, or if you prefer to have the structure and guarantees provided by namedtuple.

2 of 3
4

I really like the answer about structured versus not, so I'm just providing a concrete example below.

SimpleNamespace will accept keys that begin with _. If you're looking for a quick and easy way to turn, say, JSON you don't control into objects with field names, this is very handy:

d = {"_id": 2342122, "text": "hi there!"} # Elasticsearch gives this id!
e = SimpleNamespace(**d) # works
Name = namedtuple("Name", sorted(d)) # ValueError so we can't do Name(**d)

Note above that you can see that namedtuple gives us a whole extra object that SimpleNamespace never will. Each SimpleNamespace is really a "unique snowflake", whereas namedtuple exists without ever being instantiated with any concrete values. Wherever you have need of abstractions that generalize on concrete values, you probably should prefer it.

🌐
PyPI
pypi.org › project › simple-namespace
Client Challenge
August 2, 2021 - JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
GitHub
github.com › theskumar › python-dotenv › issues › 325
Option to return dotenv dict as a SimpleNamespace · Issue #325 · theskumar/python-dotenv
May 17, 2021 - Would you accept something like this? So I could have dot accessible variables very simply. from types import SimpleNamespace def dotenv_namespace(*args, **kwargs): return SimpleNamespace(**dotenv_values(*args, **kwargs))
Published   May 17, 2021
Author   adammarples
🌐
GitHub
github.com › python › typing › issues › 688
advanced SimpleNamespace typing · Issue #688 · python/typing
November 26, 2019 - class MySubType(TypedSimpleNamespace): bar: int class MyType(TypedSimpleNamespace): foo: MySubType # correct a: MyType = SimpleNamespace(foo=SimpleNamespace(bar=1)) # incorrect, `bar` should be a number a: MyType = SimpleNamespace(foo=SimpleNamespace(bar='abc')) ... topic: featureDiscussions about new features for Python's type annotationsDiscussions about new features for Python's type annotations
Author   itajaja
🌐
YouTube
youtube.com › watch
PYTHON : What is the difference between SimpleNamespace and empty class definition? - YouTube
PYTHON : What is the difference between SimpleNamespace and empty class definition?To Access My Live Chat Page, On Google, Search for "hows tech developer co...
Published   April 13, 2023
🌐
Feldroy
daniel.feldroy.com › posts › til-2024-12-types-simplenamespace-is-a-bunch-class
TIL: types.SimpleNamespace is a Bunch class
December 25, 2024 - I've created my own and even documented it a two times. What I did not know until recently was the types library in core Python provides easy bunch class functionality. Credit to Jeremy Howard for schooling me on this one. from types import SimpleNamespace as ns ·
🌐
Proinsias
proinsias.github.io › til › python-the-simplenamespace-utility-class
Python: The SimpleNamespace Utility Class - Looking for data in all the right places…
May 13, 2025 - The SimpleNamespace type from the types library provides an alternative to an empty class (class MyClass: pass) from which one can add and remove attributes.
🌐
GitHub
github.com › HessamLa › RecursiveNamespace
GitHub - HessamLa/RecursiveNamespace: Recursive Namespace. An extension of SimpleNamespace · GitHub
RecursiveNamespace is an extension of Python's SimpleNamespace that provides enhanced functionality for working with nested namespaces and dictionaries.
Starred by 4 users
Forked by 3 users
Languages   Python
🌐
GitHub
github.com › microsoft › pylance-release › issues › 4984
No code completion or renaming for SimpleNamespace objects · Issue #4984 · microsoft/pylance-release
October 19, 2023 - None of that happens, looks like refactoring and completion do not work for SimpleNamespace. I can understand why completion does not work, as the attributes are created on the fly at runtime, but renaming (refactoring) should. Put the cursor after print(testNS. and wait. Try to rename MEMBER in line testNS.MEMBER = 1 using F2 or right-click->Rename · Python version (& distribution if applicable, e.g.
Author   DervishD
🌐
Python
bugs.python.org › issue16160
Issue 16160: subclassing types.SimpleNamespace does not work - Python tracker
October 8, 2012 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/60364