Create a simple object

linkaiyi
Follow

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("", (), {})()
Object has 0 attachments

Was this article helpful?

This article is viewed 10 times!

Recent Viewed Articles

0 Comments

Leave a Comment

Support