How to create inline objects with properties?

Python 3.3 added the SimpleNamespace class for that exact purpose:

>>> from types import SimpleNamespace

>>> obj = SimpleNamespace(propertyName='propertyValue')
>>> obj
namespace(propertyName='propertyValue')

>>> obj.propertyName
'propertyValue'

In addition to the appropriate constructor to build the object, SimpleNamespace defines __repr__ and __eq__ (documented in 3.4) to behave as expected.


obj = type('obj', (object,), {'propertyName' : 'propertyValue'})

there are two kinds of type function uses.


Peter's answer

obj = lambda: None
obj.propertyName = 'propertyValue'