Similarly to the decorations of functions, to decorate a class
@deco
class A:
...
is a shortcut of (some say syntactic sugar, I don't like the term):
class A:
...
A = deco(A)
And this usage makes sense if the decorator takes the original class and returns a modified class or sometimes even the original class (for instance if the decorator just adds the class to some registry).
In the code you have posted (ignoring typos and formatting), the createtime
function takes a class, but returns a function. A
in your code is function, not a class.
In the next step you are trying to subclass that function to create the class B
and that is an error. A base class must be a class, obviously. Unfortunately the confusing error message makes that not clear.
How to do it correctly depends on what you want to achieve and that is not clear. Maybe you wanted just to decorate the __init__
or the __new__
method to print the timestamp of its invocation. A class decorator could do just that as well. Just a note that if you would do that for the base class A
, it will be inherited by B
. If you decorate both, the timestamp will be printed twice.