You need to create a simple class first:
class Foo(object):
pass
myobject = Foo()
myobject.foo = 'bar'
You can make it a one-liner like this:
myobject = type("Foo", (object,), {})()
myobject.foo = 'bar'
The call to type
functions identically to the previous class
statement.
If you want to be really minimal…
myobject = type("", (), {})()
The key is that the built-in types (such as list
and object
) don’t support user-defined attributes, so you need to create a type using either a class
statement or a call to the 3-parameter version of type
.