I have created a decorator to decorate all instance methods of a class. I have written following code to do so.
def debug(func):
msg = func.__name__
@wraps(func)
def wrapper(*args, **kwargs):
print(msg)
return func(*args, **kwargs)
return wrapper
# Decorator for all the methods in a class
def debugmethods(cls):
for key, val in vars(cls).items():
if callable(val):
setattr(cls, key, debug(val))
return cls
@debugmethods
class Spam:
def foo(self):
pass
def bar(self):
pass
Now I am trying to understand how this works, I mean when will this decoration happen and how can I check that?
a) It already happened?
b) When I access Spam class for the first time? For e.g.
for key, val in Spam.__dict__.items():
print(key, val)
c) When I instantiate Spam class for the first time? For e.g.
spam = Spam()
for key, val in Spam.__dict__.items():
print(key, val)
This is actually easily seen in action if you add a few print lines:
Result:
In short, the answer is much like what @jonrsharpe said, it already happened. Just figure the visual would help.