Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有时我使用没有任何结构的类,即
class Foo(object): pass foo = Foo()
我想知道是否有更紧凑的方法可以避免笨拙、无意义的定义。可以直接初始化空白对象实例吗?
使用type():
type()
>>> foo = type('Foo', (), {})() >>> foo <__main__.Foo object at 0x100499f50>
foo = object()
是常见的方式。