kaywang 发表于 2015-4-24 08:18:03

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

  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
页: [1]
查看完整版本: Python利用new创建一个类的对象