Python: The SimpleNamespace Utility Class
The
SimpleNamespace
type from thetypes
library 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
object
subclass that provides attribute access to its namespace, as well as a meaningful [representation].Unlike
object
, withSimpleNamespace
you can add and remove attributes. If aSimpleNamespace
object is initialized with keyword arguments, those are directly added to the underlying namespace.
SimpleNamespace
may be useful as a replacement forclass NS: pass
. However, for a structured record type usenamedtuple()
instead.
Via docs.python.org.
Leave a comment