How to create inline objects with properties?
obj = type('obj', (object,), {'propertyName' : 'propertyValue'})
there are two kinds of type
function uses.
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.
Peter's answer
obj = lambda: None
obj.propertyName = 'propertyValue'