我想装饰一个实际的类,使用此代码:Python类装饰
def my_decorator(cls):
def wrap(*args, **kw):
return object.__new__(cls)
return wrap
@my_decorator
class TestClass(object):
def __init__(self):
print "__init__ should run if object.__new__ correctly returns an instance of cls"
test = TestClass() # shouldn't TestClass.__init__() be run here?
我没有错误,但我也不会从TestClass.__init__()
看到消息。
根据the docs for new-style classes:
典型实现使用
super(currentclass, cls).__new__(cls[, ...])
适当的参数调用父类的方法__new__()
,然后修改返回之前需要将新创建的实例创建类的新实例。如果
__new__()
返回CLS的一个实例,则新实例的__init__()
方法将被调用像__init__(self[, ...])
,其中self是新实例,其余参数是为传递给__new__()
相同。
任何想法为什么__init__
未运行?
另外,我还试着拨打__new__
这样的:
return super(cls.__bases__[0], cls).__new__(cls)
,但它会返回一个TypeError
:
TypeError: super.__new__(TestClass): TestClass is not a subtype of super
由于某种原因,我想创建一个实例就关火'__init__'的行为。感谢您的澄清! – 2010-10-21 00:43:41