flonk
flonk

Reputation: 3876

Create a blank class instance without class definition

Sometimes I use classes without any structure, i.e.

class Foo(object):
    pass

foo = Foo()

I wonder if there is a more compact way which avoids the clumsy, meaningless definition. Can one directly initialize blank object-instances?

Upvotes: 2

Views: 87

Answers (2)

TerryA
TerryA

Reputation: 60014

Use type():

>>> foo = type('Foo', (), {})()
>>> foo
<__main__.Foo object at 0x100499f50>

Upvotes: 4

Fred Foo
Fred Foo

Reputation: 363777

foo = object()

is the common way.

Upvotes: 3

Related Questions