Python: The SimpleNamespace Utility Class
The
SimpleNamespacetype from thetypeslibrary provides an alternative to an empty class (class MyClass: pass) from which one can add and remove attributes.
import types
enki = types.SimpleNamespace()
enki.type = 'God'
print(enki.type) # God
del enki.type
print(enki.type)
# object has no attribute 'type'
Via enkipro.com.
A simple
objectsubclass that provides attribute access to its namespace, as well as a meaningful [representation].Unlike
object, withSimpleNamespaceyou can add and remove attributes. If aSimpleNamespaceobject is initialized with keyword arguments, those are directly added to the underlying namespace.
SimpleNamespacemay be useful as a replacement forclass NS: pass. However, for a structured record type usenamedtuple()instead.
Via docs.python.org.
Leave a comment