I am trying to decorate an actual class, using this code:
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?
I get no errors, but I also don't see the message from TestClass.__init__().
According to the docs for new-style classes:
Typical implementations create a new instance of the class by invoking the superclass’s
__new__()method usingsuper(currentclass, cls).__new__(cls[, ...])with appropriate arguments and then modifying the newly-created instance as necessary before returning it.If
__new__()returns an instance of cls, then the new instance’s__init__()method will be invoked like__init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to__new__().
Any ideas why __init__ isn't running?
Also, I have tried to call __new__ like this:
return super(cls.__bases__[0], cls).__new__(cls)
but it would return a TypeError:
TypeError: super.__new__(TestClass): TestClass is not a subtype of super