Discuss / Python / 为什么我按照示例写报错?

为什么我按照示例写报错?

Topic source

Hello = type('Hello', (object), dict(hello=fn))

如果第二个参数继承了object就报错: TypeError: type.__new__() argument 2 must be tuple, not type。

不加就能正常运行:Hello = type('Hello', (), dict(hello=fn)) ?

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

你再仔细对比示例。

柯里昂

#3 Created at ... [Delete] [Delete and Lock User]

第二个少了一个‘,’号

#4 Created at ... [Delete] [Delete and Lock User]

应该是Hello = type('Hello', (object,), dict(hello=fn))


  • 1

Reply