Python利用new创建一个类的对象

it2025-01-13  22

class MyClass(BaseClass):    def __new__(cls, *args, **kwargs):        return super(MyClass, cls).__new__(cls, *args, **kwargs)

super并不是一个函数,而是一个类名,形如super(B, cls)事实上调用了super类的初始化函数,产生了一个super对象。Python Manuals上介绍:super(type[, object-or-type]) Return a proxy object that delegates method calls to a parent or sibling class of type. Note: super() only works for new-style classes.

super接收两个参数,两个参数的类型之间需要有继承关系,否则会报:TypeError: super(type, obj): obj must be an instance or subtype of type  

T.__new__(S, ...) return a new object with type S, a subtype of T

转载于:https://www.cnblogs.com/tuzkee/archive/2013/02/01/2888743.html

最新回复(0)