Python simple naked objects

You need to create a simple class first:

class Foo(object):

myobject = Foo() = 'bar'

You can make it a one-liner like this:

myobject = type("Foo", (object,), {})() = '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.

Leave a Comment