且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python有什么东西像匿名内部类的Java?

更新时间:2023-11-28 23:47:22

您可以使用 type(name,bases,dict) 内建功能即时创建类。例如:

You can use the type(name, bases, dict) builtin function to create classes on the fly. For example:

op = type("MyOptionParser", (OptionParser,object), {"foo": lambda self: "foo" })
op().foo()

由于OptionParser不是new-style类,你必须在基类列表中显式包含 object

Since OptionParser isn't a new-style class, you have to explicitly include object in the list of base classes.