How to create inline objects with properties?

Jader Dias picture Jader Dias · Oct 7, 2009 · Viewed 34.4k times · Source

In Javascript it would be:

var newObject = { 'propertyName' : 'propertyValue' };
newObject.propertyName;  // returns "propertyValue"

But the same syntax in Python would create a dictionary, and that's not what I want

new_object = {'propertyName': 'propertyValue'}
new_object.propertyName  # raises an AttributeError

Answer

SilentGhost picture SilentGhost · Oct 7, 2009
obj = type('obj', (object,), {'propertyName' : 'propertyValue'})

there are two kinds of type function uses.