简体   繁体   中英

Create a blank class instance without class definition

Sometimes I use classes without any structure, ie

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?

Use type() :

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

是常见的方式。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM